📄 jtrackergui.txt
字号:
/****************************************************************//* JTrackerGUI *//* *//****************************************************************/import java.awt.*;import java.awt.event.*;import javax.swing.*;import java.util.Random;import java.lang.Math;import javax.swing.BorderFactory;import javax.swing.border.Border;import javax.swing.border.EtchedBorder;import java.util.StringTokenizer;/** * JTracker Applet * * Created by: * Nicholas Rancourt, Timothy Lindblom, Ly Luu, and Daniel Peterson * * Advisor: Professor Antonia Papandreou-Suppappola * * Co-advisors: Amit Chhetri * *------------------------------------------------------------------ * This program simulates target tracking using a particle filter. *------------------------------------------------------------------ */public class JTrackerGUI extends JApplet implements Runnable{ // Variables declaration public JPanel contentPane; //----- public JLabel Seperator; public JTextField Yvel; public JTextField Xin; public JTextField Yin; public JTextField Xvel; public JTextField Xsensor; public JTextField Ysensor; public JTextField MCIter;// private JPanel lp; public JButton startButton; public JLabel jtracklabel; public JLabel paralabel; public JLabel ylabel; public JLabel xlabel; public JLabel xlabel22; public JLabel xlabel2; public JLabel pathLabel; public JLabel measLabel; public JTextField Qin; public JLabel legendlabel; public JLabel legend; public JTextArea helpinfo; public StringTokenizer tokenizer; public String token; public boolean success; public Object dialogInput; public double xact, yact, ract, rdotact, angleact, xin, yin, rin, rdotin, anglein; //-----// private JPanel rp; //----- private EPanel pathErrorPanel; //----- private EPanel measErrorPanel; //----- public int frame, delay, appletWidth = 600, appletHeight = 400, length=50, numParts=2000, niter=5, MC, deltaT; public Thread animator; public double[][] path, xhat, error; public double[] xmeas; public double[][] noise={{0.45,0.3617,0.9323},{1.4468,0.7572,1.1944},{2.7969,1.0852,1.35},{4.4138,1.3606,1.4659}}; public Random generator; public double xmax, ymax, xmin, ymin, xvelocity, yvelocity, xinit, yinit, Qval, wtotal, xsensor, ysensor, fps; private double phiStdvRad = Math.PI/180, rStdv = 10, rDotStdv = 10; private double phiVar, rVar, rdotVar; public Image jet, radar; public LeftPanel lp; public RightPanel rp; public CheckPanel cp; public boolean run, animating, ploterror, paused; private particleList parts, resampled; private MediaTracker mediaTracker; private JSeparator divLine0; public double sensorXmeter, sensorYmeter, jetXmeter, jetYmeter; public int sensorX, sensorY, sensorWidth, sensorHeight, jetX, jetY, jetWidth, jetHeight; public void init() {// Compute sensor variances from the standard deviations defined above. phiVar = Math.pow(phiStdvRad,2); rVar = Math.pow(rStdv,2); rdotVar = Math.pow(rDotStdv,2); // The Boolean run is used to determine if the algorithm has been run // since the applet was started. This is used for intial display // purposes. run=false; ploterror=false; animating = false; // lp, a LeftPanel (defined below) contains the inputs. lp = new LeftPanel(); cp = new CheckPanel(); // Load the images of the jet and radar jet=getImage(getCodeBase(), "jet2.gif"); radar=getImage(getCodeBase(), "radar.gif"); // MediaTracker is used to wait until the images are loaded before continuing. MediaTracker mediaTracker = new MediaTracker(this); mediaTracker.addImage(jet, 0); mediaTracker.addImage(radar, 1); try { mediaTracker.waitForID(0); mediaTracker.waitForID(1); } catch (InterruptedException ie) { System.err.println(ie); System.exit(1); } // rp, a RightPanel (defined below) contains the graph showing the target track. rp = new RightPanel(); // Create Layout with lp on left and rp on right. // getContentPane().add(new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, lp, rp)); setSize(appletWidth, appletHeight); // The frame variable keeps track of the time step currently executing. frame = 0; // Used by the thread. delay = (fps > 0) ? (int)(1000 / fps) : 100; // Initialize the particleLists for the particles before and after resampling. parts=new particleList(numParts); resampled=new particleList(numParts); // Create a random number generator to add random noise. generator = new Random(); // Display the images and axes rp.repaint(); contentPane = (JPanel)this.getContentPane(); //----- Seperator = new JLabel(); Yvel = new JTextField(); Xin = new JTextField(); Yin = new JTextField(); Xvel = new JTextField(); Xsensor = new JTextField(); Ysensor = new JTextField(); MCIter = new JTextField(); Qin = new JTextField(); // lp = new JPanel(); //----- jtracklabel = new JLabel(); paralabel = new JLabel(); ylabel = new JLabel(); xlabel = new JLabel(); xlabel22 = new JLabel("Time step"); xlabel2 = new JLabel("Time step"); pathLabel = new JLabel("Estimation error in meters"); measLabel = new JLabel("Measurement error in meters"); legendlabel = new JLabel(); legend = new JLabel(); helpinfo = new JTextArea(); contentPane = (JPanel)this.getContentPane(); //----- // rp = new JPanel(); //----- pathErrorPanel = new EPanel();// pathErrorPanel.setToolTipText( "Distance between the actual position of the target and the estimated \n position in meters. Averaged after each MC iteration."); pathErrorPanel.addMouseListener(new pathmouse()); //----- measErrorPanel = new EPanel();// measErrorPanel.setToolTipText( "Distance between the actual position of the target and the measurement of the position in meters. Averaged after each MC iteration."); measErrorPanel.addMouseListener(new measmouse()); //----- startButton = new JButton(); // // jtracklabel // Icon jtrack = new ImageIcon( getClass().getResource("Jtracker.JPG")); jtracklabel.setIcon(jtrack); // // paralabel // Icon para = new ImageIcon( getClass().getResource("parameters2.JPG")); paralabel.setIcon(para); // // ylabel // Icon ybar = new ImageIcon( getClass().getResource("ybar.JPG")); ylabel.setIcon(ybar); // // xlabel // Icon xbar = new ImageIcon( getClass().getResource("xbar.JPG")); xlabel.setIcon(xbar); // // xlabel22 // //Icon xbar22 = new ImageIcon( getClass().getResource("xbar2.JPG")); //xlabel22.setIcon(xbar22); // // xlabel2 // //Icon xbar2 = new ImageIcon( getClass().getResource("xbar2.JPG")); //xlabel2.setIcon(xbar2); Icon legend12 = new ImageIcon( getClass().getResource("legend12.JPG")); legendlabel.setIcon(legend12); Icon legend13 = new ImageIcon( getClass().getResource("legend13.JPG")); legend.setIcon(legend13); divLine0 = new JSeparator(); helpinfo.setBackground(new Color(227, 228, 230)); helpinfo.setForeground(new Color(1, 1, 1)); helpinfo.setOpaque(true);// contentPane // contentPane.setLayout(null); contentPane.setBackground(new Color(203, 221, 200)); addComponent(contentPane, jtracklabel, -2,-1,233,104); addComponent(contentPane, paralabel, -5,101,123,332); addComponent(contentPane, ylabel, 250,-1,40,321); //addComponent(contentPane, xlabel, 245,317,455,8); addComponent(contentPane, rp, 290,0,500,320); addComponent(contentPane, pathLabel, 520,325,160,14); addComponent(contentPane, pathErrorPanel, 400,340,390,60); addComponent(contentPane, xlabel2, 560,400,80,14); addComponent(contentPane, divLine0, 400, 414, 390, 3); addComponent(contentPane, measLabel, 520,415,170,14); addComponent(contentPane, measErrorPanel, 400,430,390,60); addComponent(contentPane, xlabel22, 560,490,80,14); addComponent(contentPane, helpinfo, 250,513,540,45); addComponent(contentPane, legendlabel, 0,434,170,120); addComponent(contentPane, legend, 100,510,180,50); addComponent(contentPane, cp, 235, 325, 160, 185); // // rp // rp.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5)); rp.setBackground(new Color(6, 226, 252)); // // pathErrorPanel // pathErrorPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5)); pathErrorPanel.setBackground(new Color(6, 226, 252)); // // measErrorPanel // measErrorPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5)); // // contentPane // contentPane.setLayout(null); contentPane.setBackground(new Color(203, 221, 200)); addComponent(contentPane, lp, 119,101,110,414); // this.setLocation(new Point(3, -6)); this.setSize(new Dimension(800, 560)); } /////////end of inti class// This method is called when the "Start" button is pressed. It starts the thread that // enables the animation. public void startThread() { animator=new Thread(this); animator.start(); animating=true; run = true; } private class pathmouse implements MouseListener { public void mousePressed(MouseEvent e) {} public void mouseReleased(MouseEvent e) {} public void mouseClicked(MouseEvent e) {} public void mouseEntered(MouseEvent e) { helpinfo.setLineWrap(true); helpinfo.setText("Distance between the actual position of the target and the estimated position in meters. Averaged after each MC iteration."); } public void mouseExited(MouseEvent e) {} } private class measmouse implements MouseListener { public void mousePressed(MouseEvent e) {} public void mouseReleased(MouseEvent e) {} public void mouseClicked(MouseEvent e) {} public void mouseEntered(MouseEvent e) { helpinfo.setLineWrap(true); helpinfo.setText("Distance between the actual position of the target and the measurement of the position in meters. Averaged after each MC iteration."); } public void mouseExited(MouseEvent e) {} }//This method is called by the thread that was created in //the startThread method. It does the main animation. public void run() { // Remember the starting time long tm = System.currentTimeMillis(); while (Thread.currentThread() == animator) { // Delay depending on how far we are behind. try { tm += delay; Thread.sleep(Math.max(0, tm - System.currentTimeMillis())); } catch (InterruptedException e) { break; } // Advance the frame number and display the frame. if (frame<length-1) { frame++; rp.repaint(); } // If the last frame is reached, stop animation. else { MC=MC-1; for (int count=0; count<length; count++) { double newError =Math.sqrt(Math.pow((path[0][count]-xhat[0][count]),2)+Math.pow((path[1][count]-xhat[1][count]),2)); pathErrorPanel.error[count]=(newError+(niter-MC-1)*pathErrorPanel.error[count])/(niter-MC); } ploterror=true; pathErrorPanel.repaint(); measErrorPanel.repaint(); if (MC==0) { animator = null; animating = false; jetXmeter = path[0][length-1]; jetYmeter = path[1][length-1]; } else { frame=0; animator = null; animating = false; if(cp.pauseBox.isSelected()) paused = true; else { xhat[0][0]=0; xhat[1][0]=0; xhat[2][0]=0; xhat[3][0]=0; for (int i=0; i<numParts; i++) { parts.xPosition[i]=jetXmeter+100*generator.nextGaussian(); parts.yPosition[i]=jetYmeter+100*generator.nextGaussian(); parts.xVelocity[i]=xvelocity+7.07107*generator.nextGaussian(); parts.yVelocity[i]=yvelocity+7.07107*generator.nextGaussian(); xhat[0][0]=xhat[0][0]+parts.xPosition[i]; xhat[1][0]=xhat[1][0]+parts.yPosition[i]; xhat[2][0]=xhat[2][0]+parts.xVelocity[i]; xhat[3][0]=xhat[3][0]+parts.yVelocity[i]; } xhat[0][0]=xhat[0][0]/numParts; xhat[1][0]=xhat[1][0]/numParts; xhat[2][0]=xhat[2][0]/numParts; xhat[3][0]=xhat[3][0]/numParts; startThread(); animating = true; } } } } }// This method is called when the applet is no longer // visible. Set the animator variable to null so that the // thread will exit before displaying the next frame. public void stop() { animator = null; animating = false; } /* * The RightPanel class contains the graphics component that plots the paths and particles. * It also contains methods to allow dragging of the jet and radar images. */ private class RightPanel extends JPanel implements MouseListener, MouseMotionListener { private int x1, y1, x2, y2, x3, y3, xhat1, xhat2, yhat1, yhat2; // various pixel coordinates private boolean dragging; // This is true when dragging is in progress. private boolean draggingjet; // This is true when dragging the jet. private int offsetx, offsety; // The distance from the upper left corner of the // Sensor to the point where the user clicked // the rect. This offset is maintained as the // rect is dragged. /* Constructor */ public RightPanel() { //setBackground(Color.white); // Add a MouseListener to read mouse events (clicking and dragging) in the // graphics component. addMouseListener(this); addMouseMotionListener(this); // Sensor position is initially set at (0,0). sensorXmeter=0; sensorYmeter=0; sensorWidth=16; sensorHeight=16; // Jet (target) position is initially set at (500,500) meters. jetXmeter = 500; jetYmeter = 500; jetWidth = 63; jetHeight = 28; // The window is initially set to display from (0,0) to (1000,1000) meters. // These variables determine the range of the display, and are automatically // changed to keep the target path and the sensor on the display. xmin = 0; ymin = 0; xmax = 1000; ymax = 1000; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -