1   package main;
2   
3   import intellego.Intellego;
4   import util.*;
5   import interfaces.*;
6   import real.*;
7   import simworldobjects.*;
8   import main.*;
9   import NetBeansResources.*;
10  
11  import java.awt.*;
12  import java.lang.*;
13  import javax.swing.plaf.basic.*;
14  import java.awt.event.*;
15  import javax.swing.*;
16  import java.io.*;
17  
18  /** Provides a dialog box to get intial robot parameters from the user.
19   * @author Graham Ritchie
20   * @Modyfier Simon Zienkiewicz
21   */ 
22  public class InitRobotDialog extends JPanel implements ActionListener
23  {
24      private JTextField xField, zField, bField, cField;
25          private JSpinner xS,zS,bS,cS;
26          private JLabel xLabel, zLabel, bLabel, cLabel,cLabel2,title;
27          private JButton OK, cancel;
28      private SimWorld world;
29      private Controller controller;
30      private SimDisplay display;
31          private SimUI sim;
32  
33          /** Creates a new instance of InitRobotDialog() object */
34          
35          public InitRobotDialog(){
36          
37          }
38          
39          /** Creates a robot object from given parameters.
40           * @param w the SimWorld of the robot
41           * @param c the controller of the robot (the brains)
42           * @param d the SimDisplay associated with the robot
43           * @param sim the Simulation environment of the robot
44           */        
45          public void createRobot(SimWorld w, Controller c, SimDisplay d, SimUI sim)
46          {
47              //setTitle("Initialise Robot:");
48              setSize(220,195);
49              setLocation(550,180);
50              setVisible(true);
51              this.setBorder(BasicBorders.getTextFieldBorder());
52              this.setLayout(new AbsoluteLayout());   
53              
54              //set variables
55              world=w;
56              controller=c;
57              display=d;
58              this.sim = sim;
59                    
60              xS=new JSpinner(new SpinnerNumberModel(400,0,((BasicSimWorld)world).getWorldDimensions()[0],1));
61              zS=new JSpinner(new SpinnerNumberModel(500,0,((BasicSimWorld)world).getWorldDimensions()[2],1));
62              bS=new JSpinner(new SpinnerNumberModel(0,0,360,1));
63              cS=new JSpinner(new SpinnerNumberModel(7726,0,8000,1));
64                     
65              //set colors
66              xS.setBackground(Color.yellow);
67              zS.setBackground(Color.yellow);
68              bS.setBackground(Color.yellow);
69              cS.setBackground(Color.yellow);
70               
71              title = new JLabel("Initialize Robot:");
72              xLabel=new JLabel("Initial X value:");
73              zLabel=new JLabel("Initial Z value:");
74              bLabel=new JLabel("Initial bearing:");
75              cLabel=new JLabel("Enter the battery");
76              cLabel2=new JLabel("voltage(mV):");
77              //set colors
78              title.setForeground(Color.yellow);
79              xLabel.setForeground(Color.lightGray);
80              zLabel.setForeground(Color.lightGray);
81              bLabel.setForeground(Color.lightGray);
82              cLabel.setForeground(Color.lightGray);
83              cLabel2.setForeground(Color.lightGray);
84  
85              (OK=new JButton("OK")).addActionListener(this);
86              (cancel=new JButton("Cancel")).addActionListener(this);
87               
88              OK.setMargin(new Insets(1,1,1,1));
89              cancel.setMargin(new Insets(1,1,1,1));
90          
91              //set color
92              OK.setBackground(Color.yellow);
93              OK.setForeground(Color.darkGray);
94              cancel.setBackground(Color.yellow);
95              cancel.setForeground(Color.darkGray);
96  
97              int r=15;
98              this.add(title,new AbsoluteConstraints(6,7));
99              this.add(xLabel,new AbsoluteConstraints(6,30));
100             this.add(xS,new AbsoluteConstraints(30+r,55));
101             this.add(zLabel,new AbsoluteConstraints(6,85));
102             this.add(zS,new AbsoluteConstraints(30+r,110));
103             this.add(bLabel,new AbsoluteConstraints(6,140));
104             this.add(bS,new AbsoluteConstraints(50+r,165));
105             this.add(cLabel,new AbsoluteConstraints(6,195));
106             this.add(cS,new AbsoluteConstraints(30+r,237));
107             this.add(cLabel2,new AbsoluteConstraints(23,212));
108             
109             this.add(OK,new AbsoluteConstraints(32,270));
110             this.add(cancel,new AbsoluteConstraints(62,270));
111 
112             this.setBackground(Color.darkGray);
113             //this.show();
114         }
115         
116         public void callOK(){
117                         
118             try{
119                 int x=Integer.parseInt(xS.getValue().toString());
120                 int z=Integer.parseInt(zS.getValue().toString());
121                 int b=Integer.parseInt(bS.getValue().toString());
122                 int c=Integer.parseInt(cS.getValue().toString());
123 
124                 long X=(long)x;
125                 long Z=(long)z;
126                 long B=(long)b;
127 
128                 // create the robot
129                 SimRCX rcx=new SimRCX(world, controller, 30.0,40.0,60.0,"robot",X,0.0,Z,0.0,B,c);
130 
131                 // add it to the controller
132                 controller.initController(rcx);
133 
134                 // ... and add it to the world
135                 world.addObject((SimObject)rcx);
136 
137                 display.repaint();
138                 
139             }
140             catch(Exception a){
141                 main.MainInterface.displayMessage("ERROR: Incorrect Input Type | FIX: Please try again, only entering INTEGERs");
142                 
143             }
144         
145         }
146 
147         /**  Action event handler - creates a robot according to user input,
148          *  having check that the input is valid.
149          * @param e the action event
150          */
151         public void actionPerformed(ActionEvent e)
152         {
153             if (e.getSource()==OK)
154             {
155                 
156                 try{
157                     int x=Integer.parseInt(xS.getValue().toString());
158                     int z=Integer.parseInt(zS.getValue().toString());
159                     int b=Integer.parseInt(bS.getValue().toString());
160                     int c=Integer.parseInt(cS.getValue().toString());
161                     
162                     long X=(long)x;
163                     long Z=(long)z;
164                     long B=(long)b;
165                     
166                     // create the robot
167                     SimRCX rcx=new SimRCX(world, controller, 30.0,40.0,60.0,"robot",X,0.0,Z,0.0,B,c);
168 
169                     // add it to the controller
170                     controller.initController(rcx);
171 
172                     // ... and add it to the world
173                     world.addObject((SimObject)rcx);
174 
175                     display.repaint();
176 
177                     hide();
178                 }
179                 catch(Exception a){
180                     main.MainInterface.displayMessage("ERROR: Incorrect Input Type | FIX: Please try again, only entering INTEGERs");
181                     hide();
182                 }
183             }
184             else
185             {
186                 hide();
187             }
188             
189             try{sim.setSize(sim.getWidth(),sim.getHeight());}
190             catch(Exception e1){};
191     }
192 }
193