Frame.java (19678B)
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, nuovaPartitaAiTrenta, opzioni, esci, informazioni, punti, aggiornamenti, trucchi, tastiRapidi, briscola; 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, 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 /* Avvisa che si è arrivati alla quartultima mano. */ 82 if ( main.mano == 16 ) 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 ( main.aiTrenta && 97 ( ( main.punteggio >= 30 || main.punteggioCPU >= 30 ) ) ) ) { 98 99 String titolo = ""; 100 String suonoFinale = ""; 101 102 if ( main.punteggio > main.punteggioCPU ) { 103 titolo = main.stringaVinto; 104 suonoFinale = "sounds/perso.wav"; 105 } 106 if ( main.punteggio < main.punteggioCPU ) { 107 titolo = main.stringaPerso; 108 suonoFinale = "sounds/vinto.wav"; 109 } 110 if ( main.punteggio == main.punteggioCPU ) { 111 titolo = main.stringaPareggio; 112 suonoFinale = "sounds/pareggio.wav"; 113 } 114 115 if ( main.sound ) { 116 try { 117 AudioFileFormat aff = AudioSystem.getAudioFileFormat( ClassLoader.getSystemResource( suonoFinale ) ); 118 AudioInputStream ais = AudioSystem.getAudioInputStream( ClassLoader.getSystemResource( suonoFinale ) ); 119 AudioFormat af = aff.getFormat(); 120 DataLine.Info info = new DataLine.Info( Clip.class, ais.getFormat(), ( (int) ais.getFrameLength() * af.getFrameSize() ) ); 121 Clip ol = ( Clip ) AudioSystem.getLine( info ); 122 ol.open(ais); 123 ol.loop( 0 ); 124 } catch ( Exception ex ) { 125 System.err.println( e ); 126 } 127 } 128 129 JOptionPane.showMessageDialog( Frame.this, main.stringaGiocatore + main.punteggio + main.stringaComputer + main.punteggioCPU, titolo, JOptionPane.INFORMATION_MESSAGE ); 130 131 main.pulisciTavolo(); 132 133 } 134 if ( main.toccaAllaCPU ) 135 if ( main.aiTrenta ) 136 main.giocaCPUAiTrenta(); 137 else 138 main.giocaCPU(); 139 } 140 } ); 141 button.addKeyListener( new KeyAdapter() { 142 /* Aggiunge il keyListener per le scorciatoie da tastiera. Dato 143 * che il bottone prendi/lascia è l'unico componente effettivo 144 * della finestra, avrà obbligatoriamente il focus ed è necessario 145 * che sia lui a gestire gli eventi della tastiera. */ 146 public void keyReleased( KeyEvent key ) { 147 148 if ( main.keys ) { 149 switch ( key.getKeyCode() ) { 150 case KeyEvent.VK_1: 151 if ( mettiCarta( 231, 500 ) ) { 152 if ( !main.CPUHaGiocato ) 153 if ( main.aiTrenta ) 154 main.giocaCPUAiTrenta(); 155 else 156 main.giocaCPU(); 157 } 158 break; 159 160 case KeyEvent.VK_2: 161 if ( mettiCarta( 301, 500 ) ) { 162 if ( !main.CPUHaGiocato ) 163 if ( main.aiTrenta ) 164 main.giocaCPUAiTrenta(); 165 else 166 main.giocaCPU(); 167 } 168 break; 169 170 case KeyEvent.VK_3: 171 if ( mettiCarta( 371, 500 ) ) { 172 if ( !main.CPUHaGiocato ) 173 if ( main.aiTrenta ) 174 main.giocaCPUAiTrenta(); 175 else 176 main.giocaCPU(); 177 } 178 break; 179 180 case KeyEvent.VK_F1: 181 JOptionPane.showMessageDialog( Frame.this, main.stringaTasti2, "JBriscola - " + main.version, JOptionPane.INFORMATION_MESSAGE ); 182 break; 183 184 case KeyEvent.VK_F2: 185 if ( main.mano == 20 ) { 186 main.aiTrenta = false; 187 main.mischia(); 188 pannello.repaint(); 189 repaint(); 190 } 191 else { 192 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 ); 193 if ( selezione == JOptionPane.YES_OPTION ) { 194 main.aiTrenta = false; 195 main.mischia(); 196 pannello.repaint(); 197 repaint(); 198 } 199 } 200 break; 201 202 case KeyEvent.VK_F3: 203 OptionFrame optionFrame = new OptionFrame(); 204 optionFrame.setDefaultCloseOperation( JFrame.DISPOSE_ON_CLOSE ); 205 optionFrame.setResizable( false ); 206 optionFrame.setAlwaysOnTop( true ); 207 optionFrame.setSize( 300, 200 ); 208 optionFrame.setLocation( (Toolkit.getDefaultToolkit().getScreenSize().width - 300) / 2, (Toolkit.getDefaultToolkit().getScreenSize().height - 150) / 2); 209 optionFrame.setVisible( true ); 210 break; 211 212 case KeyEvent.VK_F4: 213 JOptionPane.showMessageDialog( Frame.this, main.stringaGiocatore + main.punteggio + main.stringaComputer + main.punteggioCPU, main.stringaPunteggio, JOptionPane.INFORMATION_MESSAGE ); 214 break; 215 216 case KeyEvent.VK_F5: 217 JOptionPane.showMessageDialog( Frame.this, main.stringaInformazioni2, main.stringaInformazioni, JOptionPane.PLAIN_MESSAGE ); 218 break; 219 220 case KeyEvent.VK_F6: 221 String latest = main.version; 222 try { 223 URL latestIs = new URL( "http://porkynator.altervista.org/jbriscola-latest.is" ); 224 BufferedReader latestIsReader = new BufferedReader( new InputStreamReader( latestIs.openStream() ) ); 225 latest = latestIsReader.readLine(); 226 } catch ( Exception ex ) { 227 JOptionPane.showMessageDialog( Frame.this, main.stringaNoInternet, "JBriscola - " + main.version, JOptionPane.ERROR_MESSAGE ); 228 } 229 if ( latest.equals( main.version ) ) 230 JOptionPane.showMessageDialog( Frame.this, main.stringaAggiornamentiNo, "JBriscola - " + main.version, JOptionPane.INFORMATION_MESSAGE ); 231 else 232 JOptionPane.showMessageDialog( Frame.this, main.stringaAggiornamentiSi + "\n" + main.version + " ==> " + latest + "\n\n" + main.stringaScarica, "JBriscola - " + main.version, JOptionPane.INFORMATION_MESSAGE ); 233 break; 234 235 case KeyEvent.VK_F7: 236 if ( main.mano == 20 ) { 237 main.aiTrenta = true; 238 main.mischia(); 239 pannello.repaint(); 240 repaint(); 241 } 242 else { 243 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 ); 244 if ( selezione == JOptionPane.YES_OPTION ) { 245 main.aiTrenta = true; 246 main.mischia(); 247 pannello.repaint(); 248 repaint(); 249 } 250 } 251 break; 252 253 case KeyEvent.VK_F8: 254 main.trucco( JOptionPane.showInputDialog( Frame.this, main.stringaInserisciCodice, "JBriscola - " + main.version, JOptionPane.PLAIN_MESSAGE ) ); 255 break; 256 257 case KeyEvent.VK_Q: 258 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 ); 259 if ( selezione == JOptionPane.YES_OPTION ) 260 Frame.this.dispose(); 261 break; 262 } 263 } 264 } 265 } ); 266 267 barraMenu = new JMenuBar(); 268 269 file = new JMenu( "File" ); 270 271 nuovaPartita = new JMenuItem( main.stringaNuovaPartita ); 272 nuovaPartita.addActionListener( new ActionListener() { 273 /* Avvia una nuova partita, chiedendo prima conferma se ce n'è 274 * già una in corso. */ 275 public void actionPerformed( ActionEvent e ) { 276 if ( main.mano == 20 ) { 277 main.aiTrenta = false; 278 main.mischia(); 279 pannello.repaint(); 280 repaint(); 281 } 282 283 else { 284 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 ); 285 if ( selezione == JOptionPane.YES_OPTION ) { 286 main.aiTrenta = false; 287 main.mischia(); 288 pannello.repaint(); 289 repaint(); 290 } 291 } 292 } 293 }); 294 295 nuovaPartitaAiTrenta = new JMenuItem( main.stringaNuovaPartitaAiTrenta ); 296 nuovaPartitaAiTrenta.addActionListener( new ActionListener() { 297 /* Avvia una nuova partita ai 30, chiedendo prima conferma se 298 * ce n'è già una in corso. */ 299 public void actionPerformed( ActionEvent e ) { 300 if ( main.mano == 20 ) { 301 main.aiTrenta = true; 302 main.mischia(); 303 pannello.repaint(); 304 repaint(); 305 } 306 307 else { 308 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 ); 309 if ( selezione == JOptionPane.YES_OPTION ) { 310 main.aiTrenta = true; 311 main.mischia(); 312 pannello.repaint(); 313 repaint(); 314 } 315 } 316 } 317 }); 318 319 trucchi = new JMenuItem( main.stringaTrucchi ); 320 trucchi.addActionListener( new ActionListener() { 321 public void actionPerformed( ActionEvent e ) { 322 /* Esegue il trucco */ 323 main.trucco( JOptionPane.showInputDialog( Frame.this, main.stringaInserisciCodice, "JBriscola - " + main.version, JOptionPane.PLAIN_MESSAGE ) ); 324 } 325 } ); 326 327 opzioni = new JMenuItem( main.stringaOpzioni ); 328 opzioni.addActionListener( new ActionListener() { 329 /* Apre una OptionFrame per modificare le opzioni. */ 330 public void actionPerformed( ActionEvent e ) { 331 332 OptionFrame optionFrame = new OptionFrame(); 333 optionFrame.setDefaultCloseOperation( JFrame.DISPOSE_ON_CLOSE ); 334 optionFrame.setResizable( false ); 335 optionFrame.setAlwaysOnTop( true ); 336 optionFrame.setSize( 300, 200 ); 337 optionFrame.setLocation( (Toolkit.getDefaultToolkit().getScreenSize().width - 300) / 2, (Toolkit.getDefaultToolkit().getScreenSize().height - 150) / 2); 338 optionFrame.setVisible( true ); 339 } 340 }); 341 342 esci = new JMenuItem( main.stringaEsci ); 343 esci.addActionListener( new ActionListener() { 344 /* Ciede conferma prima di uscire. */ 345 public void actionPerformed( ActionEvent e ) { 346 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 ); 347 if ( selezione == JOptionPane.YES_OPTION ) 348 Frame.this.dispose(); 349 } 350 }); 351 352 file.add( nuovaPartita ); 353 file.add( nuovaPartitaAiTrenta ); 354 file.add( trucchi ); 355 file.add( opzioni ); 356 file.addSeparator(); 357 file.add( esci ); 358 359 puntoInterrogativo = new JMenu( "?" ); 360 361 informazioni = new JMenuItem( main.stringaInformazioni ); 362 informazioni.addActionListener( new ActionListener() { 363 /* Visualizza le informazioni. */ 364 public void actionPerformed( ActionEvent e ) { 365 JOptionPane.showMessageDialog( Frame.this, main.stringaInformazioni2, main.stringaInformazioni, JOptionPane.PLAIN_MESSAGE ); 366 } 367 } ); 368 369 tastiRapidi = new JMenuItem( main.stringaTasti ); 370 tastiRapidi.addActionListener( new ActionListener() { 371 /* Visualizza i tasti rapidi. */ 372 public void actionPerformed( ActionEvent e ) { 373 JOptionPane.showMessageDialog( Frame.this, main.stringaTasti2, "JBriscola - " + main.version, JOptionPane.INFORMATION_MESSAGE ); 374 } 375 } ); 376 377 briscola = new JMenuItem( main.stringaBriscola ); 378 briscola.addActionListener( new ActionListener() { 379 /* Visualizza il seme di briscola corrente. */ 380 public void actionPerformed( ActionEvent e ) { 381 JOptionPane.showMessageDialog( Frame.this, main.stringaBriscola + ": " + main.semi[main.semeBriscola], "JBriscola - " + main.version, JOptionPane.INFORMATION_MESSAGE ); 382 } 383 }); 384 385 punti = new JMenuItem( main.stringaPunteggio ); 386 punti.addActionListener( new ActionListener() { 387 /* Visualizza il punteggio. */ 388 public void actionPerformed( ActionEvent e ) { 389 JOptionPane.showMessageDialog( Frame.this, main.stringaGiocatore + main.punteggio + main.stringaComputer + main.punteggioCPU, main.stringaPunteggio, JOptionPane.INFORMATION_MESSAGE ); 390 } 391 } ); 392 393 aggiornamenti = new JMenuItem( main.stringaAggiornamenti ); 394 aggiornamenti.addActionListener( new ActionListener() { 395 /* Cerca aggiornamenti sul sito http://porkynator.altervista.org, 396 * confrontando la versione del programma con quella scritta nel 397 * file jbriscola-latest.is (online). */ 398 public void actionPerformed( ActionEvent e ) { 399 String latest = main.version; 400 try { 401 URL latestIs = new URL( "http://porkynator.altervista.org/jbriscola-latest.is" ); 402 BufferedReader latestIsReader = new BufferedReader( new InputStreamReader( latestIs.openStream() ) ); 403 latest = latestIsReader.readLine(); 404 } catch ( Exception ex ) { 405 JOptionPane.showMessageDialog( Frame.this, main.stringaNoInternet, "JBriscola - " + main.version, JOptionPane.ERROR_MESSAGE ); 406 } 407 if ( latest.equals( main.version ) ) 408 JOptionPane.showMessageDialog( Frame.this, main.stringaAggiornamentiNo, "JBriscola - " + main.version, JOptionPane.INFORMATION_MESSAGE ); 409 else 410 JOptionPane.showMessageDialog( Frame.this, main.stringaAggiornamentiSi + "\n" + main.version + " ==> " + latest + "\n\n" + main.stringaScarica, "JBriscola - " + main.version, JOptionPane.INFORMATION_MESSAGE ); 411 } 412 } ); 413 414 puntoInterrogativo.add( punti ); 415 puntoInterrogativo.add( briscola ); 416 puntoInterrogativo.add( tastiRapidi ); 417 puntoInterrogativo.add( informazioni ); 418 puntoInterrogativo.add( aggiornamenti ); 419 420 barraMenu.add( file ); 421 barraMenu.add( puntoInterrogativo ); 422 423 setJMenuBar( barraMenu ); 424 425 /* Inizializza le immagini del retro delle carte. */ 426 retro1 = new ImageIcon( ClassLoader.getSystemResource( "img/retro1.jpg" ) ); 427 retro2 = new ImageIcon( ClassLoader.getSystemResource( "img/retro2.jpg" ) ); 428 429 /* Inizializza tutte le immagini delle carte scorrendo l'array bidimensionale. */ 430 for (int i = 0; i < 10; i++) 431 for (int j = 0; j < 4; j++) 432 arrayCarte[i][j] = new ImageIcon( ClassLoader.getSystemResource( "img/" + (i+1) + "-" + (j+1) + ".jpg" ) ); 433 434 /* Mischia le carte. */ 435 main.mischia(); 436 437 /* Aggiunge i vari componenti. */ 438 pannello2.add( button ); 439 setLayout( new BorderLayout() ); 440 add( pannello, BorderLayout.CENTER ); 441 add( pannello2, BorderLayout.SOUTH ); 442 443 /* Chiama il metodo mettiCarta se l'utente clicca. */ 444 addMouseListener( 445 new MouseAdapter() { 446 public void mouseClicked ( MouseEvent e ) { 447 if ( mettiCarta( e.getX(), e.getY() ) ) { 448 if ( !main.CPUHaGiocato ) 449 if ( main.aiTrenta ) 450 main.giocaCPUAiTrenta(); 451 else 452 main.giocaCPU(); 453 } 454 } 455 } 456 ); 457 458 /* Chiede conferma prima di uscire quando viene chiusa la finestra. */ 459 addWindowListener( 460 new WindowAdapter() { 461 public void windowClosing( WindowEvent e ) { 462 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 ); 463 if ( selezione == JOptionPane.YES_OPTION ) 464 System.exit( 0 ); 465 } 466 } 467 ); 468 } 469 470 /* Se il giocatore ha cliccato su una carta la gioca e riproduce un suono casuale. */ 471 public boolean mettiCarta( int x, int y ) { 472 473 if ( !main.toccaAllaCPU && giocata[0] == null ) { 474 if ( main.sound && 475 ( ( x > 230 && x < 290 && y > 340 && y < 566 && carteInMano[0] != null ) || 476 ( x > 300 && x < 360 && y > 340 && y < 566 && carteInMano[1] != null ) || 477 ( x > 370 && x < 430 && y > 340 && y < 566 && carteInMano[2] != null ) ) ) { 478 try { 479 480 double a = Math.random() * 6; 481 int i = (int) a; 482 483 AudioFileFormat aff = AudioSystem.getAudioFileFormat( ClassLoader.getSystemResource("sounds/" + i + ".wav") ); 484 AudioInputStream ais = AudioSystem.getAudioInputStream( ClassLoader.getSystemResource("sounds/" + i + ".wav") ); 485 AudioFormat af = aff.getFormat(); 486 DataLine.Info info = new DataLine.Info( Clip.class, ais.getFormat(), ( (int) ais.getFrameLength() * af.getFrameSize() ) ); 487 Clip ol = ( Clip ) AudioSystem.getLine( info ); 488 489 ol.open(ais); 490 ol.loop( 0 ); 491 492 } catch ( Exception e ) { 493 System.err.println( e ); 494 } 495 } 496 497 if ( x > 230 && x < 290 && y > 340 && y < 566 && carteInMano[0] != null ) { 498 giocata[0] = carteInMano[0]; 499 carteInMano[0] = null; 500 pannello.repaint(); 501 return true; 502 } 503 504 if ( x > 300 && x < 360 && y > 340 && y < 566 && carteInMano[1] != null ) { 505 giocata[0] = carteInMano[1]; 506 carteInMano[1] = null; 507 pannello.repaint(); 508 return true; 509 } 510 511 if ( x > 370 && x < 430 && y > 340 && y < 566 && carteInMano[2] != null ) { 512 giocata[0] = carteInMano[2]; 513 carteInMano[2] = null; 514 pannello.repaint(); 515 return true; 516 } 517 } 518 519 return false; 520 } 521 } 522 523