ancient-projects

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

Frame.java (9201B)


      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 javax.sound.sampled.*;
     24 import java.io.*;
     25 import java.awt.*;
     26 import java.awt.event.*;
     27 
     28 public class Frame extends JFrame {
     29 	public static boolean mazzo;
     30 	public static Main main;
     31 	public static Pannello pannello;
     32 	public static JPanel pannello2;
     33 	public static JButton button;
     34 	public static JMenuBar barraMenu;
     35 	public static JMenu file, puntoInterrogativo;
     36 	public static JMenuItem nuovaPartita, opzioni, esci, informazioni, punti;
     37 	public static ImageIcon retro1, retro2, ultimaCarta;
     38 	public static ImageIcon[] giocata = new ImageIcon[2];
     39 	public static ImageIcon[] carteInMano = new ImageIcon[3];
     40 	public static ImageIcon[] carteCPU = new ImageIcon[3];
     41 	public static ImageIcon[] arrayCarteMescolate = new ImageIcon[40];
     42 	public static ImageIcon[][] arrayCarte = new ImageIcon[10][4];
     43 	public Frame() {
     44 		super( "Tronto's JBriscola " + main.version );
     45 		mazzo = true;
     46 		main = new Main();
     47 		pannello = new Pannello();
     48 		pannello2 = new JPanel( new FlowLayout() );
     49 		button = new JButton( main.stringaBottonePrendi );
     50 		button.addActionListener( new ActionListener() {
     51 			public void actionPerformed( ActionEvent e ) {
     52 				if ( giocata[0] != null && giocata[1] != null )
     53 					main.mano++;
     54 				try {
     55 					main.chiPrende();
     56 				} catch ( Exception ex ) {
     57 					JOptionPane.showMessageDialog( Frame.this, main.stringaPrimaGioca, "JBriscola " + main.version, JOptionPane.WARNING_MESSAGE );
     58 				}
     59 				if ( main.mano == 16 )
     60 					JOptionPane.showMessageDialog( Frame.this, main.stringaUltime4, "JBriscola " + main.version, JOptionPane.INFORMATION_MESSAGE );
     61 				if ( main.mano == 17 ) {
     62 					mazzo = false;
     63 					ultimaCarta = null;
     64 					pannello.repaint();
     65 					Frame.this.repaint();
     66 				}
     67 				if ( main.mano == 20 ) {
     68 					String titolo = "";
     69 					String suonoFinale = "";
     70 					if ( main.punteggio > main.punteggioCPU ) {
     71 						titolo = main.stringaVinto;
     72 						suonoFinale = "sounds/perso.wav";
     73 					}
     74 					if ( main.punteggio < main.punteggioCPU ) {
     75 						titolo = main.stringaPerso;
     76 						suonoFinale = "sounds/vinto.wav";
     77 					}
     78 					if ( main.punteggio == main.punteggioCPU ) {
     79 						titolo = main.stringaPareggio;
     80 						suonoFinale = "sounds/pareggio.wav";
     81 					}
     82 					if ( main.sound ) {
     83 						try {
     84 							AudioFileFormat aff = AudioSystem.getAudioFileFormat( ClassLoader.getSystemResource( suonoFinale ) );
     85 							AudioInputStream ais = AudioSystem.getAudioInputStream( ClassLoader.getSystemResource( suonoFinale ) );
     86 							AudioFormat af = aff.getFormat();
     87 							DataLine.Info info = new DataLine.Info(
     88 								Clip.class,
     89 								ais.getFormat(),
     90 								( (int) ais.getFrameLength() *
     91 								af.getFrameSize() ) );
     92 							Clip ol = ( Clip ) AudioSystem.getLine( info );
     93 							ol.open(ais);
     94 							ol.loop( 0 );
     95 						} catch ( Exception ex ) {
     96 							System.err.println( e );
     97 						}
     98 					}
     99 					JOptionPane.showMessageDialog( Frame.this, main.stringaGiocatore + main.punteggio + main.stringaComputer + main.punteggioCPU, titolo, JOptionPane.PLAIN_MESSAGE );
    100 				}
    101 			}
    102 		} );
    103 		barraMenu = new JMenuBar();
    104 		file = new JMenu( "File" );
    105 		nuovaPartita = new JMenuItem( main.stringaNuovaPartita );
    106 		nuovaPartita.addActionListener( new ActionListener() {
    107 			public void actionPerformed( ActionEvent e ) {
    108 				if ( main.mano == 20 ) {
    109 					main.mischia();
    110 					pannello.repaint();
    111 					repaint();
    112 				}
    113 				else {
    114 					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 );
    115 					if ( selezione == JOptionPane.YES_OPTION ) {
    116 						main.mischia();
    117 						pannello.repaint();
    118 						repaint();
    119 					}
    120 				}
    121 			}
    122 		});
    123 		opzioni = new JMenuItem( main.stringaOpzioni );
    124 		opzioni.addActionListener( new ActionListener() {
    125 			public void actionPerformed( ActionEvent e ) {
    126 				OptionFrame optionFrame = new OptionFrame();
    127 				optionFrame.setDefaultCloseOperation( JFrame.DISPOSE_ON_CLOSE );
    128 				optionFrame.setResizable( false );
    129 				optionFrame.setAlwaysOnTop( true );
    130 				optionFrame.setSize( 300, 150 );
    131 				optionFrame.setLocation( (Toolkit.getDefaultToolkit().getScreenSize().width - 300) / 2,
    132 									(Toolkit.getDefaultToolkit().getScreenSize().height - 150) / 2);
    133 				optionFrame.setVisible( true );
    134 			}
    135 		});
    136 		esci = new JMenuItem( main.stringaEsci );
    137 		esci.addActionListener( new ActionListener() {
    138 			public void actionPerformed( ActionEvent e ) {
    139 				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 );
    140 				if ( selezione == JOptionPane.YES_OPTION )
    141 					Frame.this.dispose();
    142 			}
    143 		});
    144 		file.add( nuovaPartita );
    145 		file.add( opzioni );
    146 		file.addSeparator();
    147 		file.add( esci );
    148 		puntoInterrogativo = new JMenu( "?" );
    149 		informazioni = new JMenuItem( main.stringaInformazioni );
    150 		informazioni.addActionListener( new ActionListener() {
    151 			public void actionPerformed( ActionEvent e ) {
    152 				JOptionPane.showMessageDialog( Frame.this, main.stringaInformazioni2, main.stringaInformazioni, JOptionPane.PLAIN_MESSAGE );
    153 			}
    154 		} );
    155 		punti = new JMenuItem( main.stringaPunteggio );
    156 		punti.addActionListener( new ActionListener() {
    157 			public void actionPerformed( ActionEvent e ) {
    158 				JOptionPane.showMessageDialog( Frame.this, main.stringaGiocatore + main.punteggio + "\n" + main.stringaComputer + main.punteggioCPU, main.stringaPunteggio, JOptionPane.PLAIN_MESSAGE );
    159 			}
    160 		} );
    161 		puntoInterrogativo.add( punti );
    162 		puntoInterrogativo.add( informazioni );
    163 		barraMenu.add( file );
    164 		barraMenu.add( puntoInterrogativo );
    165 		setJMenuBar( barraMenu );
    166 		retro1 = new ImageIcon( ClassLoader.getSystemResource( "img/retro1.jpg" ) );
    167 		retro2 = new ImageIcon( ClassLoader.getSystemResource( "img/retro2.jpg" ) );
    168 		for (int i = 0; i < 10; i++)
    169 			for (int j = 0; j < 4; j++)
    170 				arrayCarte[i][j] = new ImageIcon( ClassLoader.getSystemResource( "img/" + (i+1) + "-" + (j+1) + ".jpg" ) );
    171 		main.mischia();
    172 		pannello2.add( button );
    173 		setLayout( new BorderLayout() );
    174 		add( pannello, BorderLayout.CENTER );
    175 		add( pannello2, BorderLayout.SOUTH );
    176 		addMouseListener(
    177 			new MouseAdapter() {
    178 				public void mouseClicked ( MouseEvent e ) {
    179 					if ( mettiCarta( e.getX(), e.getY() ) ) {
    180 						if ( !main.CPUHaGiocato )
    181 							main.giocaCPU();
    182 					}
    183 				}
    184 			}
    185 		);
    186 		addWindowListener(
    187 			new WindowAdapter() {
    188 				public void windowClosing( WindowEvent e ) {
    189 					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 );
    190 					if ( selezione == JOptionPane.YES_OPTION )
    191 						System.exit( 0 );
    192 				}
    193 			}
    194 		);
    195 	}
    196 	public boolean mettiCarta( int x, int y ) {
    197 		if ( !main.toccaAllaCPU && giocata[0] == null ) {
    198 			if ( main.sound && 
    199 				( ( x > 230 && x < 290 && y > 340 && y < 566 ) ||
    200 				  ( x > 300 && x < 360 && y > 340 && y < 566 ) || 
    201 				  ( x > 370 && x < 430 && y > 340 && y < 566 ) ) ) {
    202 				try {
    203 					double a = Math.random() * 6;
    204 					int i = (int) a;
    205 					AudioFileFormat aff = AudioSystem.getAudioFileFormat( ClassLoader.getSystemResource("sounds/" + i + ".wav") );
    206 					AudioInputStream ais = AudioSystem.getAudioInputStream( ClassLoader.getSystemResource("sounds/" + i + ".wav") );
    207 					AudioFormat af = aff.getFormat();
    208 					DataLine.Info info = new DataLine.Info(
    209 						Clip.class,
    210 						ais.getFormat(),
    211 						( (int) ais.getFrameLength() *
    212 						af.getFrameSize() ) );
    213 					Clip ol = ( Clip ) AudioSystem.getLine( info );
    214 					ol.open(ais);
    215 					ol.loop( 0 );
    216 				} catch ( Exception e ) {
    217 					System.err.println( e );
    218 				}
    219 			}
    220 			if ( x > 230 && x < 290 && y > 340 && y < 566 ) {
    221 				giocata[0] = carteInMano[0];
    222 				carteInMano[0] = null;
    223 				pannello.repaint();
    224 				return true;
    225 			}
    226 			if ( x > 300 && x < 360 && y > 340 && y < 566 ) {
    227 				giocata[0] = carteInMano[1];
    228 				carteInMano[1] = null;
    229 				pannello.repaint();
    230 				return true;
    231 			}
    232 			if ( x > 370 && x < 430 && y > 340 && y < 566 ) {
    233 				giocata[0] = carteInMano[2];
    234 				carteInMano[2] = null;
    235 				pannello.repaint();
    236 				return true;
    237 			}
    238 		}
    239 		return false;
    240 	}
    241 }
    242 
    243