C:\Java Projects\ElevationGUI\src\my\elevationgui\elevationGUI.java
/*
 * elevationGUI.java
 *
 * Created on January 16, 2007, 6:37 PM
 */

package my.elevationgui;

/**
 *
 * @author  Dusty Clark
 */
/* import the java libraries needed to support i/o and buffer operations */
import java.nio.*;
import java.text.*;
import javax.swing.*;
import java.awt.event.*;
        
public class elevationGUI extends javax.swing.JFrame {
    /* variables for support of the button callbacks and network i/o */
    
    double commandpos, commandvel, mode, current;
    double maxpos = 89.5;
    double minpos = 15.0;
    double maxvel = 50.0;
    int startup = 0;
        
    EasyUDP udpobj = new EasyUDP();
    byte[] tbuf = new byte[24];
    ByteBuffer txbuf = ByteBuffer.wrap(tbuf);
    byte[] rbuf = new byte[64];
    ByteBuffer rxbuf = ByteBuffer.wrap(rbuf);
    String rmtext;
    String rmhost = "brunel.mmto.arizona.edu";
    int txport = 4321;
    int rxport = 1234;
    Timer tictoc;
    
    /** Creates new form elevationGUI */
    public elevationGUI() {
        /* custom code for udp communications and a timer for listening
         * to udp packets to update the display data 
         */
         //construct the necessary portions of the udp send/receive object
        udpobj.setRemoteHost(rmhost);
        udpobj.setRxPort(rxport);
        udpobj.setTxPort(txport);

    //create the timer for listening to the current position from the network
        tictoc = new Timer(1000, new ActionListener() {
             public void actionPerformed(ActionEvent evt) {
                rxbuf.clear();
                udpobj.rcvPacket();
                rxbuf.put(udpobj.getRxBuffer());
                current = rxbuf.getDouble(0);
                
                NumberFormat numform = NumberFormat.getNumberInstance();
                numform.setMaximumFractionDigits(3);
                
                rmtext = numform.format(current);
                // update actual position value
                CurrentPosition.setText(rmtext);
                
                current = rxbuf.getDouble(8) * 3600.0;
                rmtext = numform.format(current);
                // update velocity value
                CurrentVelocity.setText(rmtext);
                
                current = rxbuf.getDouble(16) * 3600.0;
                rmtext = numform.format(current);
                // update position error value
                PositionError.setText(rmtext);
                
                //copy the current pos to the command pos on the other end
                if (startup < 1) {
                   txbuf.putDouble(0,0.0);
                   txbuf.putDouble(8,Double.parseDouble(CurrentPosition.getText()));
                   txbuf.putDouble(16,0.0);
                   udpobj.sendPacket(tbuf);
                   CommandPosition.setText(CurrentPosition.getText());
                   CommandVelocity.setText("0.0");
                }
                startup++;
                
                
             }
        });
        tictoc.start();
        
        initComponents();
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">                          
    private void initComponents() {
        CommandPosition = new javax.swing.JFormattedTextField();
        CommandVelocity = new javax.swing.JFormattedTextField();
        CurrentPosition = new javax.swing.JFormattedTextField();
        CurrentVelocity = new javax.swing.JFormattedTextField();
        PositionError = new javax.swing.JFormattedTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        SlewButton = new javax.swing.JButton();
        TrackButton = new javax.swing.JButton();
        Haltbutton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setText("Position Command");

        jLabel2.setText("Tracking Velocity Command");

        jLabel3.setText("Current Position");

        jLabel4.setText("Current Velocity");

        jLabel5.setText("Position Error, arcseconds");

        SlewButton.setText("Slew to...");
        SlewButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                SlewButtonMouseClicked(evt);
            }
        });

        TrackButton.setText("Track...");
        TrackButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                TrackButtonMouseClicked(evt);
            }
        });

        Haltbutton.setText("HALT");
        Haltbutton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                HaltbuttonMouseClicked(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addGap(44, 44, 44)
                                .addComponent(jLabel2))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(PositionError, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(CommandPosition, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(CurrentPosition, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGap(75, 75, 75)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(TrackButton, javax.swing.GroupLayout.DEFAULT_SIZE, 112, Short.MAX_VALUE)
                                            .addComponent(SlewButton, javax.swing.GroupLayout.DEFAULT_SIZE, 112, Short.MAX_VALUE)
                                            .addComponent(Haltbutton, javax.swing.GroupLayout.DEFAULT_SIZE, 112, Short.MAX_VALUE))
                                        .addGap(12, 12, 12))
                                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(CommandVelocity, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(27, 27, 27))))
                            .addComponent(CurrentVelocity, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(31, 31, 31))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel5)
                        .addContainerGap(191, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addContainerGap(249, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel4)
                        .addContainerGap(249, Short.MAX_VALUE))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(CommandVelocity, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(CommandPosition, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(32, 32, 32)
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(SlewButton)
                    .addComponent(CurrentPosition, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(28, 28, 28)
                .addComponent(jLabel4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(TrackButton)
                    .addComponent(CurrentVelocity, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(28, 28, 28)
                .addComponent(jLabel5)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(Haltbutton)
                    .addComponent(PositionError, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(26, Short.MAX_VALUE))
        );
        pack();
    }// </editor-fold>                        

    private void HaltbuttonMouseClicked(java.awt.event.MouseEvent evt) {                                        
// TODO add your handling code here:
        double commandpos;
        NumberFormat numform = NumberFormat.getNumberInstance();
        numform.setMaximumFractionDigits(3);
        
        commandpos = Double.parseDouble(CurrentPosition.getText());
        /* Mode == 0.0, Velocity 0.0, and copy the current command over */
        txbuf.putDouble(0,1.0);
        txbuf.putDouble(8,commandpos);
        txbuf.putDouble(16,0.0);
        udpobj.sendPacket(tbuf);
        //copy the halt values to the text windows
        CommandPosition.setText(numform.format(commandpos));
        CommandVelocity.setText("0.0");
        
        
    }                                       

    private void TrackButtonMouseClicked(java.awt.event.MouseEvent evt) {                                         
// TODO add your handling code here:
        double commandvel;
        NumberFormat numform = NumberFormat.getNumberInstance();
        numform.setMaximumFractionDigits(3);
        
        try {
            commandvel = Double.parseDouble(CommandVelocity.getText()) / 3600.0;
            if (commandvel > maxvel) {
            commandvel = maxvel;
            CommandVelocity.setText(numform.format(maxvel));
            } 
            if (commandvel < -maxvel) {
            commandvel = -maxvel;
            CommandVelocity.setText(numform.format(-maxvel));
            }
            txbuf.putDouble(16,commandvel);
            
        } catch (NumberFormatException ex) {
            CommandVelocity.setText("Bad entry.");
            txbuf.putDouble(16,1.0);
            
        }
        /* Mode == 2.0, starts from current position, send velocity command */
        txbuf.putDouble(0,3.0);
        txbuf.putDouble(8,Double.parseDouble(CurrentPosition.getText()));
        udpobj.sendPacket(tbuf);
        
    }                                        

    private void SlewButtonMouseClicked(java.awt.event.MouseEvent evt) {                                        
// TODO add your handling code here:
        double commandpos;
      
        NumberFormat numform = NumberFormat.getNumberInstance();
        numform.setMaximumFractionDigits(3);
        
        try {
            commandpos = Double.parseDouble(CommandPosition.getText());
            
            if (commandpos > maxpos) {
            commandpos = maxpos;
            } 
            if (commandpos < minpos) {
            commandpos = minpos;
            }
            txbuf.putDouble(8,commandpos);
            
        } catch (NumberFormatException ex) {
            CommandPosition.setText("Bad entry.");
            txbuf.putDouble(8,Double.parseDouble(CurrentPosition.getText()));
            
        }
        
        txbuf.putDouble(0,2.0);
        txbuf.putDouble(16,0.0);
        udpobj.sendPacket(tbuf);
        
    }                                       
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new elevationGUI().setVisible(true);
            }
        });
    }
    
    // Variables declaration - do not modify                     
    private javax.swing.JFormattedTextField CommandPosition;
    private javax.swing.JFormattedTextField CommandVelocity;
    private javax.swing.JFormattedTextField CurrentPosition;
    private javax.swing.JFormattedTextField CurrentVelocity;
    private javax.swing.JButton Haltbutton;
    private javax.swing.JFormattedTextField PositionError;
    private javax.swing.JButton SlewButton;
    private javax.swing.JButton TrackButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    // End of variables declaration                   
    
}