Grafica.java (6652B)
1 // Dichiaro che questa classa farà parte del package RobottinoUtils, che verrà poi importato dalla classe Robottino. 2 package RobottinoUtils; 3 4 // Importo i package standard relativi al funzionamento delle finestre e della grafica in generale. 5 import java.awt.*; 6 7 // E i package RobottinoAzioni, RobottinoUtils e Immagini da me definiti. 8 import RobottinoAzioni.*; 9 import RobottinoUtils.Listener; 10 import Immagini.*; 11 12 // La classe è una sottoclasse di Frame per poter sfruttare gli effetti grafici 13 // ma implementa anche l'interfaccia WindowListener, necessaria per gestire gli eventi collegati alle finestre 14 // (riduzione a icona, chiusura...); per questo devo successivamente ridefinire tutti i 15 // metodi dell'interfaccia. 16 public class Grafica extends Frame { 17 18 // Creo un'istanza della classe Listener che userò come listener per gli eventi. 19 public static Listener listener; 20 21 // E una di ImmagineRobot per le immagini. 22 public static ImmagineRobot immagineRobot = new ImmagineRobot(); 23 24 // Il costruttore senza argomenti. 25 public Grafica () { 26 27 // Il metodo "setBounds" posiziona e dimensiona la finestra. 28 setBounds (this.orizzontale, this.verticale, this.larghezza, this.altezza); 29 30 // "setTitle" per il titolo della finestra. 31 setTitle ("Robottino v2.2"); 32 33 // "setVisible" per poterla visualizzare. 34 setVisible (true); 35 36 // E addWindowListener per l'ascoltatore (listener). 37 listener = new Listener(); 38 addWindowListener (listener); 39 40 // Aggiungo la barra del menu con i vari sottomenu 41 setMenuBar (barraMenu); 42 43 // Il menu file. 44 NewRobot.addActionListener(listener); 45 NewRobot.setActionCommand("newrobot"); 46 47 ChangeRobot.addActionListener(listener); 48 ChangeRobot.setActionCommand("changerobot"); 49 50 DeleteRobot.addActionListener(listener); 51 DeleteRobot.setActionCommand("deleterobot"); 52 53 Exit.addActionListener(listener); 54 Exit.setActionCommand("exit"); 55 56 file.add(NewRobot); 57 file.add(ChangeRobot); 58 file.add(DeleteRobot); 59 file.addSeparator(); 60 file.add(Exit); 61 62 barraMenu.add(file); 63 64 // Il braccio sinistro. 65 LeftArmMove.addActionListener(listener); 66 LeftArmMove.setActionCommand("leftarmmove"); 67 68 LeftArmOn.addActionListener(listener); 69 LeftArmOn.setActionCommand("leftarmon"); 70 71 LeftArmOff.addActionListener(listener); 72 LeftArmOff.setActionCommand("leftarmoff"); 73 74 LeftArm.add(LeftArmMove); 75 LeftArm.add(LeftArmOn); 76 LeftArm.add(LeftArmOff); 77 78 // Il braccio destro. 79 RightArmMove.addActionListener(listener); 80 RightArmMove.setActionCommand("rightarmmove"); 81 82 RightArmOn.addActionListener(listener); 83 RightArmOn.setActionCommand("rightarmon"); 84 85 RightArmOff.addActionListener(listener); 86 RightArmOff.setActionCommand("rightarmoff"); 87 88 RightArm.add(RightArmMove); 89 RightArm.add(RightArmOn); 90 RightArm.add(RightArmOff); 91 92 // La gamba sinistra. 93 LeftLegMove.addActionListener(listener); 94 LeftLegMove.setActionCommand("leftlegmove"); 95 96 LeftLegOn.addActionListener(listener); 97 LeftLegOn.setActionCommand("leftlegon"); 98 99 LeftLegOff.addActionListener(listener); 100 LeftLegOff.setActionCommand("leftlegoff"); 101 102 LeftLeg.add(LeftLegMove); 103 LeftLeg.add(LeftLegOn); 104 LeftLeg.add(LeftLegOff); 105 106 // La gamba destra. 107 RightLegMove.addActionListener(listener); 108 RightLegMove.setActionCommand("rightlegmove"); 109 110 RightLegOn.addActionListener(listener); 111 RightLegOn.setActionCommand("rightlegon"); 112 113 RightLegOff.addActionListener(listener); 114 RightLegOff.setActionCommand("rightlegoff"); 115 116 RightLeg.add(RightLegMove); 117 RightLeg.add(RightLegOn); 118 RightLeg.add(RightLegOff); 119 120 // Menu rinomina. 121 Rename.addActionListener(listener); 122 Rename.setActionCommand("rename"); 123 124 Menu modifica = new Menu("Modifica"); 125 modifica.add(LeftArm); 126 modifica.add(RightArm); 127 modifica.add(LeftLeg); 128 modifica.add(RightLeg); 129 modifica.addSeparator(); 130 modifica.add(Rename); 131 132 barraMenu.add(modifica); 133 134 // Menu azioni. 135 Exploud.addActionListener(listener); 136 Exploud.setActionCommand("exploud"); 137 138 Bier.addActionListener(listener); 139 Bier.setActionCommand("bier"); 140 141 xxx.addActionListener(listener); 142 xxx.setActionCommand("xxx"); 143 144 Dance.addActionListener(listener); 145 Dance.setActionCommand("dance"); 146 147 azioni.add(Exploud); 148 azioni.add(Bier); 149 azioni.add(xxx); 150 azioni.add(Dance); 151 152 barraMenu.add(azioni); 153 154 // Menu info. 155 Porky.addActionListener(listener); 156 Porky.setActionCommand("porky"); 157 158 l.addActionListener(listener); 159 l.setActionCommand("l"); 160 161 RobotList.addActionListener(listener); 162 RobotList.setActionCommand("robotlist"); 163 164 info.add(Porky); 165 info.add(l); 166 info.add(RobotList); 167 168 barraMenu.add(info); 169 } 170 171 // Le variabili pubbliche di classe per il metodo "setBounds". 172 public int orizzontale = 0; 173 public int verticale = 0; 174 public int larghezza = 360; 175 public int altezza = 550; 176 177 // E i vari Menu e MenuItem. 178 MenuBar barraMenu = new MenuBar(); 179 Menu file = new Menu("File"); 180 MenuItem NewRobot = new MenuItem("Nuovo robot"); 181 MenuItem ChangeRobot = new MenuItem("Dai i comandi a un altro robot"); 182 MenuItem DeleteRobot = new MenuItem("Elimina un robot"); 183 MenuItem Exit = new MenuItem("Esci"); 184 Menu LeftArm = new Menu("Braccio sinistro"); 185 MenuItem LeftArmMove = new MenuItem("Cambia posizione"); 186 MenuItem LeftArmOn = new MenuItem("Alza"); 187 MenuItem LeftArmOff = new MenuItem("Abbassa"); 188 Menu RightArm = new Menu("Braccio destro"); 189 MenuItem RightArmMove = new MenuItem("Cambia posizione"); 190 MenuItem RightArmOn = new MenuItem("Alza"); 191 MenuItem RightArmOff = new MenuItem("Abbassa"); 192 Menu LeftLeg = new Menu("Gamba sinistra"); 193 MenuItem LeftLegMove = new MenuItem("Cambia posizione"); 194 MenuItem LeftLegOn = new MenuItem("Alza"); 195 MenuItem LeftLegOff = new MenuItem("Abbassa"); 196 Menu RightLeg = new Menu("Gamba destra"); 197 MenuItem RightLegMove = new MenuItem("Cambia posizione"); 198 MenuItem RightLegOn = new MenuItem("Alza"); 199 MenuItem RightLegOff = new MenuItem("Abbassa"); 200 MenuItem Rename = new MenuItem("Rinomina"); 201 Menu azioni = new Menu("Azioni"); 202 MenuItem Exploud = new MenuItem("Esplodi"); 203 MenuItem Bier = new MenuItem("Birra"); 204 MenuItem xxx = new MenuItem("xxx"); 205 MenuItem Dance = new MenuItem("Balla"); 206 Menu info = new Menu("?"); 207 MenuItem Porky = new MenuItem("Informazioni sul programmatore"); 208 MenuItem l = new MenuItem("Legenda Comandi"); 209 MenuItem RobotList = new MenuItem("Lista robot"); 210 211 212 // Il metodo per disegnare l'immagine del robot. 213 public void paint(Graphics g){ 214 g.drawImage(immagineRobot.robot, 100, 60, this); 215 g.drawImage(immagineRobot.raoff, 66, 248, this); 216 g.drawImage(immagineRobot.laoff, 225, 242, this); 217 g.drawImage(immagineRobot.rloff, 84, 367, this); 218 g.drawImage(immagineRobot.lloff, 148, 367, this); 219 } 220 221 222 } 223 224 225 226