ancient-projects

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

Frame.java (6394B)


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