Canvas
import java.applet.*;
import java.awt.*;
/*<applet code="CanvasDemo" width=500 height=200></applet>*/
class Canvas1 extends Canvas
{
public void paint(Graphics g)
{
g.setColor(Color.lightGray);
Dimension d=getSize();
g.drawRect(0,0,d.width-1,d.height-1);
g.drawLine(0,d.height/2,d.width,d.height/2);
g.drawLine(d.width/2,0,d.width/2,d.height-1);
g.setColor(Color.blue);
double dx=4*Math.PI/d.width;
double x=-2*Math.PI;
int h=d.height;
for(int i=0;i<d.width-1;i++)
{
int y1=(int)((h-h*Math.sin(x))/2);
int y2=(int)((h-h*Math.sin(x+dx))/2);
g.drawLine(i,y1,i+1,y2);
x+=dx;
}
}
}
public class CanvasDemo extends Applet
{
public void init()
{
Canvas1 c1=new Canvas1();
c1.setSize(401,501);
add(c1);
}
}
Comments
Post a Comment