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