Frame.java (14411B)
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 public static boolean mazzo; 31 public static Main main; 32 public static Pannello pannello; 33 public static JPanel pannello2; 34 public static JButton button; 35 public static JMenuBar barraMenu; 36 public static JMenu file, puntoInterrogativo; 37 public static JMenuItem nuovaPartita, opzioni, esci, informazioni, punti, aggiornamenti, tastiRapidi; 38 public static ImageIcon retro1, retro2, ultimaCarta; 39 public static ImageIcon[] giocata = new ImageIcon[2]; 40 public static ImageIcon[] carteInMano = new ImageIcon[3]; 41 public static ImageIcon[] carteCPU = new ImageIcon[3]; 42 public static ImageIcon[] arrayCarteMescolate = new ImageIcon[40]; 43 public static ImageIcon[][] arrayCarte = new ImageIcon[10][4]; 44 public Frame() { 45 super( "Tronto's JBriscola " + main.version ); 46 mazzo = true; 47 main = new Main(); 48 pannello = new Pannello(); 49 pannello2 = new JPanel( new FlowLayout() ); 50 button = new JButton( main.stringaBottonePrendi ); 51 button.addActionListener( new ActionListener() { 52 public void actionPerformed( ActionEvent e ) { 53 if ( giocata[0] != null && giocata[1] != null ) 54 main.mano++; 55 try { 56 main.chiPrende(); 57 } catch ( Exception ex ) { 58 JOptionPane.showMessageDialog( Frame.this, main.stringaPrimaGioca, "JBriscola " + main.version, JOptionPane.WARNING_MESSAGE ); 59 } 60 if ( main.mano == 16 ) 61 JOptionPane.showMessageDialog( Frame.this, main.stringaUltime4, "JBriscola " + main.version, JOptionPane.INFORMATION_MESSAGE ); 62 if ( main.mano == 17 ) { 63 mazzo = false; 64 ultimaCarta = null; 65 pannello.repaint(); 66 Frame.this.repaint(); 67 } 68 if ( main.mano == 20 ) { 69 String titolo = ""; 70 String suonoFinale = ""; 71 if ( main.punteggio > main.punteggioCPU ) { 72 titolo = main.stringaVinto; 73 suonoFinale = "sounds/perso.wav"; 74 } 75 if ( main.punteggio < main.punteggioCPU ) { 76 titolo = main.stringaPerso; 77 suonoFinale = "sounds/vinto.wav"; 78 } 79 if ( main.punteggio == main.punteggioCPU ) { 80 titolo = main.stringaPareggio; 81 suonoFinale = "sounds/pareggio.wav"; 82 } 83 if ( main.sound ) { 84 try { 85 AudioFileFormat aff = AudioSystem.getAudioFileFormat( ClassLoader.getSystemResource( suonoFinale ) ); 86 AudioInputStream ais = AudioSystem.getAudioInputStream( ClassLoader.getSystemResource( suonoFinale ) ); 87 AudioFormat af = aff.getFormat(); 88 DataLine.Info info = new DataLine.Info( 89 Clip.class, 90 ais.getFormat(), 91 ( (int) ais.getFrameLength() * 92 af.getFrameSize() ) ); 93 Clip ol = ( Clip ) AudioSystem.getLine( info ); 94 ol.open(ais); 95 ol.loop( 0 ); 96 } catch ( Exception ex ) { 97 System.err.println( e ); 98 } 99 } 100 JOptionPane.showMessageDialog( Frame.this, main.stringaGiocatore + main.punteggio + main.stringaComputer + main.punteggioCPU, titolo, JOptionPane.PLAIN_MESSAGE ); 101 } 102 } 103 } ); 104 button.addKeyListener( new KeyAdapter() { 105 public void keyReleased( KeyEvent key ) { 106 if ( main.keys ) { 107 switch ( key.getKeyCode() ) { 108 case KeyEvent.VK_1: 109 if ( mettiCarta( 231, 500 ) ) { 110 if ( !main.CPUHaGiocato ) 111 main.giocaCPU(); 112 } 113 break; 114 case KeyEvent.VK_2: 115 if ( mettiCarta( 301, 500 ) ) { 116 if ( !main.CPUHaGiocato ) 117 main.giocaCPU(); 118 } 119 break; 120 case KeyEvent.VK_3: 121 if ( mettiCarta( 371, 500 ) ) { 122 if ( !main.CPUHaGiocato ) 123 main.giocaCPU(); 124 } 125 break; 126 case KeyEvent.VK_F1: 127 JOptionPane.showMessageDialog( Frame.this, main.stringaTasti2, "JBriscola - " + main.version, JOptionPane.INFORMATION_MESSAGE ); 128 break; 129 case KeyEvent.VK_F2: 130 if ( main.mano == 20 ) { 131 main.mischia(); 132 pannello.repaint(); 133 repaint(); 134 } 135 else { 136 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 ); 137 if ( selezione == JOptionPane.YES_OPTION ) { 138 main.mischia(); 139 pannello.repaint(); 140 repaint(); 141 } 142 } 143 break; 144 case KeyEvent.VK_F3: 145 OptionFrame optionFrame = new OptionFrame(); 146 optionFrame.setDefaultCloseOperation( JFrame.DISPOSE_ON_CLOSE ); 147 optionFrame.setResizable( false ); 148 optionFrame.setAlwaysOnTop( true ); 149 optionFrame.setSize( 300, 200 ); 150 optionFrame.setLocation( (Toolkit.getDefaultToolkit().getScreenSize().width - 300) / 2, 151 (Toolkit.getDefaultToolkit().getScreenSize().height - 150) / 2); 152 optionFrame.setVisible( true ); 153 break; 154 case KeyEvent.VK_F4: 155 JOptionPane.showMessageDialog( Frame.this, main.stringaGiocatore + main.punteggio + "\n" + main.stringaComputer + main.punteggioCPU, main.stringaPunteggio, JOptionPane.PLAIN_MESSAGE ); 156 break; 157 case KeyEvent.VK_F5: 158 JOptionPane.showMessageDialog( Frame.this, main.stringaInformazioni2, main.stringaInformazioni, JOptionPane.PLAIN_MESSAGE ); 159 break; 160 case KeyEvent.VK_F6: 161 String latest = main.version; 162 try { 163 URL latestIs = new URL( "http://porkynator.altervista.org/jbriscola-latest.is" ); 164 BufferedReader latestIsReader = new BufferedReader( new InputStreamReader( latestIs.openStream() ) ); 165 latest = latestIsReader.readLine(); 166 } catch ( Exception ex ) { 167 JOptionPane.showMessageDialog( Frame.this, main.stringaNoInternet, "JBriscola - " + main.version, JOptionPane.ERROR_MESSAGE ); 168 } 169 if ( latest.equals( main.version ) ) 170 JOptionPane.showMessageDialog( Frame.this, main.stringaAggiornamentiNo, "JBriscola - " + main.version, JOptionPane.INFORMATION_MESSAGE ); 171 else 172 JOptionPane.showMessageDialog( Frame.this, main.stringaAggiornamentiSi + "\n" + main.version + " ==> " + latest + "\n\n" + main.stringaScarica, "JBriscola - " + main.version, JOptionPane.INFORMATION_MESSAGE ); 173 break; 174 case KeyEvent.VK_Q: 175 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 ); 176 if ( selezione == JOptionPane.YES_OPTION ) 177 Frame.this.dispose(); 178 break; 179 } 180 } 181 } 182 } ); 183 barraMenu = new JMenuBar(); 184 file = new JMenu( "File" ); 185 nuovaPartita = new JMenuItem( main.stringaNuovaPartita ); 186 nuovaPartita.addActionListener( new ActionListener() { 187 public void actionPerformed( ActionEvent e ) { 188 if ( main.mano == 20 ) { 189 main.mischia(); 190 pannello.repaint(); 191 repaint(); 192 } 193 else { 194 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 ); 195 if ( selezione == JOptionPane.YES_OPTION ) { 196 main.mischia(); 197 pannello.repaint(); 198 repaint(); 199 } 200 } 201 } 202 }); 203 opzioni = new JMenuItem( main.stringaOpzioni ); 204 opzioni.addActionListener( new ActionListener() { 205 public void actionPerformed( ActionEvent e ) { 206 OptionFrame optionFrame = new OptionFrame(); 207 optionFrame.setDefaultCloseOperation( JFrame.DISPOSE_ON_CLOSE ); 208 optionFrame.setResizable( false ); 209 optionFrame.setAlwaysOnTop( true ); 210 optionFrame.setSize( 300, 200 ); 211 optionFrame.setLocation( (Toolkit.getDefaultToolkit().getScreenSize().width - 300) / 2, 212 (Toolkit.getDefaultToolkit().getScreenSize().height - 150) / 2); 213 optionFrame.setVisible( true ); 214 } 215 }); 216 esci = new JMenuItem( main.stringaEsci ); 217 esci.addActionListener( new ActionListener() { 218 public void actionPerformed( ActionEvent e ) { 219 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 ); 220 if ( selezione == JOptionPane.YES_OPTION ) 221 Frame.this.dispose(); 222 } 223 }); 224 file.add( nuovaPartita ); 225 file.add( opzioni ); 226 file.addSeparator(); 227 file.add( esci ); 228 puntoInterrogativo = new JMenu( "?" ); 229 informazioni = new JMenuItem( main.stringaInformazioni ); 230 informazioni.addActionListener( new ActionListener() { 231 public void actionPerformed( ActionEvent e ) { 232 JOptionPane.showMessageDialog( Frame.this, main.stringaInformazioni2, main.stringaInformazioni, JOptionPane.PLAIN_MESSAGE ); 233 } 234 } ); 235 tastiRapidi = new JMenuItem( main.stringaTasti ); 236 tastiRapidi.addActionListener( new ActionListener() { 237 public void actionPerformed( ActionEvent e ) { 238 JOptionPane.showMessageDialog( Frame.this, main.stringaTasti2, "JBriscola - " + main.version, JOptionPane.INFORMATION_MESSAGE ); 239 } 240 } ); 241 punti = new JMenuItem( main.stringaPunteggio ); 242 punti.addActionListener( new ActionListener() { 243 public void actionPerformed( ActionEvent e ) { 244 JOptionPane.showMessageDialog( Frame.this, main.stringaGiocatore + main.punteggio + "\n" + main.stringaComputer + main.punteggioCPU, main.stringaPunteggio, JOptionPane.PLAIN_MESSAGE ); 245 } 246 } ); 247 aggiornamenti = new JMenuItem( main.stringaAggiornamenti ); 248 aggiornamenti.addActionListener( new ActionListener() { 249 public void actionPerformed( ActionEvent e ) { 250 String latest = main.version; 251 try { 252 URL latestIs = new URL( "http://porkynator.altervista.org/jbriscola-latest.is" ); 253 BufferedReader latestIsReader = new BufferedReader( new InputStreamReader( latestIs.openStream() ) ); 254 latest = latestIsReader.readLine(); 255 } catch ( Exception ex ) { 256 JOptionPane.showMessageDialog( Frame.this, main.stringaNoInternet, "JBriscola - " + main.version, JOptionPane.ERROR_MESSAGE ); 257 } 258 if ( latest.equals( main.version ) ) 259 JOptionPane.showMessageDialog( Frame.this, main.stringaAggiornamentiNo, "JBriscola - " + main.version, JOptionPane.INFORMATION_MESSAGE ); 260 else 261 JOptionPane.showMessageDialog( Frame.this, main.stringaAggiornamentiSi + "\n" + main.version + " ==> " + latest + "\n\n" + main.stringaScarica, "JBriscola - " + main.version, JOptionPane.INFORMATION_MESSAGE ); 262 } 263 } ); 264 puntoInterrogativo.add( punti ); 265 puntoInterrogativo.add( tastiRapidi ); 266 puntoInterrogativo.add( informazioni ); 267 puntoInterrogativo.add( aggiornamenti ); 268 barraMenu.add( file ); 269 barraMenu.add( puntoInterrogativo ); 270 setJMenuBar( barraMenu ); 271 retro1 = new ImageIcon( ClassLoader.getSystemResource( "img/retro1.jpg" ) ); 272 retro2 = new ImageIcon( ClassLoader.getSystemResource( "img/retro2.jpg" ) ); 273 for (int i = 0; i < 10; i++) 274 for (int j = 0; j < 4; j++) 275 arrayCarte[i][j] = new ImageIcon( ClassLoader.getSystemResource( "img/" + (i+1) + "-" + (j+1) + ".jpg" ) ); 276 main.mischia(); 277 pannello2.add( button ); 278 setLayout( new BorderLayout() ); 279 add( pannello, BorderLayout.CENTER ); 280 add( pannello2, BorderLayout.SOUTH ); 281 addMouseListener( 282 new MouseAdapter() { 283 public void mouseClicked ( MouseEvent e ) { 284 if ( mettiCarta( e.getX(), e.getY() ) ) { 285 if ( !main.CPUHaGiocato ) 286 main.giocaCPU(); 287 } 288 } 289 } 290 ); 291 addWindowListener( 292 new WindowAdapter() { 293 public void windowClosing( WindowEvent e ) { 294 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 ); 295 if ( selezione == JOptionPane.YES_OPTION ) 296 System.exit( 0 ); 297 } 298 } 299 ); 300 } 301 public boolean mettiCarta( int x, int y ) { 302 if ( !main.toccaAllaCPU && giocata[0] == null ) { 303 if ( main.sound && 304 ( ( x > 230 && x < 290 && y > 340 && y < 566 && carteInMano[0] != null ) || 305 ( x > 300 && x < 360 && y > 340 && y < 566 && carteInMano[1] != null ) || 306 ( x > 370 && x < 430 && y > 340 && y < 566 && carteInMano[2] != null ) ) ) { 307 try { 308 double a = Math.random() * 6; 309 int i = (int) a; 310 AudioFileFormat aff = AudioSystem.getAudioFileFormat( ClassLoader.getSystemResource("sounds/" + i + ".wav") ); 311 AudioInputStream ais = AudioSystem.getAudioInputStream( ClassLoader.getSystemResource("sounds/" + i + ".wav") ); 312 AudioFormat af = aff.getFormat(); 313 DataLine.Info info = new DataLine.Info( 314 Clip.class, 315 ais.getFormat(), 316 ( (int) ais.getFrameLength() * 317 af.getFrameSize() ) ); 318 Clip ol = ( Clip ) AudioSystem.getLine( info ); 319 ol.open(ais); 320 ol.loop( 0 ); 321 } catch ( Exception e ) { 322 System.err.println( e ); 323 } 324 } 325 if ( x > 230 && x < 290 && y > 340 && y < 566 && carteInMano[0] != null ) { 326 giocata[0] = carteInMano[0]; 327 carteInMano[0] = null; 328 pannello.repaint(); 329 return true; 330 } 331 if ( x > 300 && x < 360 && y > 340 && y < 566 && carteInMano[1] != null ) { 332 giocata[0] = carteInMano[1]; 333 carteInMano[1] = null; 334 pannello.repaint(); 335 return true; 336 } 337 if ( x > 370 && x < 430 && y > 340 && y < 566 && carteInMano[2] != null ) { 338 giocata[0] = carteInMano[2]; 339 carteInMano[2] = null; 340 pannello.repaint(); 341 return true; 342 } 343 } 344 return false; 345 } 346 } 347 348