RobotFrame.java (6922B)
1 /* 2 Copyright 2008, 2009 Sebastiano Tronto <sebastiano@luganega.org> 3 4 This file is part of Robottino. 5 6 Robottino 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 Robottino 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 Robottino; 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.event.*; 24 import java.awt.*; 25 26 public class RobotFrame extends JFrame implements ActionListener { 27 public static int rax, ray, lax, lay, rlx, rly, llx, lly; 28 public static Main main; 29 public static ImageIcon ra, la, rl, ll, body; 30 public static JMenuBar barraMenu; 31 public static JMenu modifica, raMenu, laMenu, rlMenu, llMenu; 32 public static JMenuItem raonMenuItem, raoffMenuItem, rachangeMenuItem, laonMenuItem, laoffMenuItem, lachangeMenuItem; 33 public static JMenuItem rlonMenuItem, rloffMenuItem, rlchangeMenuItem, llonMenuItem, lloffMenuItem, llchangeMenuItem; 34 RobotFrame( String titolo ) { 35 super ( titolo ); 36 rax = 66; 37 ray = 248; 38 lax = 225; 39 lay = 242; 40 rlx = 84; 41 rly = 367; 42 llx = 148; 43 lly = 367; 44 ra = new ImageIcon( ClassLoader.getSystemResource( "img/raoff.gif" ) ); 45 la = new ImageIcon( ClassLoader.getSystemResource( "img/laoff.gif" ) ); 46 rl = new ImageIcon( ClassLoader.getSystemResource( "img/rloff.gif" ) ); 47 ll = new ImageIcon( ClassLoader.getSystemResource( "img/lloff.gif" ) ); 48 body = new ImageIcon( ClassLoader.getSystemResource( "img/robot.gif" ) ); 49 raonMenuItem = new JMenuItem( "Alza" ); 50 laonMenuItem = new JMenuItem( "Alza" ); 51 rlonMenuItem = new JMenuItem( "Alza" ); 52 llonMenuItem = new JMenuItem( "Alza" ); 53 raoffMenuItem = new JMenuItem( "Abbassa" ); 54 laoffMenuItem = new JMenuItem( "Abbassa" ); 55 rloffMenuItem = new JMenuItem( "Abbassa" ); 56 lloffMenuItem = new JMenuItem( "Abbassa" ); 57 rachangeMenuItem = new JMenuItem( "Cambia posizione" ); 58 lachangeMenuItem = new JMenuItem( "Cambia posizione" ); 59 rlchangeMenuItem = new JMenuItem( "Cambia posizione" ); 60 llchangeMenuItem = new JMenuItem( "Cambia posizione" ); 61 raonMenuItem.addActionListener( this ); 62 raoffMenuItem.addActionListener( this ); 63 rachangeMenuItem.addActionListener( this ); 64 laonMenuItem.addActionListener( this ); 65 laoffMenuItem.addActionListener( this ); 66 lachangeMenuItem.addActionListener( this ); 67 rlonMenuItem.addActionListener( this ); 68 rloffMenuItem.addActionListener( this ); 69 rlchangeMenuItem.addActionListener( this ); 70 llonMenuItem.addActionListener( this ); 71 lloffMenuItem.addActionListener( this ); 72 llchangeMenuItem.addActionListener( this ); 73 raMenu = new JMenu( "Braccio destro" ); 74 raMenu.add( raonMenuItem ); 75 raMenu.add( raoffMenuItem ); 76 raMenu.add( rachangeMenuItem ); 77 laMenu = new JMenu( "Braccio sinistro" ); 78 laMenu.add( laonMenuItem ); 79 laMenu.add( laoffMenuItem ); 80 laMenu.add( lachangeMenuItem ); 81 rlMenu = new JMenu( "Gamba destra" ); 82 rlMenu.add( rlonMenuItem ); 83 rlMenu.add( rloffMenuItem ); 84 rlMenu.add( rlchangeMenuItem ); 85 llMenu = new JMenu( "Gamba sinistra" ); 86 llMenu.add( llonMenuItem ); 87 llMenu.add( lloffMenuItem ); 88 llMenu.add( llchangeMenuItem ); 89 modifica = new JMenu( "Modifica" ); 90 modifica.add( raMenu ); 91 modifica.add( laMenu ); 92 modifica.add( rlMenu ); 93 modifica.add( llMenu ); 94 barraMenu = new JMenuBar(); 95 barraMenu.add( modifica ); 96 setJMenuBar( barraMenu ); 97 } 98 public void paint ( Graphics g ) { 99 super.paint( g ); 100 body.paintIcon( this, g, 100, 60 ); 101 ra.paintIcon( this, g, this.rax, this.ray ); 102 la.paintIcon( this, g, this.lax, this.lay ); 103 rl.paintIcon( this, g, this.rlx, this.rly ); 104 ll.paintIcon( this, g, this.llx, this.lly ); 105 } 106 public void actionPerformed( ActionEvent e ) { 107 esegui( e ); 108 } 109 public void esegui( ActionEvent e ) { 110 if ( e.getSource() == raonMenuItem ) 111 main.list.getRobot( this.getTitle() ).rightArm = true; 112 else if ( e.getSource() == raoffMenuItem ) 113 main.list.getRobot( this.getTitle() ).rightArm = false; 114 else if ( e.getSource() == rachangeMenuItem ) 115 main.list.getRobot( this.getTitle() ).rightArm = !main.list.getRobot( this.getTitle() ).rightArm; 116 else if ( e.getSource() == laonMenuItem ) 117 main.list.getRobot( this.getTitle() ).leftArm = true; 118 else if ( e.getSource() == laoffMenuItem ) 119 main.list.getRobot( this.getTitle() ).leftArm = false; 120 else if ( e.getSource() == lachangeMenuItem ) 121 main.list.getRobot( this.getTitle() ).leftArm = !main.list.getRobot( this.getTitle() ).leftArm; 122 if ( e.getSource() == rlonMenuItem ) 123 main.list.getRobot( this.getTitle() ).rightLeg = true; 124 else if ( e.getSource() == rloffMenuItem ) 125 main.list.getRobot( this.getTitle() ).rightLeg = false; 126 else if ( e.getSource() == rlchangeMenuItem ) 127 main.list.getRobot( this.getTitle() ).rightLeg = !main.list.getRobot( this.getTitle() ).rightLeg; 128 else if ( e.getSource() == llonMenuItem ) 129 main.list.getRobot( this.getTitle() ).leftLeg = true; 130 else if ( e.getSource() == lloffMenuItem ) 131 main.list.getRobot( this.getTitle() ).leftLeg = false; 132 else if ( e.getSource() == llchangeMenuItem ) 133 main.list.getRobot( this.getTitle() ).leftLeg = !main.list.getRobot( this.getTitle() ).leftLeg; 134 setImages(); 135 repaint(); 136 } 137 public void setImages() { 138 if ( main.list.getRobot( this.getTitle() ).rightArm ) { 139 this.ra = new ImageIcon( ClassLoader.getSystemResource( "img/raon.gif" ) ); 140 this.rax = 68; 141 this.ray = 128; 142 } 143 else { 144 this.ra = new ImageIcon( ClassLoader.getSystemResource( "img/raoff.gif" ) ); 145 this.rax = 66; 146 this.ray = 248; 147 } 148 if ( main.list.getRobot( this.getTitle() ).leftArm ) { 149 this.la = new ImageIcon( ClassLoader.getSystemResource( "img/laon.gif" ) ); 150 this.lax = 224; 151 this.lay = 118; 152 } 153 else { 154 this.la = new ImageIcon( ClassLoader.getSystemResource( "img/laoff.gif" ) ); 155 this.lax = 225; 156 this.lay = 242; 157 } 158 if ( main.list.getRobot( this.getTitle() ).rightLeg ) { 159 this.rl = new ImageIcon( ClassLoader.getSystemResource( "img/rlon.gif" ) ); 160 this.rlx = 84; 161 this.rly = 356; 162 } 163 else { 164 this.rl = new ImageIcon( ClassLoader.getSystemResource( "img/rloff.gif" ) ); 165 this.rlx = 84; 166 this.rly = 367; 167 } 168 if ( main.list.getRobot( this.getTitle() ).leftLeg ) { 169 this.ll = new ImageIcon( ClassLoader.getSystemResource( "img/llon.gif" ) ); 170 this.llx = 140; 171 this.lly = 363; 172 } 173 else { 174 this.ll = new ImageIcon( ClassLoader.getSystemResource( "img/lloff.gif" ) ); 175 this.llx = 148; 176 this.lly = 367; 177 } 178 } 179 }