Frame.java (9042B)
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 File suono = new File( ClassLoader.getSystemResource( suonoFinale ).toURI() ); 85 AudioFileFormat aff = AudioSystem.getAudioFileFormat( suono ); 86 AudioInputStream ais = AudioSystem.getAudioInputStream( suono ); 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 barraMenu = new JMenuBar(); 105 file = new JMenu( "File" ); 106 nuovaPartita = new JMenuItem( main.stringaNuovaPartita ); 107 nuovaPartita.addActionListener( new ActionListener() { 108 public void actionPerformed( ActionEvent e ) { 109 if ( main.mano == 20 ) { 110 main.mischia(); 111 pannello.repaint(); 112 repaint(); 113 } 114 else { 115 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 ); 116 if ( selezione == JOptionPane.YES_OPTION ) { 117 main.mischia(); 118 pannello.repaint(); 119 repaint(); 120 } 121 } 122 } 123 }); 124 opzioni = new JMenuItem( main.stringaOpzioni ); 125 opzioni.addActionListener( new ActionListener() { 126 public void actionPerformed( ActionEvent e ) { 127 OptionFrame optionFrame = new OptionFrame(); 128 optionFrame.setDefaultCloseOperation( JFrame.DISPOSE_ON_CLOSE ); 129 optionFrame.setResizable( false ); 130 optionFrame.setAlwaysOnTop( true ); 131 optionFrame.setSize( 300, 150 ); 132 optionFrame.setLocation( (Toolkit.getDefaultToolkit().getScreenSize().width - 300) / 2, 133 (Toolkit.getDefaultToolkit().getScreenSize().height - 150) / 2); 134 optionFrame.setVisible( true ); 135 } 136 }); 137 esci = new JMenuItem( main.stringaEsci ); 138 esci.addActionListener( new ActionListener() { 139 public void actionPerformed( ActionEvent e ) { 140 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 ); 141 if ( selezione == JOptionPane.YES_OPTION ) 142 Frame.this.dispose(); 143 } 144 }); 145 file.add( nuovaPartita ); 146 file.add( opzioni ); 147 file.addSeparator(); 148 file.add( esci ); 149 puntoInterrogativo = new JMenu( "?" ); 150 informazioni = new JMenuItem( main.stringaInformazioni ); 151 informazioni.addActionListener( new ActionListener() { 152 public void actionPerformed( ActionEvent e ) { 153 JOptionPane.showMessageDialog( Frame.this, main.stringaInformazioni2, main.stringaInformazioni, JOptionPane.PLAIN_MESSAGE ); 154 } 155 } ); 156 punti = new JMenuItem( main.stringaPunteggio ); 157 punti.addActionListener( new ActionListener() { 158 public void actionPerformed( ActionEvent e ) { 159 JOptionPane.showMessageDialog( Frame.this, main.stringaGiocatore + main.punteggio + "\n" + main.stringaComputer + main.punteggioCPU, main.stringaPunteggio, JOptionPane.PLAIN_MESSAGE ); 160 } 161 } ); 162 puntoInterrogativo.add( punti ); 163 puntoInterrogativo.add( informazioni ); 164 barraMenu.add( file ); 165 barraMenu.add( puntoInterrogativo ); 166 setJMenuBar( barraMenu ); 167 retro1 = new ImageIcon( ClassLoader.getSystemResource( "img/retro1.jpg" ) ); 168 retro2 = new ImageIcon( ClassLoader.getSystemResource( "img/retro2.jpg" ) ); 169 for (int i = 0; i < 10; i++) 170 for (int j = 0; j < 4; j++) 171 arrayCarte[i][j] = new ImageIcon( ClassLoader.getSystemResource( "img/" + (i+1) + "-" + (j+1) + ".jpg" ) ); 172 main.mischia(); 173 pannello2.add( button ); 174 setLayout( new BorderLayout() ); 175 add( pannello, BorderLayout.CENTER ); 176 add( pannello2, BorderLayout.SOUTH ); 177 addMouseListener( 178 new MouseAdapter() { 179 public void mouseClicked ( MouseEvent e ) { 180 if ( mettiCarta( e.getX(), e.getY() ) ) { 181 if ( !main.CPUHaGiocato ) 182 main.giocaCPU(); 183 } 184 } 185 } 186 ); 187 addWindowListener( 188 new WindowAdapter() { 189 public void windowClosing( WindowEvent e ) { 190 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 ); 191 if ( selezione == JOptionPane.YES_OPTION ) 192 System.exit( 0 ); 193 } 194 } 195 ); 196 } 197 public boolean mettiCarta( int x, int y ) { 198 if ( !main.toccaAllaCPU && giocata[0] == null ) { 199 if ( main.sound ) { 200 try { 201 double a = Math.random() * 6; 202 int i = (int) a; 203 File suono = new File( ClassLoader.getSystemResource( "sounds/" + i + ".wav" ).toURI() ); 204 AudioFileFormat aff = AudioSystem.getAudioFileFormat( suono ); 205 AudioInputStream ais = AudioSystem.getAudioInputStream( suono ); 206 AudioFormat af = aff.getFormat(); 207 DataLine.Info info = new DataLine.Info( 208 Clip.class, 209 ais.getFormat(), 210 ( (int) ais.getFrameLength() * 211 af.getFrameSize() ) ); 212 Clip ol = ( Clip ) AudioSystem.getLine( info ); 213 ol.open(ais); 214 ol.loop( 0 ); 215 } catch ( Exception e ) { 216 System.err.println( e ); 217 } 218 } 219 if ( x > 230 && x < 290 && y > 340 && y < 566 ) { 220 giocata[0] = carteInMano[0]; 221 carteInMano[0] = null; 222 pannello.repaint(); 223 return true; 224 } 225 if ( x > 300 && x < 360 && y > 340 && y < 566 ) { 226 giocata[0] = carteInMano[1]; 227 carteInMano[1] = null; 228 pannello.repaint(); 229 return true; 230 } 231 if ( x > 370 && x < 430 && y > 340 && y < 566 ) { 232 giocata[0] = carteInMano[2]; 233 carteInMano[2] = null; 234 pannello.repaint(); 235 return true; 236 } 237 } 238 return false; 239 } 240 } 241 242