import java.applet.Applet; import java.awt.*; import java.util.Random; public class sincos extends Applet implements Runnable { // Line Colour Color color = new Color(94,60,10); Color bgcolor = Color.red; int xpos=0, y; int oldx, oldy; int i =0; int width, height; Thread killme=null; Image osImage; Graphics osGraphics; boolean suspended = false; long seed = 8273; Random rnum = new Random(seed); public void init() { width = size().width; height = size().height; y = height/2; if(bgcolor != null) { setBackground(bgcolor); } else { setBackground(new Color(255,255,0)); } } public void paint(Graphics osg) { osg.setColor(color); if (xpos > (width-5)) { osg.drawLine(width-5,oldy, width-5, y); } else { osg.drawLine(oldx, oldy, xpos, y); } } public void start() { if(killme == null) { killme = new Thread(this); killme.start(); } } // -------------------------------------------- // // // // MATHEMATICS ROUTINES // // // // ---------------------------------------------// public void gettandata() { oldx = xpos; oldy = y; xpos = xpos+1; y =(int)((Math.sin((double)xpos/10) + height/10)*(Math.cos((double)xpos/5) + height/10)); // Casting associates right, same precedence as *. // System.out.print(""+xpos+" "); // System.out.println(""+y); } public void getrandata() { oldx = xpos; oldy = y; xpos = xpos + 1; y = (int)(rnum.nextFloat()*30 + height/2); // System.out.print(""+xpos+" "); // System.out.println(""+y); } public void run() { while(killme!=null) { try { Thread.sleep(50); } catch (InterruptedException e) {} gettandata(); // getrandata(); repaint(); } } // Close run(). // -------------------------------------------- // // // // GRAPHICS DISPLAY // // // // ---------------------------------------------// public void update(Graphics g) { if(osImage == null) osImage=createImage(size().width,size().height); osGraphics=osImage.getGraphics(); if (xpos>width) { osGraphics.copyArea(1, 0, width, height, -1, 0); osGraphics.clipRect(0, 0, width, height); // (1) } // Close x > width. paint(osGraphics); g = this.getGraphics(); if (xpos>width) { g.drawImage(osImage, 0,0,this); // (1) osGraphics.dispose(); } // Close if. else { g.drawImage(osImage, 0,0, this); osGraphics.dispose(); } // Close if/else. } // Close update(). // -------------------------------------------- // // // // MOUSE EVENTS // // // // ---------------------------------------------// public boolean handleEvent(Event evt) { if(evt.id==Event.MOUSE_DOWN) { if (suspended) { killme.resume(); } else { killme.suspend(); } suspended = !suspended; } return true; } // Close handleEvent(). public void stop() { if (killme != null) killme.stop(); killme= null; } // Close stop(). } // Close sincos.class