From 7167b4341ef290d56fb96e2af10883a4b925d83b Mon Sep 17 00:00:00 2001 From: Sebastiano Tronto Date: Thu, 26 Mar 2026 19:27:18 +0100 Subject: I found my old code! --- JBriscola/all_versions/0.2/0.2.2/src/Frame.java | 432 ++++++++++++++++++++++++ 1 file changed, 432 insertions(+) create mode 100755 JBriscola/all_versions/0.2/0.2.2/src/Frame.java (limited to 'JBriscola/all_versions/0.2/0.2.2/src/Frame.java') diff --git a/JBriscola/all_versions/0.2/0.2.2/src/Frame.java b/JBriscola/all_versions/0.2/0.2.2/src/Frame.java new file mode 100755 index 0000000..bea69a4 --- /dev/null +++ b/JBriscola/all_versions/0.2/0.2.2/src/Frame.java @@ -0,0 +1,432 @@ +/* +Copyright 2009 Sebastiano Tronto + +This file is part of JBriscola. + + JBriscola is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + JBriscola is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with JBriscola; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + +*/ + +import javax.swing.*; +import javax.sound.sampled.*; +import java.io.*; +import java.net.*; +import java.awt.*; +import java.awt.event.*; + +public class Frame extends JFrame { + + /* Booleana per determinare se disegnare il mazzo di carte o no (se + * è l'ultima mano). */ + public static boolean mazzo; + + public static Main main; + + /* Menu, bottoni, pannelli e componenti vari. */ + public static Pannello pannello; + public static JPanel pannello2; + public static JButton button; + public static JMenuBar barraMenu; + public static JMenu file, puntoInterrogativo; + public static JMenuItem nuovaPartita, opzioni, esci, informazioni, punti, aggiornamenti, tastiRapidi; + + /* Immagini delle carte. */ + public static ImageIcon retro1, retro2, ultimaCarta; + public static ImageIcon[] giocata = new ImageIcon[2]; + public static ImageIcon[] carteInMano = new ImageIcon[3]; + public static ImageIcon[] carteCPU = new ImageIcon[3]; + public static ImageIcon[] arrayCarteMescolate = new ImageIcon[40]; + public static ImageIcon[][] arrayCarte = new ImageIcon[10][4]; + + public Frame() { + + super( "Tronto's JBriscola " + main.version ); + + mazzo = true; + main = new Main(); + pannello = new Pannello(); + pannello2 = new JPanel( new FlowLayout() ); + + button = new JButton( main.stringaBottonePrendi ); + button.addActionListener( new ActionListener() { + + /* Quando viene premuto il bottone per prendere/lasciare le carte, + * valuta innanzitutto se il giocatore ha giocato, e poi a chi + * darle ed eventualmente, se è finita la partita, a caricare + * il suono adatto. */ + public void actionPerformed( ActionEvent e ) { + + if ( giocata[0] != null && giocata[1] != null ) + main.mano++; + + try { + main.chiPrende(); + } catch ( Exception ex ) { + /* Avvisa di giocare una carta. */ + JOptionPane.showMessageDialog( Frame.this, main.stringaPrimaGioca, "JBriscola " + main.version, JOptionPane.WARNING_MESSAGE ); + } + + if ( main.mano == 16 ) + /* Avvisa che si è arrivati alla quartultima mano. */ + JOptionPane.showMessageDialog( Frame.this, main.stringaUltime4, "JBriscola " + main.version, JOptionPane.INFORMATION_MESSAGE ); + + /* Non disegna più la briscola e il mazzo se sono già state + * pescate tutte le carte. */ + if ( main.mano == 17 ) { + mazzo = false; + ultimaCarta = null; + pannello.repaint(); + Frame.this.repaint(); + } + + /* Mostra il punteggio e carica i suoni se è finita la partita. */ + if ( main.mano == 20 ) { + + String titolo = ""; + String suonoFinale = ""; + + if ( main.punteggio > main.punteggioCPU ) { + titolo = main.stringaVinto; + suonoFinale = "sounds/perso.wav"; + } + if ( main.punteggio < main.punteggioCPU ) { + titolo = main.stringaPerso; + suonoFinale = "sounds/vinto.wav"; + } + if ( main.punteggio == main.punteggioCPU ) { + titolo = main.stringaPareggio; + suonoFinale = "sounds/pareggio.wav"; + } + + if ( main.sound ) { + try { + AudioFileFormat aff = AudioSystem.getAudioFileFormat( ClassLoader.getSystemResource( suonoFinale ) ); + AudioInputStream ais = AudioSystem.getAudioInputStream( ClassLoader.getSystemResource( suonoFinale ) ); + AudioFormat af = aff.getFormat(); + DataLine.Info info = new DataLine.Info( Clip.class, ais.getFormat(), ( (int) ais.getFrameLength() * af.getFrameSize() ) ); + Clip ol = ( Clip ) AudioSystem.getLine( info ); + ol.open(ais); + ol.loop( 0 ); + } catch ( Exception ex ) { + System.err.println( e ); + } + } + + JOptionPane.showMessageDialog( Frame.this, main.stringaGiocatore + main.punteggio + main.stringaComputer + main.punteggioCPU, titolo, JOptionPane.PLAIN_MESSAGE ); + } + } + } ); + button.addKeyListener( new KeyAdapter() { + /* Aggiunge il keyListener per le scorciatoie da tastiera. Dato + * che il bottone prendi/lascia è l'unico componente effettivo + * della finestra, avrà obbligatoriamente il focus ed è necessario + * che sia lui a gestire gli eventi della tastiera. */ + public void keyReleased( KeyEvent key ) { + + if ( main.keys ) { + switch ( key.getKeyCode() ) { + case KeyEvent.VK_1: + if ( mettiCarta( 231, 500 ) ) { + if ( !main.CPUHaGiocato ) + main.giocaCPU(); + } + break; + + case KeyEvent.VK_2: + if ( mettiCarta( 301, 500 ) ) { + if ( !main.CPUHaGiocato ) + main.giocaCPU(); + } + break; + + case KeyEvent.VK_3: + if ( mettiCarta( 371, 500 ) ) { + if ( !main.CPUHaGiocato ) + main.giocaCPU(); + } + break; + + case KeyEvent.VK_F1: + JOptionPane.showMessageDialog( Frame.this, main.stringaTasti2, "JBriscola - " + main.version, JOptionPane.INFORMATION_MESSAGE ); + break; + + case KeyEvent.VK_F2: + if ( main.mano == 20 ) { + main.mischia(); + pannello.repaint(); + repaint(); + } + else { + 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 ); + if ( selezione == JOptionPane.YES_OPTION ) { + main.mischia(); + pannello.repaint(); + repaint(); + } + } + break; + + case KeyEvent.VK_F3: + OptionFrame optionFrame = new OptionFrame(); + optionFrame.setDefaultCloseOperation( JFrame.DISPOSE_ON_CLOSE ); + optionFrame.setResizable( false ); + optionFrame.setAlwaysOnTop( true ); + optionFrame.setSize( 300, 200 ); + optionFrame.setLocation( (Toolkit.getDefaultToolkit().getScreenSize().width - 300) / 2, (Toolkit.getDefaultToolkit().getScreenSize().height - 150) / 2); + optionFrame.setVisible( true ); + break; + case KeyEvent.VK_F4: + + JOptionPane.showMessageDialog( Frame.this, main.stringaGiocatore + main.punteggio + "\n" + main.stringaComputer + main.punteggioCPU, main.stringaPunteggio, JOptionPane.PLAIN_MESSAGE ); + break; + case KeyEvent.VK_F5: + + JOptionPane.showMessageDialog( Frame.this, main.stringaInformazioni2, main.stringaInformazioni, JOptionPane.PLAIN_MESSAGE ); + break; + case KeyEvent.VK_F6: + + String latest = main.version; + try { + URL latestIs = new URL( "http://porkynator.altervista.org/jbriscola-latest.is" ); + BufferedReader latestIsReader = new BufferedReader( new InputStreamReader( latestIs.openStream() ) ); + latest = latestIsReader.readLine(); + } catch ( Exception ex ) { + JOptionPane.showMessageDialog( Frame.this, main.stringaNoInternet, "JBriscola - " + main.version, JOptionPane.ERROR_MESSAGE ); + } + if ( latest.equals( main.version ) ) + JOptionPane.showMessageDialog( Frame.this, main.stringaAggiornamentiNo, "JBriscola - " + main.version, JOptionPane.INFORMATION_MESSAGE ); + else + JOptionPane.showMessageDialog( Frame.this, main.stringaAggiornamentiSi + "\n" + main.version + " ==> " + latest + "\n\n" + main.stringaScarica, "JBriscola - " + main.version, JOptionPane.INFORMATION_MESSAGE ); + break; + case KeyEvent.VK_Q: + + 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 ); + if ( selezione == JOptionPane.YES_OPTION ) + Frame.this.dispose(); + break; + } + } + } + } ); + + barraMenu = new JMenuBar(); + + file = new JMenu( "File" ); + + nuovaPartita = new JMenuItem( main.stringaNuovaPartita ); + nuovaPartita.addActionListener( new ActionListener() { + /* Avvia una nuova partita, chiedendo prima conferma se ce n'è + * già una in corso. */ + public void actionPerformed( ActionEvent e ) { + if ( main.mano == 20 ) { + main.mischia(); + pannello.repaint(); + repaint(); + } + + else { + 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 ); + if ( selezione == JOptionPane.YES_OPTION ) { + main.mischia(); + pannello.repaint(); + repaint(); + } + } + } + }); + + opzioni = new JMenuItem( main.stringaOpzioni ); + opzioni.addActionListener( new ActionListener() { + /* Apre una OptionFrame per modificare le opzioni. */ + public void actionPerformed( ActionEvent e ) { + + OptionFrame optionFrame = new OptionFrame(); + optionFrame.setDefaultCloseOperation( JFrame.DISPOSE_ON_CLOSE ); + optionFrame.setResizable( false ); + optionFrame.setAlwaysOnTop( true ); + optionFrame.setSize( 300, 200 ); + optionFrame.setLocation( (Toolkit.getDefaultToolkit().getScreenSize().width - 300) / 2, (Toolkit.getDefaultToolkit().getScreenSize().height - 150) / 2); + optionFrame.setVisible( true ); + } + }); + + esci = new JMenuItem( main.stringaEsci ); + esci.addActionListener( new ActionListener() { + /* Ciede conferma prima di uscire. */ + public void actionPerformed( ActionEvent e ) { + 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 ); + if ( selezione == JOptionPane.YES_OPTION ) + Frame.this.dispose(); + } + }); + + file.add( nuovaPartita ); + file.add( opzioni ); + file.addSeparator(); + file.add( esci ); + + puntoInterrogativo = new JMenu( "?" ); + + informazioni = new JMenuItem( main.stringaInformazioni ); + informazioni.addActionListener( new ActionListener() { + /* Visualizza le informazioni. */ + public void actionPerformed( ActionEvent e ) { + JOptionPane.showMessageDialog( Frame.this, main.stringaInformazioni2, main.stringaInformazioni, JOptionPane.PLAIN_MESSAGE ); + } + } ); + + tastiRapidi = new JMenuItem( main.stringaTasti ); + tastiRapidi.addActionListener( new ActionListener() { + /* Visualizza i tasti rapidi. */ + public void actionPerformed( ActionEvent e ) { + JOptionPane.showMessageDialog( Frame.this, main.stringaTasti2, "JBriscola - " + main.version, JOptionPane.INFORMATION_MESSAGE ); + } + } ); + + punti = new JMenuItem( main.stringaPunteggio ); + punti.addActionListener( new ActionListener() { + /* Visualizza il punteggio. */ + public void actionPerformed( ActionEvent e ) { + JOptionPane.showMessageDialog( Frame.this, main.stringaGiocatore + main.punteggio + "\n" + main.stringaComputer + main.punteggioCPU, main.stringaPunteggio, JOptionPane.PLAIN_MESSAGE ); + } + } ); + + aggiornamenti = new JMenuItem( main.stringaAggiornamenti ); + aggiornamenti.addActionListener( new ActionListener() { + /* Cerca aggiornamenti sul sito http://porkynator.altervista.org, + * confrontando la versione del programma con quella scritta nel + * file jbriscola-latest.is (online). */ + public void actionPerformed( ActionEvent e ) { + String latest = main.version; + try { + URL latestIs = new URL( "http://porkynator.altervista.org/jbriscola-latest.is" ); + BufferedReader latestIsReader = new BufferedReader( new InputStreamReader( latestIs.openStream() ) ); + latest = latestIsReader.readLine(); + } catch ( Exception ex ) { + JOptionPane.showMessageDialog( Frame.this, main.stringaNoInternet, "JBriscola - " + main.version, JOptionPane.ERROR_MESSAGE ); + } + if ( latest.equals( main.version ) ) + JOptionPane.showMessageDialog( Frame.this, main.stringaAggiornamentiNo, "JBriscola - " + main.version, JOptionPane.INFORMATION_MESSAGE ); + else + JOptionPane.showMessageDialog( Frame.this, main.stringaAggiornamentiSi + "\n" + main.version + " ==> " + latest + "\n\n" + main.stringaScarica, "JBriscola - " + main.version, JOptionPane.INFORMATION_MESSAGE ); + } + } ); + + puntoInterrogativo.add( punti ); + puntoInterrogativo.add( tastiRapidi ); + puntoInterrogativo.add( informazioni ); + puntoInterrogativo.add( aggiornamenti ); + + barraMenu.add( file ); + barraMenu.add( puntoInterrogativo ); + + setJMenuBar( barraMenu ); + + /* Inizializza le immagini del retro delle carte. */ + retro1 = new ImageIcon( ClassLoader.getSystemResource( "img/retro1.jpg" ) ); + retro2 = new ImageIcon( ClassLoader.getSystemResource( "img/retro2.jpg" ) ); + + /* Inizializza tutte le immagini delle carte scorrendo l'array bidimensionale. */ + for (int i = 0; i < 10; i++) + for (int j = 0; j < 4; j++) + arrayCarte[i][j] = new ImageIcon( ClassLoader.getSystemResource( "img/" + (i+1) + "-" + (j+1) + ".jpg" ) ); + + /* Mischia le carte. */ + main.mischia(); + + /* Aggiunge i vari componenti. */ + pannello2.add( button ); + setLayout( new BorderLayout() ); + add( pannello, BorderLayout.CENTER ); + add( pannello2, BorderLayout.SOUTH ); + + /* Chiama il metodo mettiCarta se l'utente clicca. */ + addMouseListener( + new MouseAdapter() { + public void mouseClicked ( MouseEvent e ) { + if ( mettiCarta( e.getX(), e.getY() ) ) { + if ( !main.CPUHaGiocato ) + main.giocaCPU(); + } + } + } + ); + + /* Chiede conferma prima di uscire quando viene chiusa la finestra. */ + addWindowListener( + new WindowAdapter() { + public void windowClosing( WindowEvent e ) { + 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 ); + if ( selezione == JOptionPane.YES_OPTION ) + System.exit( 0 ); + } + } + ); + } + + /* Se il giocatore ha cliccato su una carta la gioca e riproduce un suono casuale. */ + public boolean mettiCarta( int x, int y ) { + + if ( !main.toccaAllaCPU && giocata[0] == null ) { + if ( main.sound && + ( ( x > 230 && x < 290 && y > 340 && y < 566 && carteInMano[0] != null ) || + ( x > 300 && x < 360 && y > 340 && y < 566 && carteInMano[1] != null ) || + ( x > 370 && x < 430 && y > 340 && y < 566 && carteInMano[2] != null ) ) ) { + try { + + double a = Math.random() * 6; + int i = (int) a; + + AudioFileFormat aff = AudioSystem.getAudioFileFormat( ClassLoader.getSystemResource("sounds/" + i + ".wav") ); + AudioInputStream ais = AudioSystem.getAudioInputStream( ClassLoader.getSystemResource("sounds/" + i + ".wav") ); + AudioFormat af = aff.getFormat(); + DataLine.Info info = new DataLine.Info( Clip.class, ais.getFormat(), ( (int) ais.getFrameLength() * af.getFrameSize() ) ); + Clip ol = ( Clip ) AudioSystem.getLine( info ); + + ol.open(ais); + ol.loop( 0 ); + + } catch ( Exception e ) { + System.err.println( e ); + } + } + + if ( x > 230 && x < 290 && y > 340 && y < 566 && carteInMano[0] != null ) { + giocata[0] = carteInMano[0]; + carteInMano[0] = null; + pannello.repaint(); + return true; + } + + if ( x > 300 && x < 360 && y > 340 && y < 566 && carteInMano[1] != null ) { + giocata[0] = carteInMano[1]; + carteInMano[1] = null; + pannello.repaint(); + return true; + } + + if ( x > 370 && x < 430 && y > 340 && y < 566 && carteInMano[2] != null ) { + giocata[0] = carteInMano[2]; + carteInMano[2] = null; + pannello.repaint(); + return true; + } + } + + return false; + } +} + + -- cgit v1.3