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