ancient-projects

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

Frame.java (5499B)


      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, 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" );
     42 		mazzo = true;
     43 		main = new Main();
     44 		pannello = new Pannello();
     45 		button = new JButton( "Prendi/Lascia carte" );
     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, "Prima gioca!" );
     54 				}
     55 				if ( main.mano == 16 )
     56 					JOptionPane.showMessageDialog( Frame.this, "Ultime 4 mani - occhio alla briscola!" );
     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 = "Hai vinto!";
     65 					if ( main.punteggio < main.punteggioCPU )
     66 						titolo = "Hai perso";
     67 					if ( main.punteggio == main.punteggioCPU )
     68 						titolo = "Pareggio!";
     69 					JOptionPane.showMessageDialog( Frame.this, "Giocatore: " + main.punteggio + "\nComputer: " + main.punteggioCPU, titolo, JOptionPane.PLAIN_MESSAGE );
     70 				}
     71 			}
     72 		} );
     73 		barraMenu = new JMenuBar();
     74 		file = new JMenu( "File" );
     75 		nuovaPartita = new JMenuItem( "Nuova Partita" );
     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, "Vuoi davvero iniziare una nuova partita?", "JBriscola 0.1", JOptionPane.YES_NO_OPTION,  JOptionPane.QUESTION_MESSAGE, null, new String[] { "Si", "No" }, "No" );
     85 					if ( selezione == JOptionPane.YES_OPTION ) {
     86 						main.mischia();
     87 						pannello.repaint();
     88 						repaint();
     89 					}
     90 				}
     91 			}
     92 		});
     93 		esci = new JMenuItem( "Esci" );
     94 		esci.addActionListener( new ActionListener() {
     95 			public void actionPerformed( ActionEvent e ) {
     96 				int selezione = JOptionPane.showOptionDialog( Frame.this, "Vuoi davvero uscire?", "JBriscola 0.1", JOptionPane.YES_NO_OPTION,  JOptionPane.QUESTION_MESSAGE, null, new String[] { "Si", "No" }, "No" );
     97 				if ( selezione == JOptionPane.YES_OPTION )
     98 					Frame.this.dispose();
     99 			}
    100 		});
    101 		file.add( nuovaPartita );
    102 		file.add( esci );
    103 		barraMenu.add( file );
    104 		setJMenuBar( barraMenu );
    105 		retro1 = new ImageIcon( ClassLoader.getSystemResource( "img/retro1.jpg" ) );
    106 		retro2 = new ImageIcon( ClassLoader.getSystemResource( "img/retro2.jpg" ) );
    107 		for (int i = 0; i < 10; i++)
    108 			for (int j = 0; j < 4; j++)
    109 				arrayCarte[i][j] = new ImageIcon( ClassLoader.getSystemResource( "img/" + (i+1) + "-" + (j+1) + ".jpg" ) );
    110 		main.mischia();
    111 		setLayout( new BorderLayout() );
    112 		add( pannello, BorderLayout.CENTER );
    113 		add( button, BorderLayout.SOUTH );
    114 		addMouseListener(
    115 			new MouseAdapter() {
    116 				public void mouseClicked ( MouseEvent e ) {
    117 					if ( mettiCarta( e.getX(), e.getY() ) ) {
    118 						if ( !main.CPUHaGiocato )
    119 							main.giocaCPU();
    120 					}
    121 				}
    122 			}
    123 		);
    124 		addWindowListener(
    125 			new WindowAdapter() {
    126 				public void windowClosing( WindowEvent e ) {
    127 					int selezione = JOptionPane.showOptionDialog( Frame.this, "Vuoi davvero uscire?", "JBriscola 0.1", JOptionPane.YES_NO_OPTION,  JOptionPane.QUESTION_MESSAGE, null, new String[] { "Si", "No" }, "No" );
    128 					if ( selezione == JOptionPane.YES_OPTION )
    129 						System.exit( 0 );
    130 				}
    131 			}
    132 		);
    133 	}
    134 	public boolean mettiCarta( int x, int y ) {
    135 		if ( !main.toccaAllaCPU && giocata[0] == null ) {
    136 			if ( x > 230 && x < 290 && y > 340 && y < 566 ) {
    137 				giocata[0] = carteInMano[0];
    138 				carteInMano[0] = null;
    139 				pannello.repaint();
    140 				return true;
    141 			}
    142 			if ( x > 300 && x < 360 && y > 340 && y < 566 ) {
    143 				giocata[0] = carteInMano[1];
    144 				carteInMano[1] = null;
    145 				pannello.repaint();
    146 				return true;
    147 			}
    148 			if ( x > 370 && x < 430 && y > 340 && y < 566 ) {
    149 				giocata[0] = carteInMano[2];
    150 				carteInMano[2] = null;
    151 				pannello.repaint();
    152 				return true;
    153 			}
    154 		}
    155 		return false;
    156 	}
    157 }
    158 
    159