JAVA笛卡尔曲线

时间:2022-09-07 17:15:37

效果图如下:

JAVA笛卡尔曲线

 import java.applet.*;
import java.awt.*; public class Descartes extends Applet
{
int AppletWidth,AppletHeight;
Image OffScreen;
Graphics drawOffScreen;
public void init()
{
setBackground(Color.black); //设置背景为黑色
AppletWidth = getSize().width * 2; //获取applet的宽度
AppletHeight = getSize().height * 2; //获取appplet的高度
OffScreen = createImage(AppletWidth,AppletHeight); //创建一副使用了双缓冲技术的不可见的画布
drawOffScreen = OffScreen.getGraphics();
}
public void paint(Graphics g)
{
//设置前景色为白色
drawOffScreen.clearRect(0,0,AppletWidth,AppletHeight);
drawOffScreen.setColor(Color.white);
double x,y,r;
for(int i=0; i<=90 ; i++)
{
for(int j=0; j<=90; j++)
{
r=Math.PI / 45 * i * ( 1 - Math.sin(Math.PI / 45 * j)) * 18;
x = r * Math.cos(Math.PI / 45 * j) * Math.sin(Math.PI / 45 * i)+AppletWidth / 2;
y = -r*Math.sin(Math.PI / 45 * j) + AppletHeight / 4;
drawOffScreen.fillOval((int)x,(int)y,2,2); //绘制椭圆形
}
}
g.drawImage(OffScreen,0,0,this); //生成图片
}
}