package demo.helicopter; import java.awt.*; import java.awt.event.*; import javax.swing.*; import mil.navy.nps.dis.*; public class HeloControlPanel extends JFrame implements AdjustmentListener, ActionListener{ JPanel jPanel1 = new JPanel(); JPanel jPanel2 = new JPanel(); JPanel jPanel3 = new JPanel(); JButton raisenoseButton = new JButton(); JButton hoverButton = new JButton(); JButton lowernoseButton = new JButton(); JButton leftturnButton = new JButton(); JButton rightturnButton = new JButton(); JButton homeButton = new JButton(); JButton newvehicleButton = new JButton(); JTextField noseTextField = new JTextField(); JLabel jLabel1 = new JLabel(); JScrollBar bankScroll = new JScrollBar(); JScrollBar speedScroll = new JScrollBar(); JScrollBar torqueScroll = new JScrollBar(); 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(); JLabel jLabel12 = new JLabel(); JLabel jLabel13 = new JLabel(); JTextField torqueTextField = new JTextField(); JTextField speedTextField = new JTextField(); JTextField bankTextField = new JTextField(); JTextField headingTextField = new JTextField(); JTextField abovegroundTextField = new JTextField(); JLabel jLabel14 = new JLabel(); JLabel jLabel15 = new JLabel(); JLabel jLabel16 = new JLabel(); JLabel jLabel17 = new JLabel(); JLabel jLabel18 = new JLabel(); JTextField altitudeTextField = new JTextField(); JLabel jLabel19 = new JLabel(); JButton fireButton = new JButton(); JTextField rocketsTextField = new JTextField(); JLabel jLabel20 = new JLabel(); // holders for current position on each of the scroll bars private static int torquePosition = 50; private static int bankPosition = 0; private static int speedPosition = 0; private static int mainAmmo; private static int noseAttitude = 0; // 0 = not turning; 1 = turning private static boolean leftPedalTurnState; private static boolean rightPedalTurnState; // Creates an ActionInterpreter to listen and send PDU's private static HeloActionInterpreter ai; //Construct the frame public HeloControlPanel(String ipAddr, int portNum, int timeToLive, short siteNum, short appNum, short id, String marking, boolean rtpHeaderEnabled) { System.out.println ("HeloControlPanel constructor, marking=" + marking); enableEvents(AWTEvent.WINDOW_EVENT_MASK); ai = new HeloActionInterpreter(ipAddr, portNum, timeToLive, siteNum, appNum, id, marking, rtpHeaderEnabled, this); try { jbInit(); } catch (Exception e) { e.printStackTrace(); } leftPedalTurnState = false; rightPedalTurnState = false; mainAmmo = 14; System.out.println("Before start AI"); ai.start(); } //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.setFont(new Font("Courier", 1, 12)); jPanel1.setLayout(null); jPanel1.setBackground(new Color(152, 152, 152)); jPanel1.setBounds(new Rectangle(0, 0, 370, 150)); jPanel2.setLayout(null); jPanel2.setBackground(new Color(212, 131, 81)); jPanel2.setBounds(new Rectangle(375, 0, 185, 150)); jPanel3.setLayout(null); jPanel3.setBackground(new Color(152, 152, 152)); jPanel3.setBounds(new Rectangle(565, 0, 195, 150)); raisenoseButton.setFont(new Font("SansSerif", Font.BOLD, 10)); raisenoseButton.setText("Nose Up"); raisenoseButton.setBounds(new Rectangle(97, 38, 92, 34)); raisenoseButton.addActionListener(this); hoverButton.setFont(new Font("SansSerif", Font.BOLD, 10)); hoverButton.setText("Hover"); hoverButton.setBounds(new Rectangle(1, 76, 94, 34)); hoverButton.addActionListener(this); lowernoseButton.setFont(new Font("SansSerif", Font.BOLD, 10)); lowernoseButton.setText("Nose Down"); lowernoseButton.setBounds(new Rectangle(97, 76, 92, 34)); lowernoseButton.addActionListener(this); leftturnButton.setFont(new Font("SansSerif", Font.BOLD, 10)); leftturnButton.setText("Left Turn"); leftturnButton.setBounds(new Rectangle(1, 114, 94, 34)); leftturnButton.addActionListener(this); rightturnButton.setFont(new Font("SansSerif", Font.BOLD, 10)); rightturnButton.setText("Right Turn"); rightturnButton.setBounds(new Rectangle(97, 114, 92, 34)); rightturnButton.addActionListener(this); homeButton.setFont(new Font("SansSerif", Font.BOLD, 10)); homeButton.setText("Home"); homeButton.setBounds(new Rectangle(1, 1, 94, 34)); homeButton.addActionListener(this); newvehicleButton.setFont(new Font("SansSerif", Font.BOLD, 9)); newvehicleButton.setText("New Vehicle"); newvehicleButton.setBounds(new Rectangle(1, 38, 94, 34)); newvehicleButton.addActionListener(this); noseTextField.setText("0"); noseTextField.setBounds(new Rectangle(123, 1, 36, 20)); noseTextField.setHorizontalAlignment(JTextField.CENTER); noseTextField.setEditable(false); jLabel1.setForeground(Color.black); jLabel1.setText("Nose Angle"); jLabel1.setBounds(new Rectangle(113, 19, 65, 16)); bankScroll.setForeground(Color.black); bankScroll.setVisibleAmount(5); bankScroll.setOrientation(JScrollBar.HORIZONTAL); bankScroll.setBounds(new Rectangle(1, 115, 154, 20)); bankScroll.setMaximum(64); bankScroll.setMinimum(-59); bankScroll.addAdjustmentListener(this); speedScroll.setVisibleAmount(5); speedScroll.setOrientation(JScrollBar.HORIZONTAL); speedScroll.setBounds(new Rectangle(1, 65, 154, 20)); speedScroll.setMaximum(125); speedScroll.addAdjustmentListener(this); torqueScroll.setVisibleAmount(5); torqueScroll.setOrientation(JScrollBar.HORIZONTAL); torqueScroll.setBounds(new Rectangle(1, 15, 154, 20)); torqueScroll.setMinimum(29); torqueScroll.setMaximum(105); torqueScroll.setValue(64); torqueScroll.addAdjustmentListener(this); jLabel2.setForeground(new Color(0, 90, 0)); jLabel2.setText("29"); jLabel2.setBounds(new Rectangle(12, 35, 15, 12)); jLabel3.setForeground(new Color(0, 90, 0)); jLabel3.setText("64"); jLabel3.setBounds(new Rectangle(68, 35, 16, 14)); jLabel4.setForeground(new Color(0, 90, 0)); jLabel4.setText("100"); jLabel4.setBounds(new Rectangle(126, 35, 24, 13)); jLabel5.setForeground(new Color(0, 90, 0)); jLabel5.setText("Torque (vertical thrust)"); // jLabel5.setBounds(new Rectangle(56, 1, 41, 14)); jLabel5.setBounds(new Rectangle(13, 1, 150, 14)); jLabel6.setForeground(Color.blue); jLabel6.setText("0"); jLabel6.setBounds(new Rectangle(14, 85, 12, 13)); jLabel7.setForeground(Color.blue); jLabel7.setText("60"); jLabel7.setBounds(new Rectangle(68, 85, 18, 14)); jLabel8.setForeground(Color.blue); jLabel8.setText("120"); jLabel8.setBounds(new Rectangle(126, 85, 25, 17)); jLabel9.setForeground(Color.blue); jLabel9.setText("Speed"); jLabel9.setBounds(new Rectangle(57, 50, 38, 14)); jLabel10.setForeground(Color.black); jLabel10.setText("-59"); jLabel10.setBounds(new Rectangle(8, 135, 22, 14)); jLabel11.setForeground(Color.black); jLabel11.setText("0"); jLabel11.setBounds(new Rectangle(71, 135, 10, 15)); jLabel12.setForeground(Color.black); jLabel12.setText("+59"); jLabel12.setBounds(new Rectangle(123, 135, 23, 15)); jLabel13.setForeground(Color.black); jLabel13.setText("Angle of Bank"); jLabel13.setBounds(new Rectangle(38, 100, 78, 13)); torqueTextField.setText("64"); torqueTextField.setBounds(new Rectangle(174, 1, 33, 20)); torqueTextField.setHorizontalAlignment(JTextField.CENTER); torqueTextField.setEditable(false); speedTextField.setText("0"); speedTextField.setBounds(new Rectangle(174, 51, 33, 20)); speedTextField.setHorizontalAlignment(JTextField.CENTER); speedTextField.setEditable(false); bankTextField.setText("0"); bankTextField.setBounds(new Rectangle(174, 101, 33, 20)); bankTextField.setHorizontalAlignment(JTextField.CENTER); bankTextField.setEditable(false); headingTextField.setText("360"); headingTextField.setBounds(new Rectangle(311, 1, 33, 20)); headingTextField.setHorizontalAlignment(JTextField.CENTER); headingTextField.setEditable(false); abovegroundTextField.setText("yes"); abovegroundTextField.setBounds(new Rectangle(240, 1, 33, 20)); abovegroundTextField.setHorizontalAlignment(JTextField.CENTER); abovegroundTextField.setEditable(false); jLabel14.setForeground(new Color(0, 90, 0)); jLabel14.setText("Torque"); jLabel14.setBounds(new Rectangle(171, 22, 43, 14)); jLabel15.setForeground(Color.blue); jLabel15.setText("Speed"); jLabel15.setBounds(new Rectangle(172, 71, 38, 15)); jLabel16.setForeground(Color.black); jLabel16.setText("Bank"); jLabel16.setBounds(new Rectangle(176, 122, 30, 17)); jLabel17.setForeground(Color.black); jLabel17.setText("Altitude"); jLabel17.setBounds(new Rectangle(237, 72, 43, 15)); jLabel18.setForeground(Color.black); jLabel18.setText("Heading"); jLabel18.setBounds(new Rectangle(304, 22, 47, 14)); altitudeTextField.setText("yes"); altitudeTextField.setBounds(new Rectangle(240, 51, 33, 20)); altitudeTextField.setHorizontalAlignment(JTextField.CENTER); altitudeTextField.setEditable(false); jLabel19.setForeground(Color.black); jLabel19.setText("Above Ground"); jLabel19.setBounds(new Rectangle(218, 22, 82, 14)); fireButton.setFont(new Font("SansSerif", Font.BOLD, 10)); fireButton.setText("Fire Rockets"); fireButton.setBounds(new Rectangle(269, 114, 100, 33)); fireButton.addActionListener(this); rocketsTextField.setText("14"); rocketsTextField.setBounds(new Rectangle(302, 80, 33, 20)); rocketsTextField.setHorizontalAlignment(JTextField.CENTER); rocketsTextField.setEditable(false); jLabel20.setForeground(Color.black); jLabel20.setText("Rockets Onboard"); jLabel20.setBounds(new Rectangle(269, 98, 100, 15)); this.setResizable(false); this.setTitle("Helo Control Panel " + frameTitle); this.getContentPane().add(jPanel1, null); jPanel1.add(bankScroll, null); jPanel1.add(speedScroll, null); jPanel1.add(torqueScroll, 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(jLabel12, null); jPanel1.add(jLabel13, null); jPanel1.add(torqueTextField, null); jPanel1.add(speedTextField, null); jPanel1.add(bankTextField, null); jPanel1.add(headingTextField, null); jPanel1.add(abovegroundTextField, null); jPanel1.add(jLabel14, null); jPanel1.add(jLabel15, null); jPanel1.add(jLabel16, null); jPanel1.add(jLabel17, null); jPanel1.add(jLabel18, null); jPanel1.add(altitudeTextField, null); jPanel1.add(jLabel19, null); jPanel1.add(fireButton, null); jPanel1.add(rocketsTextField, null); jPanel1.add(jLabel20, null); this.getContentPane().add(jPanel2, null); this.getContentPane().add(jPanel3, null); jPanel3.add(raisenoseButton, null); jPanel3.add(hoverButton, null); jPanel3.add(lowernoseButton, null); jPanel3.add(leftturnButton, null); jPanel3.add(rightturnButton, null); jPanel3.add(homeButton, null); jPanel3.add(newvehicleButton, null); jPanel3.add(noseTextField, null); jPanel3.add(jLabel1, 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 the power scrollbar is the source of the event if (evt.getSource().equals(torqueScroll) && ai.getIsCollided() == false){ torquePosition = evt.getValue(); // update the current slider position int currentTorque = torquePosition; // invert the values for the display System.out.println("Torgue = " + currentTorque); Integer temp = new Integer(currentTorque); torqueTextField.setText(temp.toString()); // print new value in the textField ai.setTorque((new Float(currentTorque)).floatValue()); repaint(); // repaint the screen } // end if // if the speed scrollbar is the source of the event else if (evt.getSource().equals(speedScroll) && ai.getIsCollided() == false){ speedPosition = evt.getValue(); // update the current slider position 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(bankScroll) && ai.getIsCollided() == false){ bankPosition = evt.getValue(); // update the current slider position if (bankPosition < 0){ Integer temp1 = new Integer( bankPosition); bankTextField.setText(temp1.toString()); // print new value in the textField System.out.println("Angle of bank = " + temp1); ai.setBankAngle((new Float(temp1.intValue())).floatValue()); repaint(); // repaint the screen } // end if else if (bankPosition == 0){ Integer temp2 = new Integer(0); bankTextField.setText(temp2.toString()); // print new value in the textField System.out.println("Angle of bank = " + temp2); ai.setBankAngle((new Float(temp2.intValue())).floatValue()); repaint(); } // end else if else { Integer temp3 = new Integer( bankPosition); bankTextField.setText(temp3.toString()); // print new value in the textField System.out.println("Angle of bank = " + temp3); ai.setBankAngle((new Float(temp3.intValue())).floatValue()); repaint(); // repaint the screen } // end else } // end else if } // end adjustmentValueChanged() //============================================================================================= // Implements ActionListener //============================================================================================= public void actionPerformed(ActionEvent evt){ Object source = evt.getSource(); //String text = evt.getActionCommand(); if (source.equals(homeButton)){ reset(); } // end if else if (source.equals(fireButton) && ai.getIsCollided() == false){ System.out.println("Fire Button Pressed"); if (mainAmmo > 0){ mainAmmo = mainAmmo - 1; rocketsTextField.setText((new Integer(mainAmmo)).toString()); ai.sendFirePdu();} } // end else if else if (source.equals(lowernoseButton) && ai.getIsCollided() == false){ System.out.println("Lower Nose Button Pressed"); ai.lowerNose(); repaint(); } // end else if else if (source.equals(raisenoseButton) && ai.getIsCollided() == false){ System.out.println("Raise Nose Button Pressed"); ai.raiseNose(); repaint(); } // end else if else if (source.equals(hoverButton) && ai.getIsCollided() == false){ System.out.println("Hover Button Pressed"); bankScroll.setValue(0); bankTextField.setText("0"); speedScroll.setValue(0); speedTextField.setText("0"); torqueScroll.setValue(64); torqueTextField.setText("64"); ai.hover(); repaint(); } // end else if else if (source.equals(leftturnButton) && ai.getIsCollided() == false){ System.out.println("Left PT Button Pressed"); EntityStatePdu temp = ai.getEspdu(); if(leftPedalTurnState == true){ temp.setEntityAngularVelocityZ(0); leftPedalTurnState = false; return; } // end if // ensure helo is hovering before a left pedal turn is attempted if (temp.getEntityLinearVelocityX() != 0 || temp.getEntityLinearVelocityY() != 0 || temp.getEntityLinearVelocityZ() != 0 || temp.getEntityAngularVelocityX() != 0 || temp.getEntityAngularVelocityY() != 0 || temp.getEntityAngularVelocityZ() != 0){ headingTextField.setText(new Double((temp.getEntityOrientationPsi() * 180) / Math.PI).toString()); bankScroll.setValue(0); bankTextField.setText("0"); speedScroll.setValue(0); speedTextField.setText("0"); torqueScroll.setValue(64); torqueTextField.setText("64"); ai.hover(); repaint(); } // end if leftPedalTurnState = true; ai.leftPedalTurn(); repaint(); } // end else if else if (source.equals(rightturnButton) && ai.getIsCollided() == false){ System.out.println("Right PT Button Pressed"); EntityStatePdu temp1 = ai.getEspdu(); if(rightPedalTurnState == true){ temp1.setEntityAngularVelocityZ(0); rightPedalTurnState = false; return; } // end if // ensure helo is hovering before a left pedal turn is attempted if (temp1.getEntityLinearVelocityX() != 0 || temp1.getEntityLinearVelocityY() != 0 || temp1.getEntityLinearVelocityZ() != 0 || temp1.getEntityAngularVelocityX() != 0 || temp1.getEntityAngularVelocityY() != 0 || temp1.getEntityAngularVelocityZ() != 0){ bankScroll.setValue(0); bankTextField.setText("0"); speedScroll.setValue(0); speedTextField.setText("0"); torqueScroll.setValue(64); torqueTextField.setText("64"); ai.hover(); repaint(); } // end if ai.rightPedalTurn(); repaint(); rightPedalTurnState = true; } // end else if else if (source.equals(newvehicleButton) && ai.getIsCollided() == false){ ai.selfDestruct(); ai.reset(); ai.stopRun(); // stop BSB reading ai.setFlying(false); // stop sending Pdus this.setVisible(false); dispose(); StartPanel sp = new StartPanel(500, 250); sp.setVisible(true); } // end else if else { System.out.println("Unknown button pressed"); } // end else } // end actionPerformed() //============================================================================================= // Utility Methods //============================================================================================= public void reset(){ bankScroll.setValue(0); bankTextField.setText("0"); speedScroll.setValue(0); speedTextField.setText("0"); torqueScroll.setValue(64); torqueTextField.setText("64"); rocketsTextField.setText("14"); mainAmmo = 14; ai.reset(); repaint(); } // end reset() public void resetAmmo(){ rocketsTextField.setText("14"); mainAmmo = 14; 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 updateAltitude(double height, double agl){ altitudeTextField.setText(new Integer((int) - height).toString()); double delta = height - agl; abovegroundTextField.setText(new Integer((int) - delta).toString()); } public void updateNoseAttitude(int speed, int noseAttitude){ noseTextField.setText(new Integer(noseAttitude).toString()); speedPosition = speed; speedScroll.setValue(speed); speedTextField.setText(new Integer(speedPosition).toString()); repaint(); } //============================================================================================= // Main //============================================================================================= public static void main(String[] args){ HeloControlPanel hcp = new HeloControlPanel("224.2.181.145", 62040, 15, (short)0, (short)1, (short)10, "Helicopter", false); hcp.show(); } // end main }