/* Copyright 2008, 2009 Sebastiano Tronto This file is part of Robottino. Robottino 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. Robottino 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 Robottino; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Frame extends JFrame implements ActionListener { public static int i; public static Main main; public static RobotPanel robotPanel; public static JLabel label; public static Timer timer; public static JMenuBar barraMenu; public static JMenu file, modifica, azioni, info, raMenu, laMenu, rlMenu, llMenu; public static JMenuItem raonMenuItem, raoffMenuItem, rachangeMenuItem, laonMenuItem, laoffMenuItem, lachangeMenuItem; public static JMenuItem rlonMenuItem, rloffMenuItem, rlchangeMenuItem, llonMenuItem, lloffMenuItem, llchangeMenuItem; public static JMenuItem rinomina, birra, balla, esplodi, informazioni, esci; public Frame() { super( "Robottino versione 4.2" ); i = 0; main = new Main(); robotPanel = new RobotPanel(); label = new JLabel( "Robot: " + main.robot.getName() ); label.setFont( new Font( "Monospace", 0, 30 ) ); barraMenu = new JMenuBar(); rinomina = new JMenuItem( "Cambia nome" ); birra = new JMenuItem( "Birra" ); balla = new JMenuItem( "Balla" ); esplodi = new JMenuItem( "Esplodi" ); informazioni = new JMenuItem( "Informazioni" ); esci = new JMenuItem( "Esci" ); raonMenuItem = new JMenuItem( "Alza" ); laonMenuItem = new JMenuItem( "Alza" ); rlonMenuItem = new JMenuItem( "Alza" ); llonMenuItem = new JMenuItem( "Alza" ); raoffMenuItem = new JMenuItem( "Abbassa" ); laoffMenuItem = new JMenuItem( "Abbassa" ); rloffMenuItem = new JMenuItem( "Abbassa" ); lloffMenuItem = new JMenuItem( "Abbassa" ); rachangeMenuItem = new JMenuItem( "Cambia posizione" ); lachangeMenuItem = new JMenuItem( "Cambia posizione" ); rlchangeMenuItem = new JMenuItem( "Cambia posizione" ); llchangeMenuItem = new JMenuItem( "Cambia posizione" ); esci.addActionListener( this ); rinomina.addActionListener( this ); birra.addActionListener( this ); balla.addActionListener( this ); esplodi.addActionListener( this ); informazioni.addActionListener( this ); raonMenuItem.addActionListener( this ); raoffMenuItem.addActionListener( this ); rachangeMenuItem.addActionListener( this ); laonMenuItem.addActionListener( this ); laoffMenuItem.addActionListener( this ); lachangeMenuItem.addActionListener( this ); rlonMenuItem.addActionListener( this ); rloffMenuItem.addActionListener( this ); rlchangeMenuItem.addActionListener( this ); llonMenuItem.addActionListener( this ); lloffMenuItem.addActionListener( this ); llchangeMenuItem.addActionListener( this ); raMenu = new JMenu( "Braccio destro" ); raMenu.add( raonMenuItem ); raMenu.add( raoffMenuItem ); raMenu.add( rachangeMenuItem ); laMenu = new JMenu( "Braccio sinistro" ); laMenu.add( laonMenuItem ); laMenu.add( laoffMenuItem ); laMenu.add( lachangeMenuItem ); rlMenu = new JMenu( "Gamba destra" ); rlMenu.add( rlonMenuItem ); rlMenu.add( rloffMenuItem ); rlMenu.add( rlchangeMenuItem ); llMenu = new JMenu( "Gamba sinistra" ); llMenu.add( llonMenuItem ); llMenu.add( lloffMenuItem ); llMenu.add( llchangeMenuItem ); modifica = new JMenu( "Modifica" ); modifica.add( raMenu ); modifica.add( laMenu ); modifica.add( rlMenu ); modifica.add( llMenu ); file = new JMenu( "File" ); file.add( rinomina ); file.add( esci ); azioni = new JMenu( "Azioni" ); azioni.add( birra ); azioni.add( balla ); azioni.add( esplodi ); info = new JMenu( "?" ); info.add( informazioni ); barraMenu.add( file ); barraMenu.add( modifica ); barraMenu.add( azioni ); barraMenu.add( info ); setJMenuBar( barraMenu ); add( robotPanel, BorderLayout.CENTER ); add( label, BorderLayout.SOUTH ); addWindowListener( new WindowAdapter() { public void windowClosing( WindowEvent e ) { int selezione = JOptionPane.showOptionDialog( Frame.this, "Vuoi davvero uscire?", "Robottino v4.2", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, new String[] { "Si", "No" }, "No" ); if ( selezione == JOptionPane.YES_OPTION ) System.exit( 0 ); } } ); } public void actionPerformed( ActionEvent e ) { if ( e.getSource() == esci ) { int selezione = JOptionPane.showOptionDialog( this, "Vuoi davvero uscire?", "Robottino v4.2", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, new String[] { "Si", "No" }, "No" ); if ( selezione == JOptionPane.YES_OPTION ) System.exit( 0 ); } else if ( e.getSource() == rinomina ) { String newName = main.robot.getName(); newName = JOptionPane.showInputDialog( this, "Inserire il nuovo nome per il robottino:", newName ); if ( newName != null ) { main.robot.setName( newName ); label .setText( "Robot: " + main.robot.getName() ); } } else if ( e.getSource() == raonMenuItem ) main.robot.setRA( true ); else if ( e.getSource() == raoffMenuItem ) main.robot.setRA( false ); else if ( e.getSource() == rachangeMenuItem ) main.robot.setRA( !main.robot.rightArm ); else if ( e.getSource() == laonMenuItem ) main.robot.setLA( true ); else if ( e.getSource() == laoffMenuItem ) main.robot.setLA( false ); else if ( e.getSource() == lachangeMenuItem ) main.robot.setLA( !main.robot.leftArm ); else if ( e.getSource() == rlonMenuItem ) main.robot.setRL( true ); else if ( e.getSource() == rloffMenuItem ) main.robot.setRL( false ); else if ( e.getSource() == rlchangeMenuItem ) main.robot.setRL( !main.robot.rightLeg ); else if ( e.getSource() == llonMenuItem ) main.robot.setLL( true ); else if ( e.getSource() == lloffMenuItem ) main.robot.setLL( false ); else if ( e.getSource() == llchangeMenuItem ) main.robot.setLL( !main.robot.leftLeg ); else if ( e.getSource() == birra ) { ImageIcon birra = new ImageIcon( ClassLoader.getSystemResource( "img/birra5.gif" ) ); JOptionPane.showMessageDialog( this, "", "Robottino v4.2", JOptionPane.PLAIN_MESSAGE, birra ); birra = new ImageIcon( ClassLoader.getSystemResource( "img/birra1.jpg" ) ); JOptionPane.showMessageDialog( this, "", "Robottino v4.2", JOptionPane.PLAIN_MESSAGE, birra ); birra = new ImageIcon( ClassLoader.getSystemResource( "img/birra2.jpg" ) ); JOptionPane.showMessageDialog( this, "", "Robottino v4.2", JOptionPane.PLAIN_MESSAGE, birra ); birra = new ImageIcon( ClassLoader.getSystemResource( "img/birra3.jpg" ) ); JOptionPane.showMessageDialog( this, "", "Robottino v4.2", JOptionPane.PLAIN_MESSAGE, birra ); birra = new ImageIcon( ClassLoader.getSystemResource( "img/birra4.jpg" ) ); JOptionPane.showMessageDialog( this, "", "Robottino v4.2", JOptionPane.PLAIN_MESSAGE, birra ); JOptionPane.showMessageDialog( this, "Che bevuta!", "Robottino v4.2", JOptionPane.PLAIN_MESSAGE ); JOptionPane.showMessageDialog( this, "Buuuuuuurp!", "Robottino v4.2", JOptionPane.PLAIN_MESSAGE ); robotPanel.body = new ImageIcon( ClassLoader.getSystemResource( "img/robot.gif" ) ); } if ( e.getSource() == esplodi ) { String aux = main.robot.getName(); main.robot.setPositions( false, false, false, false ); main.robot.setName( "no-name" ); label.setText( "Robot: " + main.robot.getName() ); ImageIcon boom = new ImageIcon( ClassLoader.getSystemResource( "img/boom.png" ) ); JOptionPane.showMessageDialog( this, "", "Robottino v4.2", JOptionPane.PLAIN_MESSAGE, boom ); JOptionPane.showMessageDialog( this, aux + " si è autodistrutto!", "Robottino v4.2", JOptionPane.WARNING_MESSAGE ); JOptionPane.showMessageDialog( this, "È stato creato un nuovo robottino: " + main.robot.getName(), "Robottino v4.2", JOptionPane.INFORMATION_MESSAGE ); } if ( e.getSource() == informazioni ) { String aux = main.robot.getName() + ":\n\n"; if ( main.robot.rightArm ) aux += "Braccio destro: alzato\n"; else aux += "Braccio destro: abbassato\n"; if ( main.robot.leftArm ) aux += "Braccio sinistro: alzato\n"; else aux += "Braccio sinistro: abbassato\n"; if ( main.robot.rightLeg ) aux += "Gamba destra: alzata\n"; else aux += "Gamba destra: abbassata\n"; if ( main.robot.leftLeg ) aux += "Gamba sinistra: alzata\n"; else aux += "Gamba sinistra: abbassata\n"; JOptionPane.showMessageDialog( this, aux, "Robottino - v4.2", JOptionPane.INFORMATION_MESSAGE ); } else if ( e.getSource() == balla ) { this.i = 0; main.robot.setPositions( false, false, false, false ); timer = new Timer( 250, new ActionListener() { public void actionPerformed( ActionEvent e ) { Frame.this.i++; switch( Frame.this.i ) { case 1: Frame.main.robot.setRA( true ); break; case 2: break; case 3: Frame.main.robot.setLA( true ); break; case 4: break; case 5: Frame.main.robot.setPositions( false, false, false, true ); break; case 6: Frame.main.robot.setPositions( true, true, false, false ); break; case 7: Frame.main.robot.setPositions( false, false, true, false ); break; case 8: Frame.main.robot.setPositions( true, true, false, false ); break; case 9: Frame.main.robot.setPositions( true, false, false, true ); break; case 10: Frame.main.robot.setPositions( false, false, false, false ); break; case 11: Frame.main.robot.setPositions( true, false, false, true ); break; case 12: Frame.main.robot.setPositions( false, true, true, false ); break; case 13: Frame.main.robot.setPositions( false, false, false, false ); break; case 14: Frame.main.robot.setPositions( false, true, true, false ); break; case 15: Frame.main.robot.setPositions( false, false, false, false ); break; case 16: break; case 17: Frame.main.robot.setPositions( false, false, true, false ); break; case 18: Frame.main.robot.setPositions( false, false, false, true ); break; case 19: Frame.main.robot.setPositions( false, false, true, false ); break; case 20: Frame.main.robot.setPositions( false, false, false, true ); break; case 21: Frame.main.robot.setPositions( false, false, true, false ); break; case 22: Frame.main.robot.setPositions( false, false, false, true ); break; case 23: Frame.main.robot.setPositions( true, true, true, true ); break; case 24: break; case 25: break; case 26: Frame.main.robot.setPositions( false, false, false, false ); break; case 27: JOptionPane.showMessageDialog( Frame.this, "Cha fatica!", "Robottino v4.2", JOptionPane.PLAIN_MESSAGE ); JOptionPane.showMessageDialog( Frame.this, Frame.main.robot.getName() + " è tornato alla posizione di partenza", "Robottino v4.2", JOptionPane.PLAIN_MESSAGE ); break; default: timer.stop(); break; } Frame.this.robotPanel.setImages(); Frame.this.robotPanel.repaint(); Frame.this.repaint(); } } ); timer.start(); } try { this.robotPanel.setImages(); this.robotPanel.repaint(); this.repaint(); } catch ( NullPointerException n ) { } } }