/* Copyright 2009 Sebastiano Tronto This file is part of JBriscola. JBriscola is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. JBriscola is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with JBriscola; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Frame extends JFrame { public static boolean mazzo; public static Main main; public static Pannello pannello; public static JButton button; public static JMenuBar barraMenu; public static JMenu file; public static JMenuItem nuovaPartita, opzioni, esci; public static ImageIcon retro1, retro2, ultimaCarta; public static ImageIcon[] giocata = new ImageIcon[2]; public static ImageIcon[] carteInMano = new ImageIcon[3]; public static ImageIcon[] carteCPU = new ImageIcon[3]; public static ImageIcon[] arrayCarteMescolate = new ImageIcon[40]; public static ImageIcon[][] arrayCarte = new ImageIcon[10][4]; public Frame() { super( "Tronto's JBriscola 0.1 Beta 2" ); mazzo = true; main = new Main(); pannello = new Pannello(); button = new JButton( main.stringaBottonePrendi ); button.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent e ) { if ( giocata[0] != null && giocata[1] != null ) main.mano++; try { main.chiPrende(); } catch ( Exception ex ) { JOptionPane.showMessageDialog( Frame.this, main.stringaPrimaGioca, "JBriscola 0.1", JOptionPane.WARNING_MESSAGE ); } if ( main.mano == 16 ) JOptionPane.showMessageDialog( Frame.this, main.stringaUltime4, "JBriscola 0.1", JOptionPane.INFORMATION_MESSAGE ); if ( main.mano == 17 ) { mazzo = false; ultimaCarta = null; pannello.repaint(); Frame.this.repaint(); } if ( main.mano == 20 ) { String titolo = main.stringaVinto; if ( main.punteggio < main.punteggioCPU ) titolo = main.stringaPerso; if ( main.punteggio == main.punteggioCPU ) titolo = main.stringaPareggio; JOptionPane.showMessageDialog( Frame.this, main.stringaGiocatore + main.punteggio + main.stringaComputer + main.punteggioCPU, titolo, JOptionPane.PLAIN_MESSAGE ); } } } ); barraMenu = new JMenuBar(); file = new JMenu( "File" ); nuovaPartita = new JMenuItem( main.stringaNuovaPartita ); nuovaPartita.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent e ) { if ( main.mano == 20 ) { main.mischia(); pannello.repaint(); repaint(); } else { int selezione = JOptionPane.showOptionDialog( Frame.this, main.stringaVuoiDavveroIniziare, "JBriscola 0.1", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, new String[] { main.stringaSi, main.stringaNo }, main.stringaNo ); if ( selezione == JOptionPane.YES_OPTION ) { main.mischia(); pannello.repaint(); repaint(); } } } }); opzioni = new JMenuItem( main.stringaOpzioni ); opzioni.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent e ) { OptionFrame optionFrame = new OptionFrame(); optionFrame.setDefaultCloseOperation( JFrame.DISPOSE_ON_CLOSE ); optionFrame.setResizable( false ); optionFrame.setAlwaysOnTop( true ); optionFrame.setSize( 300, 150 ); optionFrame.setLocation( (Toolkit.getDefaultToolkit().getScreenSize().width - 300) / 2, (Toolkit.getDefaultToolkit().getScreenSize().height - 150) / 2); optionFrame.setVisible( true ); } }); esci = new JMenuItem( main.stringaEsci ); esci.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent e ) { int selezione = JOptionPane.showOptionDialog( Frame.this, main.stringaVuoiDavveroUscire, "JBriscola 0.1", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, new String[] { main.stringaSi, main.stringaNo }, main.stringaNo ); if ( selezione == JOptionPane.YES_OPTION ) Frame.this.dispose(); } }); file.add( nuovaPartita ); file.add( opzioni ); file.addSeparator(); file.add( esci ); barraMenu.add( file ); setJMenuBar( barraMenu ); retro1 = new ImageIcon( ClassLoader.getSystemResource( "img/retro1.jpg" ) ); retro2 = new ImageIcon( ClassLoader.getSystemResource( "img/retro2.jpg" ) ); for (int i = 0; i < 10; i++) for (int j = 0; j < 4; j++) arrayCarte[i][j] = new ImageIcon( ClassLoader.getSystemResource( "img/" + (i+1) + "-" + (j+1) + ".jpg" ) ); main.mischia(); setLayout( new BorderLayout() ); add( pannello, BorderLayout.CENTER ); add( button, BorderLayout.SOUTH ); addMouseListener( new MouseAdapter() { public void mouseClicked ( MouseEvent e ) { if ( mettiCarta( e.getX(), e.getY() ) ) { if ( !main.CPUHaGiocato ) main.giocaCPU(); } } } ); addWindowListener( new WindowAdapter() { public void windowClosing( WindowEvent e ) { int selezione = JOptionPane.showOptionDialog( Frame.this, main.stringaVuoiDavveroUscire, "JBriscola 0.1", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, new String[] { main.stringaSi, main.stringaNo }, main.stringaNo ); if ( selezione == JOptionPane.YES_OPTION ) System.exit( 0 ); } } ); } public boolean mettiCarta( int x, int y ) { if ( !main.toccaAllaCPU && giocata[0] == null ) { if ( x > 230 && x < 290 && y > 340 && y < 566 ) { giocata[0] = carteInMano[0]; carteInMano[0] = null; pannello.repaint(); return true; } if ( x > 300 && x < 360 && y > 340 && y < 566 ) { giocata[0] = carteInMano[1]; carteInMano[1] = null; pannello.repaint(); return true; } if ( x > 370 && x < 430 && y > 340 && y < 566 ) { giocata[0] = carteInMano[2]; carteInMano[2] = null; pannello.repaint(); return true; } } return false; } }