ancient-projects

My earliest programs (2006-2010)
git clone https://git.tronto.net/ancient-projects
Download | Log | Files | Refs | README

Frame.java (7034B)


      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 java.awt.*;
     24 import java.awt.event.*;
     25 
     26 public class Frame extends JFrame {
     27 	public static boolean mazzo;
     28 	public static Main main;
     29 	public static Pannello pannello;
     30 	public static JPanel pannello2;
     31 	public static JButton button;
     32 	public static JMenuBar barraMenu;
     33 	public static JMenu file, puntoInterrogativo;
     34 	public static JMenuItem nuovaPartita, opzioni, esci, informazioni;
     35 	public static ImageIcon retro1, retro2, ultimaCarta;
     36 	public static ImageIcon[] giocata = new ImageIcon[2];
     37 	public static ImageIcon[] carteInMano = new ImageIcon[3];
     38 	public static ImageIcon[] carteCPU = new ImageIcon[3];
     39 	public static ImageIcon[] arrayCarteMescolate = new ImageIcon[40];
     40 	public static ImageIcon[][] arrayCarte = new ImageIcon[10][4];
     41 	public Frame() {
     42 		super( "Tronto's JBriscola " + main.version );
     43 		mazzo = true;
     44 		main = new Main();
     45 		pannello = new Pannello();
     46 		pannello2 = new JPanel( new FlowLayout() );
     47 		button = new JButton( main.stringaBottonePrendi );
     48 		button.addActionListener( new ActionListener() {
     49 			public void actionPerformed( ActionEvent e ) {
     50 				if ( giocata[0] != null && giocata[1] != null )
     51 					main.mano++;
     52 				try {
     53 					main.chiPrende();
     54 				} catch ( Exception ex ) {
     55 					JOptionPane.showMessageDialog( Frame.this, main.stringaPrimaGioca, "JBriscola " + main.version, JOptionPane.WARNING_MESSAGE );
     56 				}
     57 				if ( main.mano == 16 )
     58 					JOptionPane.showMessageDialog( Frame.this, main.stringaUltime4, "JBriscola " + main.version, JOptionPane.INFORMATION_MESSAGE );
     59 				if ( main.mano == 17 ) {
     60 					mazzo = false;
     61 					ultimaCarta = null;
     62 					pannello.repaint();
     63 					Frame.this.repaint();
     64 				}
     65 				if ( main.mano == 20 ) {
     66 					String titolo = main.stringaVinto;
     67 					if ( main.punteggio < main.punteggioCPU )
     68 						titolo = main.stringaPerso;
     69 					if ( main.punteggio == main.punteggioCPU )
     70 						titolo = main.stringaPareggio;
     71 					JOptionPane.showMessageDialog( Frame.this, main.stringaGiocatore + main.punteggio + main.stringaComputer + main.punteggioCPU, titolo, JOptionPane.PLAIN_MESSAGE );
     72 				}
     73 			}
     74 		} );
     75 		barraMenu = new JMenuBar();
     76 		file = new JMenu( "File" );
     77 		nuovaPartita = new JMenuItem( main.stringaNuovaPartita );
     78 		nuovaPartita.addActionListener( new ActionListener() {
     79 			public void actionPerformed( ActionEvent e ) {
     80 				if ( main.mano == 20 ) {
     81 					main.mischia();
     82 					pannello.repaint();
     83 					repaint();
     84 				}
     85 				else {
     86 					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 );
     87 					if ( selezione == JOptionPane.YES_OPTION ) {
     88 						main.mischia();
     89 						pannello.repaint();
     90 						repaint();
     91 					}
     92 				}
     93 			}
     94 		});
     95 		opzioni = new JMenuItem( main.stringaOpzioni );
     96 		opzioni.addActionListener( new ActionListener() {
     97 			public void actionPerformed( ActionEvent e ) {
     98 				OptionFrame optionFrame = new OptionFrame();
     99 				optionFrame.setDefaultCloseOperation( JFrame.DISPOSE_ON_CLOSE );
    100 				optionFrame.setResizable( false );
    101 				optionFrame.setAlwaysOnTop( true );
    102 				optionFrame.setSize( 300, 150 );
    103 				optionFrame.setLocation( (Toolkit.getDefaultToolkit().getScreenSize().width - 300) / 2,
    104 									(Toolkit.getDefaultToolkit().getScreenSize().height - 150) / 2);
    105 				optionFrame.setVisible( true );
    106 			}
    107 		});
    108 		esci = new JMenuItem( main.stringaEsci );
    109 		esci.addActionListener( new ActionListener() {
    110 			public void actionPerformed( ActionEvent e ) {
    111 				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 );
    112 				if ( selezione == JOptionPane.YES_OPTION )
    113 					Frame.this.dispose();
    114 			}
    115 		});
    116 		file.add( nuovaPartita );
    117 		file.add( opzioni );
    118 		file.addSeparator();
    119 		file.add( esci );
    120 		puntoInterrogativo = new JMenu( "?" );
    121 		informazioni = new JMenuItem( main.stringaInformazioni );
    122 		informazioni.addActionListener( new ActionListener() {
    123 			public void actionPerformed( ActionEvent e ) {
    124 				JOptionPane.showMessageDialog( Frame.this, main.stringaInformazioni2, main.stringaInformazioni, JOptionPane.PLAIN_MESSAGE );
    125 			}
    126 		} );
    127 		puntoInterrogativo.add( informazioni );
    128 		barraMenu.add( file );
    129 		barraMenu.add( puntoInterrogativo );
    130 		setJMenuBar( barraMenu );
    131 		retro1 = new ImageIcon( ClassLoader.getSystemResource( "img/retro1.jpg" ) );
    132 		retro2 = new ImageIcon( ClassLoader.getSystemResource( "img/retro2.jpg" ) );
    133 		for (int i = 0; i < 10; i++)
    134 			for (int j = 0; j < 4; j++)
    135 				arrayCarte[i][j] = new ImageIcon( ClassLoader.getSystemResource( "img/" + (i+1) + "-" + (j+1) + ".jpg" ) );
    136 		main.mischia();
    137 		pannello2.add( button );
    138 		setLayout( new BorderLayout() );
    139 		add( pannello, BorderLayout.CENTER );
    140 		add( pannello2, BorderLayout.SOUTH );
    141 		addMouseListener(
    142 			new MouseAdapter() {
    143 				public void mouseClicked ( MouseEvent e ) {
    144 					if ( mettiCarta( e.getX(), e.getY() ) ) {
    145 						if ( !main.CPUHaGiocato )
    146 							main.giocaCPU();
    147 					}
    148 				}
    149 			}
    150 		);
    151 		addWindowListener(
    152 			new WindowAdapter() {
    153 				public void windowClosing( WindowEvent e ) {
    154 					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 );
    155 					if ( selezione == JOptionPane.YES_OPTION )
    156 						System.exit( 0 );
    157 				}
    158 			}
    159 		);
    160 	}
    161 	public boolean mettiCarta( int x, int y ) {
    162 		if ( !main.toccaAllaCPU && giocata[0] == null ) {
    163 			if ( x > 230 && x < 290 && y > 340 && y < 566 ) {
    164 				giocata[0] = carteInMano[0];
    165 				carteInMano[0] = null;
    166 				pannello.repaint();
    167 				return true;
    168 			}
    169 			if ( x > 300 && x < 360 && y > 340 && y < 566 ) {
    170 				giocata[0] = carteInMano[1];
    171 				carteInMano[1] = null;
    172 				pannello.repaint();
    173 				return true;
    174 			}
    175 			if ( x > 370 && x < 430 && y > 340 && y < 566 ) {
    176 				giocata[0] = carteInMano[2];
    177 				carteInMano[2] = null;
    178 				pannello.repaint();
    179 				return true;
    180 			}
    181 		}
    182 		return false;
    183 	}
    184 }
    185 
    186