import java.awt.*; import java.awt.event.*; import java.applet.*; import java.net.URL; public class rates extends Applet { CurvePanel mainpanel; Button restartButton, mathReviewButton, applicationButton, startButton; Panel buttonpanel = new Panel(); Panel dialogPanel = new Panel(); Scrollbar xPosSlider, yPosSlider, xVelSlider, yVelSlider; int xInitPos = 20; int yInitPos = 20; int xVel = -30; int yVel = -20; int xInitPos0 = 20; int yInitPos0 = 20; int xVel0 = -30; int yVel0 = -20; int xPos=xInitPos,yPos=yInitPos; private static final int RADIUS = 3; int time=0; int delay=20; int topTime=1; int xDir,yDir; int tempTopTime = 1; Thread runner = null; // Thread for doing the drifing animation Image OSC = null; // Off-screen canvas for double buffering, created in doResize() Graphics OSCGraphics; // Graphics context for OSC AdjustmentListener sliderListener = new AdjustmentListener() { public void adjustmentValueChanged( AdjustmentEvent e ) { xInitPos=xPosSlider.getValue(); yInitPos=yPosSlider.getValue(); xVel=xVelSlider.getValue(); yVel=yVelSlider.getValue(); tempTopTime = topTime; topTime = 1; mainpanel.run(); topTime = tempTopTime; mainpanel.repaint(); } }; ActionListener restartListener = new ActionListener() { public void actionPerformed (ActionEvent e) { runner=null; xInitPos = xInitPos0; yInitPos = yInitPos0; xVel = xVel0; yVel = yVel0; time=0; topTime=1; mainpanel.run(); } }; ActionListener mathReviewListener = new ActionListener() { public void actionPerformed (ActionEvent e) { mainpanel.repaint(); } }; ActionListener applicationListener = new ActionListener() { public void actionPerformed (ActionEvent e) { mainpanel.repaint(); } }; ActionListener startListener = new ActionListener() { public void actionPerformed (ActionEvent e) { runner=null; topTime=16; xPos = xInitPos; yPos = yInitPos; mainpanel.init(); } }; public void init() { setBackground(Color.white); mainpanel = new CurvePanel(); setLayout(new BorderLayout()); add("Center",mainpanel); mainpanel.init(); add("South",dialogPanel); dialogPanel.setLayout(new GridLayout(1,6)); xPosSlider = new Scrollbar(Scrollbar.HORIZONTAL,20,1,-30,31); xPosSlider.addAdjustmentListener(sliderListener); xPosSlider.setBlockIncrement(2); dialogPanel.add(xPosSlider); yPosSlider = new Scrollbar(Scrollbar.HORIZONTAL,20,1,-30,31); yPosSlider.addAdjustmentListener(sliderListener); yPosSlider.setBlockIncrement(2); dialogPanel.add(yPosSlider); xVelSlider = new Scrollbar(Scrollbar.HORIZONTAL,-30,1,-30,31); xVelSlider.addAdjustmentListener(sliderListener); xVelSlider.setBlockIncrement(2); dialogPanel.add(xVelSlider); yVelSlider = new Scrollbar(Scrollbar.HORIZONTAL,-20,1,-30,31); yVelSlider.addAdjustmentListener(sliderListener); yVelSlider.setBlockIncrement(2); dialogPanel.add(yVelSlider); restartButton = new Button("restart"); restartButton.addActionListener(restartListener); dialogPanel.add(restartButton); startButton = new Button("start"); startButton.addActionListener(startListener); dialogPanel.add(startButton); } public class CurvePanel extends Panel implements Runnable{ public void init(){ this.start(); } public void steam(){ doFrame(); OSCGraphics.setColor(Color.black); OSCGraphics.drawLine(0,0,800-1,0);OSCGraphics.drawLine(800-1,0,800-1,400-1); //box it OSCGraphics.drawLine(800-1,400-1,0,400-1);OSCGraphics.drawLine(0,400-1,0,0); OSCGraphics.drawLine(20,210,350,210); OSCGraphics.drawLine(160,20,160,350); //Pos axes OSCGraphics.drawLine(400,350,710,350); OSCGraphics.drawLine(400,20,400,350); //dist axes OSCGraphics.drawString("time",704,347);OSCGraphics.drawString("distance",403,25); OSCGraphics.drawString("N",156,18);OSCGraphics.drawString("S",156,362); OSCGraphics.drawString("E",355,213);OSCGraphics.drawString("W",8,213); OSCGraphics.drawString("y",163,27);OSCGraphics.drawString("x",346,202); OSCGraphics.drawString("Intial X pos. = "+xInitPos,20,373); OSCGraphics.drawString("Intial Y pos. = "+yInitPos,140,373); OSCGraphics.drawString("X vel. = "+xVel,300,373); OSCGraphics.drawString("Y vel. = "+yVel,440,373); for (int i = -30; i<=40; i+=10) {//Pos ticks int y=210-4*i; OSCGraphics.drawLine(155, y, 165, y); if (i!=0) OSCGraphics.drawString(""+i,168,y); int x=160+4*i; OSCGraphics.drawLine(x, 205, x, 215); OSCGraphics.drawString(""+i,x+2,207); } xPos=xInitPos+(int)(time*xVel/10.0); yPos=yInitPos+(int)(time*yVel/10.0); OSCGraphics.setColor(Color.red); //X ship OSCGraphics.fillOval(160+4*xPos-RADIUS,210-RADIUS,2*RADIUS,2*RADIUS); xDir=1; if(xVel<0) xDir=-1; OSCGraphics.drawLine(160+4*xPos+11*xDir,210,160+4*xPos+4*xDir,218); OSCGraphics.drawLine(160+4*xPos+4*xDir,218,160+4*xPos-10*xDir,216); OSCGraphics.drawLine(160+4*xPos-10*xDir,216,160+4*xPos-10*xDir,204); OSCGraphics.drawLine(160+4*xPos-10*xDir,204,160+4*xPos+4*xDir,202); OSCGraphics.drawLine(160+4*xPos+4*xDir,202,160+4*xPos+11*xDir,210); OSCGraphics.setColor(Color.blue); //Y ship OSCGraphics.fillOval(160-RADIUS,210-4*yPos-RADIUS,2*RADIUS,2*RADIUS); yDir=1; if(yVel<0) yDir=-1; OSCGraphics.drawLine(160,210-4*yPos-11*yDir,168,210-4*yPos-4*yDir); OSCGraphics.drawLine(168,210-4*yPos-4*yDir,166,210-4*yPos+10*yDir); OSCGraphics.drawLine(166,210-4*yPos+10*yDir,154,210-4*yPos+10*yDir); OSCGraphics.drawLine(154,210-4*yPos+10*yDir,152,210-4*yPos-4*yDir); OSCGraphics.drawLine(152,210-4*yPos-4*yDir,160,210-4*yPos-11*yDir); OSCGraphics.setColor(Color.green); //distance connector OSCGraphics.drawLine(160+4*xPos,210,160,210-4*yPos); OSCGraphics.drawLine(400+20*time,350,400+20*time,350-(int)(4*Math.sqrt(xPos*xPos+yPos*yPos)+.5)); OSCGraphics.setColor(Color.black); for (int i=0;i<4*time;i++){ int z1=350-(int)(4*Math.sqrt((xInitPos+i*xVel/40.0)*(xInitPos+i*xVel/40.0)+ (yInitPos+i*yVel/40.0)*(yInitPos+i*yVel/40.0))); int z2=350-(int)(4*Math.sqrt((xInitPos+(i+1)*xVel/40.0)*(xInitPos+(i+1)*xVel/40.0)+ (yInitPos+(i+1)*yVel/40.0)*(yInitPos+(i+1)*yVel/40.0))); OSCGraphics.drawLine(400+5*i,z1,400+5*(i+1),z2); } repaint(); } public void update(Graphics g) { // override update(), so that it doesn't erase before calling paint() paint(g); } void doFrame() { OSC = null; OSC = createImage(800,400); OSCGraphics = OSC.getGraphics(); } public void start() { // check values of height,width and start a thread to do the animation if (runner == null) { doFrame(); runner = new Thread(this); runner.start(); } } public void stop() { // stop the animation if (runner != null) { runner.stop(); runner = null; } } synchronized public void paint(Graphics g) { // just copy the offscreen canvas to the screen if (OSC != null) { g.drawImage(OSC,0,0,this); } } public void run() { // run the animation for (time=0;time