import java.applet.*; import java.awt.*; //copyright 2000 M Paez, U Antioquia, R landau, OSU//Author: Carlos E. Yaguna //Departamento de Fisica //Universidad de Antioquia //Medellin COLOMBIA //yaguna@pegasus.udea.edu.co public class Fig2 extends Applet { private Fig2Canv theCanvas; private Panel thePanel,p1,p2,p3; private Button b1,b2,b3; private Choice lista; private TextField t1; public double PI=3.141592; public void init() { int max; max=(int)360; t1=new TextField(3); lista=new Choice(); lista.addItem("1-1"); lista.addItem("1-2"); lista.addItem("1-3"); lista.addItem("1-4"); lista.addItem("2-3"); lista.addItem("3-4"); lista.addItem("3-5"); lista.addItem("4-5"); lista.addItem("5-6"); b1=new Button("Start"); b2=new Button("Pause"); b3=new Button("Stop"); p1=new Panel(); p2=new Panel(); p3=new Panel(); p1.setLayout(new GridLayout(1,4)); p2.setLayout(new BorderLayout()); theCanvas=new Fig2Canv(); theCanvas.resize(400,400); theCanvas.setBackground(Color.yellow); p1.add(b1); p1.add(b2); p1.add(lista); p1.add(t1); //p1.add(b3); p2.add("North",p1); thePanel=new Panel(); setLayout(new BorderLayout()); thePanel.setLayout(new BorderLayout()); thePanel.add("Center",theCanvas); thePanel.add("North",p2); add("Center",thePanel); theCanvas.part=120; theCanvas.max=10; theCanvas.ii=0; theCanvas.lis0(); theCanvas.init(); theCanvas.start(); } public boolean action(Event e,Object o) { if(e.target instanceof TextField){ if(e.target==t1){ theCanvas.desf2=Double.valueOf(t1.getText()).doubleValue(); theCanvas.repaint(); } return true; } if(e.target==lista){ if(lista.getSelectedIndex()==0)theCanvas.lis0(); if(lista.getSelectedIndex()==1)theCanvas.lis1(); if(lista.getSelectedIndex()==2)theCanvas.lis2(); if(lista.getSelectedIndex()==3)theCanvas.lis3(); if(lista.getSelectedIndex()==4)theCanvas.lis4(); if(lista.getSelectedIndex()==5)theCanvas.lis5(); if(lista.getSelectedIndex()==6)theCanvas.lis6(); if(lista.getSelectedIndex()==7)theCanvas.lis7(); if(lista.getSelectedIndex()==8)theCanvas.lis8(); theCanvas.desf2=0.0; } if(e.target instanceof Button){ if(e.target==b1){ theCanvas.ii=1; theCanvas.repaint(); } if(e.target==b2){ if(theCanvas.ii==1){ theCanvas.ii=0; theCanvas.repaint(); } } return true; } return true; } } class Fig2Canv extends Canvas implements Runnable{ double ome1,ome2,desf,desf2,part,max; double PI=3.141592,tt; int ii,xp,yp; Thread runstring; private Image offScreenImage; public void init() { tt=0.0; //max=10; //part=120; //ii=1; offScreenImage=createImage(size().width,size().height); } public void setOme1(int w) { ome1=(w*PI/180.0);} public void setOme2(int h) { ome2=(h*PI/180.0);} public void setDesf(double j) {desf=(j*PI/180.0);} public void start() { if(runstring==null); { runstring=new Thread(this); runstring.start(); } } public void stop() { if(runstring != null){ runstring.suspend(); runstring=null; } } public void run() { while(true){ if(ii==1) repaint(); try{ Thread.sleep(300); } catch(InterruptedException e){ } } } public void update(Graphics g) { //To avoid flicker Graphics offScreenGraphics=offScreenImage.getGraphics(); offScreenGraphics.setColor(getBackground()); offScreenGraphics.fillRect(0,0,size().width,size().height); offScreenGraphics.setColor(g.getColor()); paint(offScreenGraphics); g.drawImage(offScreenImage,0,0,this); } void coordenadas(double x,double y) { int width,height,xpcero,ypcero; double ymmax,xmmax; width=200;//ancho del Canvas height=200;//altura xmmax=1;//coordenada de mundo maxima para x xpcero=100;//coordenada de pantalla para el punto medio ymmax=1; ypcero=100; xp=(int)(xpcero+(width-xpcero)/xmmax*x); yp=(int)(ypcero+((-height+ypcero)/ymmax*y)); } public void lis0() { setOme1(120); setOme2(120); max=3; part=120; repaint(); } public void lis1() { setOme1(240); setOme2(120); max=10; part=120; repaint(); } public void lis2() { setOme1(120); setOme2(40); max=10; part=120; repaint(); } public void lis3() { setOme1(120); setOme2(30); max=20; part=120; repaint(); } public void lis4() { setOme1(120); setOme2(80); max=10; part=120; repaint(); } public void lis5() { setOme1(120); setOme2(160); max=10; part=120; repaint(); } public void lis6() { setOme1(120); setOme2(200); max=9; part=200; repaint(); } public void lis7() { setOme1(120); setOme2(150); max=20; part=200; repaint(); } public void lis8() { setOme1(180); setOme2(150); max=20; part=200; repaint(); } public void plot (Graphics g) { double i; double xm,ym,x1,y1; //max=10; //part=120; x1=50; y1=50; setDesf(desf2); g.setColor(Color.black); tt=0.0; for(i=0;i0)g.drawLine((int)x1,(int)y1,(int)xp,(int)yp); x1=xp; y1=yp; tt=tt+max/part; } } public void grafica(Graphics g) { int xi,yi,xc,yc,r; xc=30; yc=30; r=10; g.setColor(Color.red); g.drawOval(xc-r,yc-r,2*r,2*r); xi=(int)(xc+r*Math.cos(desf)); yi=(int)(yc-r*Math.sin(desf)); g.setColor(Color.blue); g.drawLine(xc,yc,xi,yi); } public void paint(Graphics g) { plot(g); grafica(g); if(ii==1)desf2+=5; if(desf2%360==0)desf2=0; } }