package demo.helicopter; import java.awt.*; import java.awt.event.*; import javax.swing.*; import mil.navy.nps.dis.*; public class AgentTankControlPanel extends JFrame implements AdjustmentListener, ActionListener{ JPanel jPanel1 = new JPanel(); JPanel jPanel2 = new JPanel(); JPanel jPanel3 = new JPanel(); JButton raisegunButton = new JButton(); JButton brakesButton = new JButton(); JButton lowergunButton = new JButton(); JButton turretleftButton = new JButton(); JButton turretrightButton = new JButton(); JButton homeButton = new JButton(); JButton newvehicleButton = new JButton(); JTextField headingTextField = new JTextField(); JScrollBar speedScroll = new JScrollBar(); JTextField speedTextField = new JTextField(); JScrollBar turnScroll = new JScrollBar(); JTextField turnTextField = new JTextField(); JLabel jLabel1 = new JLabel(); JLabel jLabel2 = new JLabel(); JLabel jLabel3 = new JLabel(); JLabel jLabel4 = new JLabel(); JLabel jLabel5 = new JLabel(); JLabel jLabel6 = new JLabel(); JLabel jLabel7 = new JLabel(); JLabel jLabel8 = new JLabel(); JLabel jLabel9 = new JLabel(); JLabel jLabel10 = new JLabel(); JLabel jLabel11 = new JLabel(); JButton offenseButton = new JButton(); JButton defenseButton = new JButton(); JButton firemaingunButton = new JButton(); JButton fireauxgunButton = new JButton(); JTextField mainammoTextField = new JTextField(); JTextField auxammoTextField = new JTextField(); JLabel jLabel12 = new JLabel(); JLabel jLabel13 = new JLabel(); JTextField gunelevTextField = new JTextField(); JLabel jLabel14 = new JLabel(); JPanel jPanel4 = new JPanel(); private static int turnPosition = 0; // each of the scroll bars private static int speedPosition = 0; private static boolean leftTurretTurnState; // 0 = not turning; 1 = turning private static boolean rightTurretTurnState; // 0 = not turning; 1 = turning private static boolean forwardState; private static boolean reverseState; private static boolean upGunState; private static boolean downGunState; private static int mainAmmo; private static int mgAmmo; private static boolean offenseState; private static boolean defenseState; private AgentTankActionInterpreter ai; //Construct the frame //============================================================================================= // Constructor //============================================================================================= public AgentTankControlPanel(String ipAddr, int portNum, int timeToLive, short siteNum, short appNum, short id, String marking, boolean rtpHeaderEnabled) { ai = new AgentTankActionInterpreter(ipAddr, portNum, timeToLive, siteNum, appNum, id, marking, rtpHeaderEnabled, this); enableEvents(AWTEvent.WINDOW_EVENT_MASK); try { jbInit(); } catch (Exception e) { e.printStackTrace(); } leftTurretTurnState = false; rightTurretTurnState = false; forwardState = true; reverseState = false; offenseState = true; defenseState = false; mainAmmo = 40; mgAmmo = 30; ai.start(); //start tank action interpeter } //Component initialization private void jbInit() throws Exception { this.getContentPane().setLayout(null); this.setSize(new Dimension(765, 175)); Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); this.setLocation((screenSize.width - 765) / 2, (screenSize.height - 175)); this.setVisible(true); this.setBackground(Color.black); this.setForeground(Color.black); this.setFont(new Font("Courier", 1, 12)); String frameTitle = ai.getFrameHeaderInfo(); jPanel1.setLayout(null); jPanel1.setBackground(new Color(150, 150, 150)); jPanel1.setFont(new Font("Serif", 0, 10)); jPanel1.setBounds(new Rectangle(0, 0, 370, 150)); jPanel2.setBackground(new Color(212, 131, 81)); jPanel2.setLayout(null); jPanel2.setBounds(new Rectangle(375, 0, 185, 150)); jPanel3.setBackground(new Color(153, 153, 153)); jPanel3.setLayout(null); jPanel3.setBounds(new Rectangle(565, 0, 195, 150)); raisegunButton.setText("Raise Gun"); raisegunButton.setFont(new Font("SansSerif", 1, 10)); raisegunButton.setBounds(new Rectangle(97, 38, 92, 34)); raisegunButton.addActionListener(this); brakesButton.setText("Brakes"); brakesButton.setFont(new Font("SansSerif", 1, 10)); brakesButton.setBounds(new Rectangle(1, 76, 94, 34)); brakesButton.addActionListener(this); lowergunButton.setText("Lower Gun"); lowergunButton.setFont(new Font("SansSerif", 1, 10)); lowergunButton.setBounds(new Rectangle(97, 76, 92, 34)); lowergunButton.addActionListener(this); turretleftButton.setText("Turret Left"); turretleftButton.setFont(new Font("SansSerif", 1, 10)); turretleftButton.setBounds(new Rectangle(1, 114, 94, 34)); turretleftButton.addActionListener(this); turretrightButton.setText("Turret Right"); turretrightButton.setFont(new Font("SansSerif", 1, 10)); turretrightButton.setBounds(new Rectangle(97, 114, 92, 34)); turretrightButton.addActionListener(this); homeButton.setText("Home"); homeButton.setFont(new Font("SansSerif", 1, 10)); homeButton.setBounds(new Rectangle(1, 1, 94, 34)); homeButton.addActionListener(this); newvehicleButton.setText("New Vehicle"); newvehicleButton.setFont(new Font("SansSerif", 1, 9)); newvehicleButton.setBounds(new Rectangle(1, 38, 94, 34)); newvehicleButton.addActionListener(this); headingTextField.setHorizontalAlignment(JTextField.CENTER); headingTextField.setEditable(false); headingTextField.setText("0"); headingTextField.setBounds(new Rectangle(170, 1, 35, 20)); speedScroll.setVisibleAmount(5); speedScroll.setMaximum(65); speedScroll.setFont(new Font("SansSerif", 1, 10)); speedScroll.setOrientation(JScrollBar.HORIZONTAL); speedScroll.setBounds(new Rectangle(1, 50, 154, 20)); speedScroll.addAdjustmentListener(this); speedTextField.setHorizontalAlignment(JTextField.CENTER); speedTextField.setText("0"); speedTextField.setEditable(false); speedTextField.setBounds(new Rectangle(170, 50, 35, 20)); turnScroll.setVisibleAmount(5); turnScroll.setMaximum(55); turnScroll.setOrientation(JScrollBar.HORIZONTAL); turnScroll.setMinimum(-50); turnScroll.setBounds(new Rectangle(1, 105, 154, 20)); turnScroll.addAdjustmentListener(this); turnTextField.setHorizontalAlignment(JTextField.CENTER); turnTextField.setText("0"); turnTextField.setEditable(false); turnTextField.setBounds(new Rectangle(170, 105, 35, 20)); jLabel1.setForeground(Color.black); jLabel1.setText("Heading"); jLabel1.setBounds(new Rectangle(163, 22, 51, 17)); jLabel2.setForeground(Color.black); jLabel2.setText("Speed"); jLabel2.setBounds(new Rectangle(169, 71, 38, 18)); jLabel3.setForeground(Color.black); jLabel3.setText("Turn Rate"); jLabel3.setBounds(new Rectangle(159, 125, 57, 17)); jLabel4.setForeground(Color.black); jLabel4.setText("Speed Control"); jLabel4.setBounds(new Rectangle(37, 35, 83, 15)); jLabel5.setForeground(Color.black); jLabel5.setText("Turn Rate Control"); jLabel5.setBounds(new Rectangle(28, 90, 99, 15)); jLabel6.setForeground(Color.black); jLabel6.setText("0"); jLabel6.setBounds(new Rectangle(77, 125, 10, 15)); jLabel7.setForeground(Color.black); jLabel7.setText("+50"); jLabel7.setBounds(new Rectangle(125, 125, 21, 15)); jLabel8.setForeground(Color.black); jLabel8.setText("-50"); jLabel8.setBounds(new Rectangle(8, 125, 23, 15)); jLabel9.setForeground(Color.black); jLabel9.setText("0"); jLabel9.setBounds(new Rectangle(10, 71, 12, 15)); jLabel10.setForeground(Color.black); jLabel10.setText("30"); jLabel10.setBounds(new Rectangle(70, 71, 18, 15)); jLabel11.setForeground(Color.black); jLabel11.setText("60"); jLabel11.setBounds(new Rectangle(131, 71, 16, 15)); offenseButton.setText("Reverse"); offenseButton.setFont(new Font("SansSerif", 1, 9)); offenseButton.setBounds(new Rectangle(1, 1, 75, 30)); offenseButton.addActionListener(this); defenseButton.setText("Forward"); defenseButton.setFont(new Font("SansSerif", 1, 10)); defenseButton.setBounds(new Rectangle(79, 1, 75, 30)); defenseButton.addActionListener(this); firemaingunButton.setText("Fire Main Gun"); firemaingunButton.setFont(new Font("SansSerif", 1, 10)); firemaingunButton.setBounds(new Rectangle(265, 36, 103, 33)); firemaingunButton.addActionListener(this); fireauxgunButton.setText("Fire Aux Gun"); fireauxgunButton.setFont(new Font("SansSerif", 1, 10)); fireauxgunButton.setBounds(new Rectangle(265, 114, 104, 31)); fireauxgunButton.addActionListener(this); mainammoTextField.setHorizontalAlignment(JTextField.CENTER); mainammoTextField.setText("40"); mainammoTextField.setEditable(false); mainammoTextField.setBounds(new Rectangle(301, 1, 31, 18)); auxammoTextField.setHorizontalAlignment(JTextField.CENTER); auxammoTextField.setText("30"); auxammoTextField.setEditable(false); auxammoTextField.setBounds(new Rectangle(301, 77, 31, 18)); jLabel12.setForeground(Color.black); jLabel12.setText("Main Gun Ammo"); jLabel12.setBounds(new Rectangle(272, 18, 93, 15)); jLabel13.setForeground(Color.black); jLabel13.setText("Aux Gun Ammo"); jLabel13.setBounds(new Rectangle(275, 94, 87, 16)); gunelevTextField.setHorizontalAlignment(JTextField.CENTER); gunelevTextField.setText("0"); gunelevTextField.setEditable(false); gunelevTextField.setBounds(new Rectangle(123, 1, 36, 20)); jLabel14.setForeground(Color.black); jLabel14.setText("Main Gun Elev"); jLabel14.setBounds(new Rectangle(104, 20, 80, 16)); this.setResizable(false); this.setTitle("Agent Tank Control Panel " + frameTitle); this.setBackground(Color.black); this.setFont(new Font("Courier", 1, 12)); this.getContentPane().add(jPanel1, null); jPanel1.add(headingTextField, null); jPanel1.add(speedScroll, null); jPanel1.add(speedTextField, null); jPanel1.add(turnScroll, null); jPanel1.add(turnTextField, null); jPanel1.add(jLabel1, null); jPanel1.add(jLabel2, null); jPanel1.add(jLabel3, null); jPanel1.add(jLabel4, null); jPanel1.add(jLabel5, null); jPanel1.add(jLabel6, null); jPanel1.add(jLabel7, null); jPanel1.add(jLabel8, null); jPanel1.add(jLabel9, null); jPanel1.add(jLabel10, null); jPanel1.add(jLabel11, null); jPanel1.add(offenseButton, null); jPanel1.add(defenseButton, null); jPanel1.add(firemaingunButton, null); jPanel1.add(fireauxgunButton, null); jPanel1.add(mainammoTextField, null); jPanel1.add(auxammoTextField, null); jPanel1.add(jLabel12, null); jPanel1.add(jLabel13, null); this.getContentPane().add(jPanel2, null); this.getContentPane().add(jPanel3, null); jPanel3.add(raisegunButton, null); jPanel3.add(brakesButton, null); jPanel3.add(lowergunButton, null); jPanel3.add(turretleftButton, null); jPanel3.add(turretrightButton, null); jPanel3.add(homeButton, null); jPanel3.add(newvehicleButton, null); jPanel3.add(gunelevTextField, null); jPanel3.add(jLabel14, null); } //Overriden so we can exit on System Close protected void processWindowEvent(WindowEvent e) { super.processWindowEvent(e); if (e.getID() == WindowEvent.WINDOW_CLOSING) { System.exit(0); } } //============================================================================================= // Implements AdjustmentListener //============================================================================================= public void adjustmentValueChanged(AdjustmentEvent evt){ if (evt.getSource().equals(speedScroll)){ speedPosition = evt.getValue(); // update the current slider position if (reverseState){ Integer temp1 = new Integer( -speedPosition); speedTextField.setText(temp1.toString()); // print new value in the textField System.out.println("Speed = " + temp1); ai.setSpeed((new Float(temp1.intValue())).floatValue()); repaint(); } else { Integer temp1 = new Integer(speedPosition); speedTextField.setText(temp1.toString()); // print new value in the textField System.out.println("Speed = " + temp1); ai.setSpeed((new Float(temp1.intValue())).floatValue()); repaint(); } // repaint the screen } // end else if // if the heading scroll is the source of the event else if (evt.getSource().equals(turnScroll)){ turnPosition = evt.getValue(); // update the current slider position if (turnPosition < 0){ if (!reverseState){ Integer temp1 = new Integer(turnPosition); turnTextField.setText(temp1.toString()); // print new value in the textField System.out.println("Angle of bank = " + temp1); ai.setTurnAngle((new Float(temp1.intValue())).floatValue()); repaint(); // repaint the screen } // end if else { Integer temp4 = new Integer(-turnPosition); turnTextField.setText(temp4.toString()); // print new value in the textField System.out.println("Angle of bank = " + temp4); ai.setTurnAngle((new Float(temp4.intValue())).floatValue()); repaint(); // repaint the screen } // end else } //end if else if (turnPosition == 0){ Integer temp2 = new Integer(0); turnTextField.setText(temp2.toString()); // print new value in the textField System.out.println("Angle of bank = " + temp2); ai.setTurnAngle((new Float(temp2.intValue())).floatValue()); repaint(); } // end else if else { if (!reverseState){ Integer temp3 = new Integer(turnPosition); turnTextField.setText(temp3.toString()); // print new value in the textField System.out.println("Angle of bank = " + temp3); ai.setTurnAngle((new Float(temp3.intValue())).floatValue()); repaint(); // repaint the screen }//end if else { Integer temp5 = new Integer(-turnPosition); turnTextField.setText(temp5.toString()); // print new value in the textField System.out.println("Angle of bank = " + temp5); ai.setTurnAngle((new Float(temp5.intValue())).floatValue()); repaint(); // repaint the screen } // end else } // end else } // end else if } // end adjustmentValueChanged() //============================================================================================= // Implements ActionListener //============================================================================================= public void actionPerformed(ActionEvent evt){ Object buttonName = evt.getSource(); if (buttonName.equals(newvehicleButton)) { ai.selfDestruct(); ai.reset(); ai.stopRun(); ai.setDriving(false); //stop sending pdus this.setVisible(false); dispose(); StartPanel sp = new StartPanel(500,100); sp.setVisible(true); }// end if /* // Remove all functionality of buttons on Agent Control Panel if (ai.getIsCollided() == false){ if (buttonName.equals(homeButton)){ turnScroll.setValue(0); turnTextField.setText("0"); speedScroll.setValue(0); speedTextField.setText("0"); ai.reset(); repaint(); } // end if else if (buttonName.equals(firemaingunButton)){ System.out.println("Fire Main Button Pressed"); if (mainAmmo > 0){ mainAmmo = mainAmmo - 1; mainammoTextField.setText((new Integer(mainAmmo)).toString()); ai.sendMainFirePdu();} } // end else if else if (buttonName.equals(fireauxgunButton)){ System.out.println("Fire COAX Button Pressed"); if (mgAmmo > 0){ mgAmmo = mgAmmo - 1; auxammoTextField.setText((new Integer(mgAmmo)).toString()); ai.sendFirePdu();} } // end else if else if (buttonName.equals(defenseButton)){ System.out.println("defense Button Pressed"); if (ai.getOffensive()){ ai.setDefensive(true); ai.setOffensive(false); offenseState = false; defenseState = true; repaint(); } } // end else if else if (buttonName.equals(offenseButton)){ System.out.println("offense Button Pressed"); if (ai.getDefensive()){ ai.setOffensive(true); ai.setDefensive(false); offenseState = true; defenseState = false; repaint(); } } // end else if else if (buttonName.equals(raisegunButton)){ System.out.println("raise Button Pressed"); downGunState = false; if(upGunState){ ai.elevateGun(); upGunState = false; return; } // end if ai.elevateGun(); repaint(); upGunState = true; } // end else if else if (buttonName.equals(lowergunButton)){ System.out.println("lower Button Pressed"); upGunState = false; if(downGunState){ ai.depressGun(); downGunState = false; return; } // end if ai.depressGun(); repaint(); downGunState = true; } // end else if else if (buttonName.equals(brakesButton)){ System.out.println("Brake Button Pressed"); turnScroll.setValue(0); turnTextField.setText("0"); speedScroll.setValue(0); speedTextField.setText("0"); ai.brake(); repaint(); } // end else if else if (buttonName.equals(turretleftButton)){ System.out.println("Lt Turret Button Pressed"); rightTurretTurnState = false; if(leftTurretTurnState){ ai.leftTurretTurn(); leftTurretTurnState = false; repaint(); return; } // end if leftTurretTurnState = true; ai.leftTurretTurn(); repaint(); } // end else if else if (buttonName.equals(turretrightButton)){ System.out.println("Rt turret Button Pressed"); leftTurretTurnState = false; if(rightTurretTurnState){ ai.rightTurretTurn(); rightTurretTurnState = false; repaint(); return; } // end if ai.rightTurretTurn(); repaint(); rightTurretTurnState = true; } // end else if else if (buttonName.equals(homeButton)){ reset(); ai.reset(); } //end else if reset else { System.out.println("Unknown button pressed"); } // end else }//end iscollided if else { System.out.println("control panel locked due to death"); } //end else */ } // end actionPerformed() //======================================================================= // Utility Methods //======================================================================= public void reset(){ turnScroll.setValue(0); turnTextField.setText("0"); speedScroll.setValue(0); speedTextField.setText("0"); mainammoTextField.setText("40"); mainAmmo = 40; auxammoTextField.setText("30"); mgAmmo = 30; repaint(); } //end reset public void resetAmmo(){ mainammoTextField.setText("40"); mainAmmo = 40; auxammoTextField.setText("30"); mgAmmo = 30; repaint(); } //end resetAmmo public void updateHeading(double mypsi){ if ( mypsi < 0 ) { headingTextField.setText(new Integer((int)( 360 + (((mypsi * 180) / Math.PI) % 360)) ).toString()); } else { headingTextField.setText(new Integer((int)((mypsi * 180) / Math.PI) % 360 ).toString()); } } public void updateGunElev(double angle){ gunelevTextField.setText(new Integer((int) ((angle * 180) / Math.PI)).toString()); } //============================================================================================= // Main //============================================================================================= public static void main(String[] args){ //AgentTankControlPanel fsm = new AgentTankControlPanel(860, 170); AgentTankControlPanel atcp = new AgentTankControlPanel("224.2.181.145", 62040, 15, (short)0, (short)1, (short)16, "Tank", false); atcp.show(); } // end main } // end of the class