ancient-projects

My earliest programs (2006-2010)
git clone https://git.tronto.net/ancient-projects
Download | Log | Files | Refs | README

Frame.java (16426B)


      1 /*
      2 Copyright 2009 Sebastiano Tronto <sebastiano@luganega.org>
      3 
      4 This file is part of JBriscola.
      5 
      6     JBriscola is free software; you can redistribute it and/or modify
      7     it under the terms of the GNU General Public License as published by
      8     the Free Software Foundation; either version 2 of the License, or
      9     (at your option) any later version.
     10 
     11     JBriscola is distributed in the hope that it will be useful,
     12     but WITHOUT ANY WARRANTY; without even the implied warranty of
     13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     14     GNU General Public License for more details.
     15 
     16     You should have received a copy of the GNU General Public License
     17     along with JBriscola; if not, write to the Free Software
     18     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
     19 
     20 */
     21 
     22 import javax.swing.*;
     23 import javax.sound.sampled.*;
     24 import java.io.*;
     25 import java.net.*;
     26 import java.awt.*;
     27 import java.awt.event.*;
     28 
     29 public class Frame extends JFrame {
     30 	
     31 	/* Booleana per determinare se disegnare il mazzo di carte o no (se
     32 	 * è l'ultima mano). */
     33 	public static boolean mazzo;
     34 	
     35 	public static Main main;
     36 	
     37 	/* Menu, bottoni, pannelli e componenti vari. */
     38 	public static Pannello pannello;
     39 	public static JPanel pannello2;
     40 	public static JButton button;
     41 	public static JMenuBar barraMenu;
     42 	public static JMenu file, puntoInterrogativo;
     43 	public static JMenuItem nuovaPartita, opzioni, esci, informazioni, punti, aggiornamenti, tastiRapidi;
     44 	
     45 	/* Immagini delle carte. */
     46 	public static ImageIcon retro1, retro2, ultimaCarta;
     47 	public static ImageIcon[] giocata = new ImageIcon[2];
     48 	public static ImageIcon[] carteInMano = new ImageIcon[3];
     49 	public static ImageIcon[] carteCPU = new ImageIcon[3];
     50 	public static ImageIcon[] arrayCarteMescolate = new ImageIcon[40];
     51 	public static ImageIcon[][] arrayCarte = new ImageIcon[10][4];
     52 	
     53 	public Frame() {
     54 		
     55 		super( "Tronto's JBriscola " + main.version );
     56 		
     57 		mazzo = true;
     58 		main = new Main();
     59 		pannello = new Pannello();
     60 		pannello2 = new JPanel( new FlowLayout() );
     61 		
     62 		button = new JButton( main.stringaBottonePrendi );
     63 		button.addActionListener( new ActionListener() {
     64 			
     65 			/* Quando viene premuto il bottone per prendere/lasciare le carte,
     66 			 * valuta innanzitutto se il giocatore ha giocato, e poi a chi
     67 			 * darle ed eventualmente, se è finita la partita, a caricare
     68 			 * il suono adatto. */
     69 			public void actionPerformed( ActionEvent e ) {
     70 				
     71 				if ( giocata[0] != null && giocata[1] != null )
     72 					main.mano++;
     73 					
     74 				try {
     75 					main.chiPrende();
     76 				} catch ( Exception ex ) {
     77 					/* Avvisa di giocare una carta. */
     78 					JOptionPane.showMessageDialog( Frame.this, main.stringaPrimaGioca, "JBriscola " + main.version, JOptionPane.WARNING_MESSAGE );
     79 				}
     80 				
     81 				if ( main.mano == 16 )
     82 					/* Avvisa che si è arrivati alla quartultima mano. */
     83 					JOptionPane.showMessageDialog( Frame.this, main.stringaUltime4, "JBriscola " + main.version, JOptionPane.INFORMATION_MESSAGE );
     84 				
     85 				/* Non disegna più la briscola e il mazzo se sono già state
     86 				 * pescate tutte le carte. */
     87 				if ( main.mano == 17 ) {
     88 					mazzo = false;
     89 					ultimaCarta = null;
     90 					pannello.repaint();
     91 					Frame.this.repaint();
     92 				}
     93 				
     94 				/* Mostra il punteggio e carica i suoni se è finita la partita. */
     95 				if ( main.mano == 20 ) {
     96 					
     97 					String titolo = "";
     98 					String suonoFinale = "";
     99 					
    100 					if ( main.punteggio > main.punteggioCPU ) {
    101 						titolo = main.stringaVinto;
    102 						suonoFinale = "sounds/perso.wav";
    103 					}
    104 					if ( main.punteggio < main.punteggioCPU ) {
    105 						titolo = main.stringaPerso;
    106 						suonoFinale = "sounds/vinto.wav";
    107 					}
    108 					if ( main.punteggio == main.punteggioCPU ) {
    109 						titolo = main.stringaPareggio;
    110 						suonoFinale = "sounds/pareggio.wav";
    111 					}
    112 					
    113 					if ( main.sound ) {
    114 						try {
    115 							AudioFileFormat aff = AudioSystem.getAudioFileFormat( ClassLoader.getSystemResource( suonoFinale ) );
    116 							AudioInputStream ais = AudioSystem.getAudioInputStream( ClassLoader.getSystemResource( suonoFinale ) );
    117 							AudioFormat af = aff.getFormat();
    118 							DataLine.Info info = new DataLine.Info( Clip.class, ais.getFormat(), ( (int) ais.getFrameLength() * af.getFrameSize() ) );
    119 							Clip ol = ( Clip ) AudioSystem.getLine( info );
    120 							ol.open(ais);
    121 							ol.loop( 0 );
    122 						} catch ( Exception ex ) {
    123 							System.err.println( e );
    124 						}
    125 					}
    126 					
    127 					JOptionPane.showMessageDialog( Frame.this, main.stringaGiocatore + main.punteggio + main.stringaComputer + main.punteggioCPU, titolo, JOptionPane.PLAIN_MESSAGE );
    128 				}
    129 			}
    130 		} );
    131 		button.addKeyListener( new KeyAdapter() {
    132 			/* Aggiunge il keyListener per le scorciatoie da tastiera. Dato
    133 			 * che il bottone prendi/lascia è l'unico componente effettivo
    134 			 * della finestra, avrà obbligatoriamente il focus ed è necessario
    135 			 * che sia lui a gestire gli eventi della tastiera. */
    136 			public void keyReleased( KeyEvent key ) {
    137 
    138 				if ( main.keys ) {
    139 					switch ( key.getKeyCode() ) {
    140 						case KeyEvent.VK_1:
    141 							if ( mettiCarta( 231, 500 ) ) {
    142 								if ( !main.CPUHaGiocato )
    143 									main.giocaCPU();
    144 							}
    145 							break;
    146 							
    147 						case KeyEvent.VK_2:
    148 							if ( mettiCarta( 301, 500 ) ) {
    149 								if ( !main.CPUHaGiocato )
    150 									main.giocaCPU();
    151 							}
    152 							break;
    153 							
    154 						case KeyEvent.VK_3:
    155 							if ( mettiCarta( 371, 500 ) ) {
    156 								if ( !main.CPUHaGiocato )
    157 									main.giocaCPU();
    158 							}
    159 							break;
    160 							
    161 						case KeyEvent.VK_F1:
    162 							JOptionPane.showMessageDialog( Frame.this, main.stringaTasti2, "JBriscola - " + main.version, JOptionPane.INFORMATION_MESSAGE );
    163 							break;
    164 							
    165 						case KeyEvent.VK_F2:
    166 							if ( main.mano == 20 ) {
    167 								main.mischia();
    168 								pannello.repaint();
    169 								repaint();
    170 							}
    171 							else {
    172 								int selezione = JOptionPane.showOptionDialog( Frame.this, main.stringaVuoiDavveroIniziare, "JBriscola " + main.version, JOptionPane.YES_NO_OPTION,  JOptionPane.QUESTION_MESSAGE, null, new String[] { main.stringaSi, main.stringaNo }, main.stringaNo );
    173 								if ( selezione == JOptionPane.YES_OPTION ) {
    174 									main.mischia();
    175 									pannello.repaint();
    176 									repaint();
    177 								}
    178 							}
    179 							break;
    180 							
    181 						case KeyEvent.VK_F3:
    182 							OptionFrame optionFrame = new OptionFrame();
    183 							optionFrame.setDefaultCloseOperation( JFrame.DISPOSE_ON_CLOSE );
    184 							optionFrame.setResizable( false );
    185 							optionFrame.setAlwaysOnTop( true );
    186 							optionFrame.setSize( 300, 200 );
    187 							optionFrame.setLocation( (Toolkit.getDefaultToolkit().getScreenSize().width - 300) / 2, (Toolkit.getDefaultToolkit().getScreenSize().height - 150) / 2);
    188 							optionFrame.setVisible( true );
    189 							break;
    190 						case KeyEvent.VK_F4:
    191 						
    192 							JOptionPane.showMessageDialog( Frame.this, main.stringaGiocatore + main.punteggio + "\n" + main.stringaComputer + main.punteggioCPU, main.stringaPunteggio, JOptionPane.PLAIN_MESSAGE );
    193 							break;
    194 						case KeyEvent.VK_F5:
    195 						
    196 							JOptionPane.showMessageDialog( Frame.this, main.stringaInformazioni2, main.stringaInformazioni, JOptionPane.PLAIN_MESSAGE );
    197 							break;
    198 						case KeyEvent.VK_F6:
    199 						
    200 							String latest = main.version;
    201 							try {
    202 								URL latestIs = new URL( "http://porkynator.altervista.org/jbriscola-latest.is" );
    203 								BufferedReader latestIsReader = new BufferedReader( new InputStreamReader( latestIs.openStream() ) );
    204 								latest = latestIsReader.readLine();
    205 							} catch ( Exception ex ) {
    206 								JOptionPane.showMessageDialog( Frame.this, main.stringaNoInternet, "JBriscola - " + main.version, JOptionPane.ERROR_MESSAGE );
    207 							}
    208 							if ( latest.equals( main.version ) )
    209 								JOptionPane.showMessageDialog( Frame.this, main.stringaAggiornamentiNo, "JBriscola - " + main.version, JOptionPane.INFORMATION_MESSAGE );
    210 							else
    211 								JOptionPane.showMessageDialog( Frame.this, main.stringaAggiornamentiSi + "\n" + main.version + " ==> " + latest + "\n\n" + main.stringaScarica, "JBriscola - " + main.version, JOptionPane.INFORMATION_MESSAGE );
    212 							break;
    213 						case KeyEvent.VK_Q:
    214 						
    215 							int selezione = JOptionPane.showOptionDialog( Frame.this, main.stringaVuoiDavveroUscire, "JBriscola  " + main.version, JOptionPane.YES_NO_OPTION,  JOptionPane.QUESTION_MESSAGE, null, new String[] { main.stringaSi, main.stringaNo }, main.stringaNo );
    216 							if ( selezione == JOptionPane.YES_OPTION )
    217 								Frame.this.dispose();
    218 							break;
    219 					}
    220 				}
    221 			}
    222 		} );
    223 		
    224 		barraMenu = new JMenuBar();
    225 		
    226 		file = new JMenu( "File" );
    227 		
    228 		nuovaPartita = new JMenuItem( main.stringaNuovaPartita );
    229 		nuovaPartita.addActionListener( new ActionListener() {
    230 			/* Avvia una nuova partita, chiedendo prima conferma se ce n'è
    231 			 * già una in corso. */
    232 			public void actionPerformed( ActionEvent e ) {
    233 				if ( main.mano == 20 ) {
    234 					main.mischia();
    235 					pannello.repaint();
    236 					repaint();
    237 				}
    238 				
    239 				else {
    240 					int selezione = JOptionPane.showOptionDialog( Frame.this, main.stringaVuoiDavveroIniziare, "JBriscola " + main.version, JOptionPane.YES_NO_OPTION,  JOptionPane.QUESTION_MESSAGE, null, new String[] { main.stringaSi, main.stringaNo }, main.stringaNo );
    241 					if ( selezione == JOptionPane.YES_OPTION ) {
    242 						main.mischia();
    243 						pannello.repaint();
    244 						repaint();
    245 					}
    246 				}
    247 			}
    248 		});
    249 		
    250 		opzioni = new JMenuItem( main.stringaOpzioni );
    251 		opzioni.addActionListener( new ActionListener() {
    252 			/* Apre una OptionFrame per modificare le opzioni. */
    253 			public void actionPerformed( ActionEvent e ) {
    254 				
    255 				OptionFrame optionFrame = new OptionFrame();
    256 				optionFrame.setDefaultCloseOperation( JFrame.DISPOSE_ON_CLOSE );
    257 				optionFrame.setResizable( false );
    258 				optionFrame.setAlwaysOnTop( true );
    259 				optionFrame.setSize( 300, 200 );
    260 				optionFrame.setLocation( (Toolkit.getDefaultToolkit().getScreenSize().width - 300) / 2, (Toolkit.getDefaultToolkit().getScreenSize().height - 150) / 2);
    261 				optionFrame.setVisible( true );
    262 			}
    263 		});
    264 		
    265 		esci = new JMenuItem( main.stringaEsci );
    266 		esci.addActionListener( new ActionListener() {
    267 			/* Ciede conferma prima di uscire. */
    268 			public void actionPerformed( ActionEvent e ) {
    269 				int selezione = JOptionPane.showOptionDialog( Frame.this, main.stringaVuoiDavveroUscire, "JBriscola  " + main.version, JOptionPane.YES_NO_OPTION,  JOptionPane.QUESTION_MESSAGE, null, new String[] { main.stringaSi, main.stringaNo }, main.stringaNo );
    270 				if ( selezione == JOptionPane.YES_OPTION )
    271 					Frame.this.dispose();
    272 			}
    273 		});
    274 		
    275 		file.add( nuovaPartita );
    276 		file.add( opzioni );
    277 		file.addSeparator();
    278 		file.add( esci );
    279 		
    280 		puntoInterrogativo = new JMenu( "?" );
    281 		
    282 		informazioni = new JMenuItem( main.stringaInformazioni );
    283 		informazioni.addActionListener( new ActionListener() {
    284 			/* Visualizza le informazioni. */
    285 			public void actionPerformed( ActionEvent e ) {
    286 				JOptionPane.showMessageDialog( Frame.this, main.stringaInformazioni2, main.stringaInformazioni, JOptionPane.PLAIN_MESSAGE );
    287 			}
    288 		} );
    289 		
    290 		tastiRapidi = new JMenuItem( main.stringaTasti );
    291 		tastiRapidi.addActionListener( new ActionListener() {
    292 			/* Visualizza i tasti rapidi. */
    293 			public void actionPerformed( ActionEvent e ) {
    294 				JOptionPane.showMessageDialog( Frame.this, main.stringaTasti2, "JBriscola - " + main.version, JOptionPane.INFORMATION_MESSAGE );
    295 			}
    296 		} );
    297 		
    298 		punti = new JMenuItem( main.stringaPunteggio );
    299 		punti.addActionListener( new ActionListener() {
    300 			/* Visualizza il punteggio. */
    301 			public void actionPerformed( ActionEvent e ) {
    302 				JOptionPane.showMessageDialog( Frame.this, main.stringaGiocatore + main.punteggio + "\n" + main.stringaComputer + main.punteggioCPU, main.stringaPunteggio, JOptionPane.PLAIN_MESSAGE );
    303 			}
    304 		} );
    305 		
    306 		aggiornamenti = new JMenuItem( main.stringaAggiornamenti );
    307 		aggiornamenti.addActionListener( new ActionListener() {
    308 			/* Cerca aggiornamenti sul sito http://porkynator.altervista.org,
    309 			 * confrontando la versione del programma con quella scritta nel
    310 			 * file jbriscola-latest.is (online). */
    311 			public void actionPerformed( ActionEvent e ) {
    312 				String latest = main.version;
    313 				try {
    314 					URL latestIs = new URL( "http://porkynator.altervista.org/jbriscola-latest.is" );
    315 					BufferedReader latestIsReader = new BufferedReader( new InputStreamReader( latestIs.openStream() ) );
    316 					latest = latestIsReader.readLine();
    317 				} catch ( Exception ex ) {
    318 					JOptionPane.showMessageDialog( Frame.this, main.stringaNoInternet, "JBriscola - " + main.version, JOptionPane.ERROR_MESSAGE );
    319 				}
    320 				if ( latest.equals( main.version ) )
    321 					JOptionPane.showMessageDialog( Frame.this, main.stringaAggiornamentiNo, "JBriscola - " + main.version, JOptionPane.INFORMATION_MESSAGE );
    322 				else
    323 					JOptionPane.showMessageDialog( Frame.this, main.stringaAggiornamentiSi + "\n" + main.version + " ==> " + latest + "\n\n" + main.stringaScarica, "JBriscola - " + main.version, JOptionPane.INFORMATION_MESSAGE );
    324 			}
    325 		} );
    326 		
    327 		puntoInterrogativo.add( punti );
    328 		puntoInterrogativo.add( tastiRapidi );
    329 		puntoInterrogativo.add( informazioni );
    330 		puntoInterrogativo.add( aggiornamenti );
    331 		
    332 		barraMenu.add( file );
    333 		barraMenu.add( puntoInterrogativo );
    334 		
    335 		setJMenuBar( barraMenu );
    336 		
    337 		/* Inizializza le immagini del retro delle carte. */
    338 		retro1 = new ImageIcon( ClassLoader.getSystemResource( "img/retro1.jpg" ) );
    339 		retro2 = new ImageIcon( ClassLoader.getSystemResource( "img/retro2.jpg" ) );
    340 		
    341 		/* Inizializza tutte le immagini delle carte scorrendo l'array bidimensionale. */
    342 		for (int i = 0; i < 10; i++)
    343 			for (int j = 0; j < 4; j++)
    344 				arrayCarte[i][j] = new ImageIcon( ClassLoader.getSystemResource( "img/" + (i+1) + "-" + (j+1) + ".jpg" ) );
    345 		
    346 		/* Mischia le carte. */
    347 		main.mischia();
    348 		
    349 		/* Aggiunge i vari componenti. */
    350 		pannello2.add( button );
    351 		setLayout( new BorderLayout() );
    352 		add( pannello, BorderLayout.CENTER );
    353 		add( pannello2, BorderLayout.SOUTH );
    354 		
    355 		/* Chiama il metodo mettiCarta se l'utente clicca. */
    356 		addMouseListener(
    357 			new MouseAdapter() {
    358 				public void mouseClicked ( MouseEvent e ) {
    359 					if ( mettiCarta( e.getX(), e.getY() ) ) {
    360 						if ( !main.CPUHaGiocato )
    361 							main.giocaCPU();
    362 					}
    363 				}
    364 			}
    365 		);
    366 		
    367 		/* Chiede conferma prima di uscire quando viene chiusa la finestra. */
    368 		addWindowListener(
    369 			new WindowAdapter() {
    370 				public void windowClosing( WindowEvent e ) {
    371 					int selezione = JOptionPane.showOptionDialog( Frame.this, main.stringaVuoiDavveroUscire, "JBriscola  " + main.version, JOptionPane.YES_NO_OPTION,  JOptionPane.QUESTION_MESSAGE, null, new String[] { main.stringaSi, main.stringaNo }, main.stringaNo );
    372 					if ( selezione == JOptionPane.YES_OPTION )
    373 						System.exit( 0 );
    374 				}
    375 			}
    376 		);
    377 	}
    378 	
    379 	/* Se il giocatore ha cliccato su una carta la gioca e riproduce un suono casuale. */
    380 	public boolean mettiCarta( int x, int y ) {
    381 		
    382 		if ( !main.toccaAllaCPU && giocata[0] == null ) {
    383 			if ( main.sound && 
    384 				( ( x > 230 && x < 290 && y > 340 && y < 566 && carteInMano[0] != null ) ||
    385 				  ( x > 300 && x < 360 && y > 340 && y < 566 && carteInMano[1] != null ) || 
    386 				  ( x > 370 && x < 430 && y > 340 && y < 566 && carteInMano[2] != null ) ) ) {
    387 				try {
    388 					
    389 					double a = Math.random() * 6;
    390 					int i = (int) a;
    391 					
    392 					AudioFileFormat aff = AudioSystem.getAudioFileFormat( ClassLoader.getSystemResource("sounds/" + i + ".wav") );
    393 					AudioInputStream ais = AudioSystem.getAudioInputStream( ClassLoader.getSystemResource("sounds/" + i + ".wav") );
    394 					AudioFormat af = aff.getFormat();
    395 					DataLine.Info info = new DataLine.Info( Clip.class, ais.getFormat(), ( (int) ais.getFrameLength() * af.getFrameSize() ) );
    396 					Clip ol = ( Clip ) AudioSystem.getLine( info );
    397 					
    398 					ol.open(ais);
    399 					ol.loop( 0 );
    400 					
    401 				} catch ( Exception e ) {
    402 					System.err.println( e );
    403 				}
    404 			}
    405 			
    406 			if ( x > 230 && x < 290 && y > 340 && y < 566 && carteInMano[0] != null ) {
    407 				giocata[0] = carteInMano[0];
    408 				carteInMano[0] = null;
    409 				pannello.repaint();
    410 				return true;
    411 			}
    412 			
    413 			if ( x > 300 && x < 360 && y > 340 && y < 566 && carteInMano[1] != null ) {
    414 				giocata[0] = carteInMano[1];
    415 				carteInMano[1] = null;
    416 				pannello.repaint();
    417 				return true;
    418 			}
    419 			
    420 			if ( x > 370 && x < 430 && y > 340 && y < 566 && carteInMano[2] != null ) {
    421 				giocata[0] = carteInMano[2];
    422 				carteInMano[2] = null;
    423 				pannello.repaint();
    424 				return true;
    425 			}
    426 		}
    427 		
    428 		return false;
    429 	}
    430 }
    431 
    432