/* * n_graph6.java * * Created on 29 Δεκέμβριος 2003, 10:55 πμ */ /** * * @author gauss * @version */ import java.awt.*; import java.applet.*; import java.awt.event.*; public class n_graph6 extends Applet implements ActionListener { Button button1 = new Button(); // Plot Button Button button2 = new Button();// Init Button Button button3 = new Button();// Plot Button2 Image buffer,buffer2; // image buffer Graphics myGC,myGC2; // graphics for the buffer int vxmin,vymin,vxmax,vymax; //screen coordinates double xmin,ymin,xmax,ymax; //imaginary coordinates double dx; //interval of x double angle; //angle double d_angle; //interval of angle double cx,cy,cr; //center and radius of circle /* initializing */ public void init(){ String str; xmin=-3;xmax=7;ymin=-1.5;ymax=1.5; vxmin=0;vxmax=500;vymin=0;vymax=150; dx=Math.PI/60; angle=0;d_angle=Math.PI/60; cx=-1.5;cy=0;cr=1; setLayout(null); //free layout setSize(600,250); //size of this applet add(button1); button1.setBackground(Color.cyan); button1.setForeground(Color.red); button1.setLabel("y=sinx"); button1.setFont(new Font("Dialog", Font.BOLD, 12)); button1.setBounds(320,5,50,20); button1.addActionListener(this); add(button2); button2.setBackground(Color.green); button2.setForeground(Color.black); button2.setLabel("Init"); button2.setFont(new Font("Dialog", Font.BOLD, 12)); button2.setBounds(390,5,50,20); button2.addActionListener(this); add(button3); button3.setBackground(Color.red); button3.setForeground(Color.cyan); button3.setLabel("y=sin2x"); button3.setFont(new Font("Dialog", Font.BOLD, 12)); button3.setBounds(450,5,50,20); button3.addActionListener(this); // create off-screen image buffer = createImage(vxmax-vxmin,vymax-vymin); buffer2 = createImage(vxmax-vxmin,vymax-vymin); // get graphics myGC = buffer.getGraphics(); myGC2 = buffer2.getGraphics(); setLayout(null); setSize(500,150); } public void paint(Graphics g){ drawGraph(); //draw graph to the buffer. g.drawImage(buffer,0,0,this); //copy buffer to the screen } public void paint2(Graphics g){ drawGraph2(); //draw graph to the buffer. g.drawImage(buffer2,0,0,this); //copy buffer to the screen } public void drawGraph() { double x1,y1,x2,y2; //axes and scale scale(myGC); //unit circle myGC.setColor(Color.black); Line(myGC,cx,ymin,cx,ymax); Circle(myGC,cx,cy,1); x1=cx;y1=cy; x2=cx+cr*Math.cos(angle);y2=cy+cr*Math.sin(angle); Line(myGC,x1,y1,x2,y2); myGC.setColor(Color.gray); Line(myGC,x2,y2,angle,Math.sin(angle)); //graph myGC.setColor(Color.blue); x2=0;y2=Math.sin(x2); for(x1=0;x1<=angle;x1=x1+dx){ y1=Math.sin(x1); Line(myGC,x1,y1,x2,y2); x2=x1;y2=y1; } } public void drawGraph2() { double x1,y1,x2,y2; //axes and scale scale(myGC2); //unit circle myGC2.setColor(Color.black); Line(myGC2,cx,ymin,cx,ymax); Circle(myGC2,cx,cy,1); x1=cx;y1=cy; x2=cx+cr*Math.cos(angle);y2=cy+cr*Math.sin(angle); Line(myGC2,x1,y1,x2,y2); myGC2.setColor(Color.gray); Line(myGC2,x2,y2,angle,Math.sin(angle)); //graph myGC2.setColor(Color.red); x2=0;y2=Math.sin(2*x2); for(x1=0;x1<=angle;x1=x1+dx){ y1=Math.sin(2*x1); Line(myGC2,x1,y1,x2,y2); x2=x1;y2=y1; } } /* Convert imaginary coordinates to screen cordinates. */ public int mapX(double x){ int sx; sx= vxmin+(int)((x-xmin)/(xmax-xmin)*(double)(vxmax-vxmin)) ; return sx; } public int mapY(double y){ int sy; sy= vymin+(int)((ymax-y)/(ymax-ymin)*(double)(vymax-vymin)); return sy; } /* Drawing Line */ public void Line(Graphics g,double x1,double y1,double x2,double y2){ g.drawLine(mapX(x1),mapY(y1),mapX(x2),mapY(y2)); } /* Drawing Circle */ public void Circle(Graphics g,double x,double y,double r){ g.drawOval(mapX(x-r),mapY(y+r),mapX(2*r)-mapX(0),mapY(-2*r)-mapY(0)); } /* axes and lattice */ public void scale(Graphics g){ double x,y; //back color g.setColor(new Color(255, 255, 192)); g.fillRect(vxmin,vymin,vxmax-vxmin,vymax-vymin); g.setColor(Color.lightGray); for (x=Math.PI/2;x<=Math.PI*2;x=x+Math.PI/2){ Line(g,x,-0.05,x,0.05); } for (y=-1;y<=1;y=y+1){ Line(g,-0.05,y,0.05,y); } // xy-axes g.setColor(Color.black); Line(g,0,ymin,0,ymax); Line(g,xmin,0,xmax,0); } public void actionPerformed(ActionEvent event) { String str; Object object = event.getSource(); Graphics g=getGraphics(); if (object == button1){ angle=0; //plotting animation while(angle<2*Math.PI){ angle=angle+d_angle; paint(g); } } if (object == button3){ angle=0; //plotting animation while(angle<2*Math.PI){ angle=angle+d_angle; paint2(g); } } if (object == button2){ angle=0; repaint(); } } }