ancient-projects

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

Frame.java (4008B)


      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 ImageIcon retro1, retro2, ultimaCarta;
     32 	public static ImageIcon[] giocata = new ImageIcon[2];
     33 	public static ImageIcon[] carteInMano = new ImageIcon[3];
     34 	public static ImageIcon[] carteCPU = new ImageIcon[3];
     35 	public static ImageIcon[] arrayCarteMescolate = new ImageIcon[40];
     36 	public static ImageIcon[][] arrayCarte = new ImageIcon[10][4];
     37 	public Frame() {
     38 		super( "Tronto's JBriscola 0.1" );
     39 		mazzo = true;
     40 		main = new Main();
     41 		pannello = new Pannello();
     42 		button = new JButton( "Prendi/Lascia carte" );
     43 		button.addActionListener( new ActionListener() {
     44 			public void actionPerformed( ActionEvent e ) {
     45 				if ( giocata[0] != null && giocata[1] != null )
     46 					main.mano++;
     47 				else
     48 					JOptionPane.showMessageDialog( Frame.this, "Prima gioca!" );
     49 				try {
     50 					main.chiPrende();
     51 				} catch ( Exception ex ) {
     52 					JOptionPane.showMessageDialog( Frame.this, "Prima gioca!" );
     53 				}
     54 				if ( main.mano == 16 )
     55 					JOptionPane.showMessageDialog( Frame.this, "Ultime 4 mani - occhio alla briscola!" );
     56 				if ( main.mano == 17 ) {
     57 					mazzo = false;
     58 					ultimaCarta = null;
     59 					pannello.repaint();
     60 					Frame.this.repaint();
     61 				}
     62 				if ( main.mano == 20 )
     63 					JOptionPane.showMessageDialog( Frame.this, "Giocatore: " + main.punteggio + "\nComputer: " + main.punteggioCPU + "\n\nMa grazie tante, il computer gioca a caso!\nNon serve che ti gasi tanto." );
     64 			}
     65 		} );
     66 		retro1 = new ImageIcon( ClassLoader.getSystemResource( "img/retro1.jpg" ) );
     67 		retro2 = new ImageIcon( ClassLoader.getSystemResource( "img/retro2.jpg" ) );
     68 		for (int i = 0; i < 10; i++)
     69 			for (int j = 0; j < 4; j++)
     70 				arrayCarte[i][j] = new ImageIcon( ClassLoader.getSystemResource( "img/" + (i+1) + "-" + (j+1) + ".jpg" ) );
     71 		main.mischia();
     72 		carteInMano[0] = arrayCarteMescolate[0];
     73 		carteInMano[1] = arrayCarteMescolate[1];
     74 		carteInMano[2] = arrayCarteMescolate[2];
     75 		carteCPU[0] = arrayCarteMescolate[3];
     76 		carteCPU[1] = arrayCarteMescolate[4];
     77 		carteCPU[2] = arrayCarteMescolate[5];
     78 		ultimaCarta = arrayCarteMescolate[39];
     79 		setLayout( new BorderLayout() );
     80 		add( pannello, BorderLayout.CENTER );
     81 		add( button, BorderLayout.SOUTH );
     82 		addMouseListener(
     83 			new MouseAdapter() {
     84 				public void mouseClicked ( MouseEvent e ) {
     85 					if ( mettiCarta( e.getX(), e.getY() ) ) {
     86 						if ( !main.CPUHaGiocato )
     87 							main.giocaCPU();
     88 					}
     89 				}
     90 			}
     91 		);
     92 	}
     93 	public boolean mettiCarta( int x, int y ) {
     94 		if ( !main.toccaAllaCPU && giocata[0] == null ) {
     95 			if ( x > 230 && x < 290 && y > 340 && y < 566 ) {
     96 				giocata[0] = carteInMano[0];
     97 				carteInMano[0] = null;
     98 				pannello.repaint();
     99 				return true;
    100 			}
    101 			if ( x > 300 && x < 360 && y > 340 && y < 566 ) {
    102 				giocata[0] = carteInMano[1];
    103 				carteInMano[1] = null;
    104 				pannello.repaint();
    105 				return true;
    106 			}
    107 			if ( x > 370 && x < 430 && y > 340 && y < 566 ) {
    108 				giocata[0] = carteInMano[2];
    109 				carteInMano[2] = null;
    110 				pannello.repaint();
    111 				return true;
    112 			}
    113 		}
    114 		return false;
    115 	}
    116 }
    117 
    118