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