Java 图形程序设计

变形心形曲线 bxxxqx.java

程序设计:谢山水 2003.11.11.

// 变形心形曲线 bxxxqx.java 程序设计: 谢山水 2003.11.11.
// 正弦波,多叶曲线:线宽、颜色、分段颜色、背景色、花叶数、显示速度
// 画图方向:向左画、向右画、向上画、向下画
// 位置:水平、垂直多条 java 多叶曲线

import java.awt.*;

// 1.图形程序设计 bxxxqx 类继承框架 Frame
public class bxxxqx extends Frame {
public bxxxqx() {
setLayout(new BorderLayout()); // 设置边界布局
DrawPanel display = new DrawPanel();// 创建画图面板实例 display
add("Center", display); // 对中加入画图面板 display
}

// 2.关闭窗口,终止程序
public boolean handleEvent(Event e) {
switch (e.id) {
case Event.WINDOW_DESTROY: // 窗口关闭
System.exit(0); // 系统退出,终止程序
return true;
default:
return false;
}
}
// 3.程序入口 main
public static void main(String args[]) {
bxxxqx iavabxxxqx = new bxxxqx();
iavabxxxqx.setTitle("Java 语言图形程序设计 变形心形曲线 谢山水 2003.11.11.");
iavabxxxqx.setSize(800,600);
iavabxxxqx.show();
}
}

// 4.定义画板类,求x,y轴投影的方法,定义画图方法
class DrawPanel extends Panel {
int x0=400,x1=x0-170,x2=x0+170,y0=270,y1=y0-105,y2=y0+105;
int n,y,r;
// 花叶,x,y原点,半径
double x,PI=3.141596,i,i1=PI/1111;

// 定义心形曲线 fx,fy
double fx(double i){ //定义求x轴投影和x轴坐标的方法
return x+(Math.cos(n*i)*Math.cos(i))*r;
// 调整x轴坐标原点,花叶数,x轴投影,花叶半径
}
double fy(double i){ //定义求y轴投影和y轴坐标的方法
return y+(Math.cos(n*i)*Math.sin(i))*r;
// 调整y轴坐标原点,花叶数,y轴投影,花叶半径
}

// 定义变形曲线1 fx1,fy1
double fx1(double i){ //定义求x轴投影和x轴坐标的方法
return x+(Math.sin(n*i)*Math.cos(i))*r;
// 调整x轴坐标原点,花叶数,x轴投影,花叶半径
}
double fy1(double i){ //定义求y轴投影和y轴坐标的方法
return y+(Math.sin(i)*Math.sin(i))*r;
// 调整y轴坐标原点,花叶数,y轴投影,花叶半径
}

// 定义变形曲线2 fx2,fy2
double fx2(double i){ //定义求x轴投影和x轴坐标的方法
return x+(Math.sin(i)*Math.cos(i))*r;
// 调整x轴坐标原点,花叶数,x轴投影,花叶半径
}
double fy2(double i){ //定义求y轴投影和y轴坐标的方法
return y+(Math.sin(n*i)*Math.sin(i))*r;
// 调整y轴坐标原点,花叶数,y轴投影,花叶半径
}

public void paint(Graphics g) { // 定义画图方法

// 5.画图纸边框
g.setColor(new Color(255,0,255)); // 设置画笔为紫色
g.drawRect(5,5,780,540); // 画图纸外边框

g.setColor(new Color(0,133,255)); // 设置画笔为青色
g.drawRect(15,15,760,520); // 画图纸内边框
g.drawRect(16,16,758,518);
g.drawRect(17,17,756,516);
g.drawRect(18,18,754,514);

// 6.绘制上水平3叶紫色花边
g.setColor(new Color(255,0,255)); //设置画笔为紫色
n=3;y=40;r=10; // 花叶数,半径,y原点,
for (x=50;x<755;x+=23){
for (i=0;i<=2*PI;i+=i1){
g.drawLine((int)fx(i),(int)fy(i),(int)fx(i+i1),(int)fy(i+i1));
}
}

// 7.绘制右垂直5叶绿色花边
g.setColor(new Color(0,155,0)); // 设置画笔为绿色
n=5;x=750; // 花叶数,半径,x原点,
for (y=65;y<495;y+=23){
for (i=0;i<=2*PI;i+=i1){
g.drawLine((int)fx(i),(int)fy(i),(int)fx(i+i1),(int)fy(i+i1));
}
}

// 8.绘制下水平7叶青色花边
g.setColor(new Color(0,133,255)); // 设置画笔为青色
n=7;y=510; // 花叶数,y原点,
for (x=740;x>30;x-=23){
for (i=0;i<=2*PI;i+=i1){
g.drawLine((int)fx(i),(int)fy(i),(int)fx(i+i1),(int)fy(i+i1));
}
}

// 9.绘制左垂直9叶橙色花边
g.setColor(new Color(255,200,0)); // 设置画笔为橙色
n=9;x=45; // 花叶数,x原点
for (y=480;y>50;y-=23){
for (i=0;i<=2*PI;i+=i1){
g.drawLine((int)fx(i),(int)fy(i),(int)fx(i+i1),(int)fy(i+i1));
}
}

// 10.画左窗口和输出文字
g.setColor(new Color(0,255,0));
g.drawRect(65,60,330,422); // 画左窗口绿色边框
g.setColor(new Color(255,255,0));
g.fillRect(68,63,324,416); // 画左黄色填充窗口

Font fnt14 = new Font("TimesRoman",Font.PLAIN,14);
Font fnt22 = new Font("宋体",Font.BOLD,22); // 创建字体对象

g.setFont(fnt14); // 设置当前字体
g.setColor(new Color(255,0,255));
g.drawString("Java 语言图形程序设计",150,100);// 紫字

g.setFont(fnt22); // 设置当前字体
g.setColor(new Color(0,133,0));
g.drawString("变形心形曲线",160,130); // 绿字

g.setFont(fnt14); // 设置当前字体
g.setColor(new Color(0,0,255));
g.drawString("程序设计:谢山水 2003.11.11.",130,160);// 蓝字

g.setColor(new Color(0,0,255));
g.drawString("定义心形曲线投影方法",100,200);
g.drawString("double fx(double i){",100,215);
g.drawString("return x+(Math.cos(n*i)*Math.cos(i))*r;",100,230);
g.drawString("double fy(double i){",100,245);
g.drawString("return y+(Math.cos(n*i)*Math.sin(i))*r;",100,260);

g.drawString("定义变形曲线1投影方法",100,290);
g.drawString("double fx1(double i){",100,305);
g.drawString("return x+(Math.sin(n*i)*Math.cos(i))*r;",100,320);
g.drawString("double fy1(double i){",100,335);
g.drawString("return y+(Math.sin(i)*Math.sin(i))*r;",100,350);

g.drawString("定义变形曲线2投影方法",100,380);
g.drawString("double fx2(double i){",100,395);
g.drawString("return x+(Math.sin(i)*Math.cos(i))*r;",100,410);
g.drawString("double fy2(double i){",100,425);
g.drawString("return y+(Math.sin(n*i)*Math.sin(i))*r;",100,440);


// 11.画右窗口绘制变形曲线1
g.setColor(new Color(255,0,0));
g.drawRect(402,60,330,422); // 画右窗口红色边框
g.setColor(new Color(0,255,200));
g.setColor(new Color(0,255,200));
g.fillRect(405,63,324,416); // 画右青色填充窗口 g.setColor(new Color(255,0,0));
g.drawString("变形曲线1",540,100); // 紫字

// 12.绘制上变形曲线1
g.setColor(new Color(255,0,0)); 
n=2;x=x2-100;y=y1;r=35; // x,y原点
for (i=0;i<=2*PI;i+=i1/55){
g.drawLine((int)fx1(i),(int)fy1(i),(int)fx1(i+i1),(int)fy1(i+i1));
}

g.setColor(new Color(0,133,0)); 
n=3;x=x2; // x,y原点
for (i=0;i<=2*PI;i+=i1/55){
g.drawLine((int)fx1(i),(int)fy1(i),(int)fx1(i+i1),(int)fy1(i+i1));
}

g.setColor(new Color(0,0,255)); 
n=4;x=x2+100; // x,y原点
for (i=0;i<=2*PI;i+=i1/55){
g.drawLine((int)fx1(i),(int)fy1(i),(int)fx1(i+i1),(int)fy1(i+i1));
}


// 13.绘制中变形曲线1
g.setColor(new Color(255,0,0)); 
n=5;x=x2-100;y=y0;r=40; // x,y原点
for (i=0;i<=2*PI;i+=i1/55){
g.drawLine((int)fx1(i),(int)fy1(i),(int)fx1(i+i1),(int)fy1(i+i1));
}

g.setColor(new Color(0,133,0)); 
n=6;x=x2; // x,y原点
for (i=0;i<=2*PI;i+=i1/55){
g.drawLine((int)fx1(i),(int)fy1(i),(int)fx1(i+i1),(int)fy1(i+i1));
}

g.setColor(new Color(0,0,255)); 
n=7;x=x2+100; // x,y原点
for (i=0;i<=2*PI;i+=i1/55){
g.drawLine((int)fx1(i),(int)fy1(i),(int)fx1(i+i1),(int)fy1(i+i1));
}


// 14.绘制下变形曲线1
g.setColor(new Color(255,0,0)); 
n=8;x=x2-100;y=y2+20;r=45; // x,y原点
for (i=0;i<=2*PI;i+=i1/55){
g.drawLine((int)fx1(i),(int)fy1(i),(int)fx1(i+i1),(int)fy1(i+i1));
}

g.setColor(new Color(0,133,0)); 
n=9;x=x2; // x,y原点
for (i=0;i<=2*PI;i+=i1/55){
g.drawLine((int)fx1(i),(int)fy1(i),(int)fx1(i+i1),(int)fy1(i+i1));
}

g.setColor(new Color(0,0,255)); 
n=10;x=x2+100; // x,y原点
for (i=0;i<=2*PI;i+=i1/55){
g.drawLine((int)fx1(i),(int)fy1(i),(int)fx1(i+i1),(int)fy1(i+i1));


// 15.刷新右窗口绘制变形曲线2
g.setColor(new Color(0,255,200));
g.fillRect(405,63,324,416); // 画右青色填充窗口 g.setColor(new Color(255,0,0));
g.drawString("变形曲线2",540,100); // 紫字

// 16.绘制上变形曲线2
g.setColor(new Color(255,0,0)); 
n=2;x=x2-100;y=y1;r=35; // x,y原点
for (i=0;i<=2*PI;i+=i1/55){
g.drawLine((int)fx2(i),(int)fy2(i),(int)fx2(i+i1),(int)fy2(i+i1));
}

g.setColor(new Color(0,133,0)); 
n=3;x=x2; // x,y原点
for (i=0;i<=2*PI;i+=i1/55){
g.drawLine((int)fx2(i),(int)fy2(i),(int)fx2(i+i1),(int)fy2(i+i1));
}

g.setColor(new Color(0,0,255)); 
n=4;x=x2+100; // x,y原点
for (i=0;i<=2*PI;i+=i1/55){
g.drawLine((int)fx2(i),(int)fy2(i),(int)fx2(i+i1),(int)fy2(i+i1));
}


// 17.绘制中变形曲线2
g.setColor(new Color(255,0,0)); 
n=5;x=x2-100;y=y0;r=40; // x,y原点
for (i=0;i<=2*PI;i+=i1/55){
g.drawLine((int)fx2(i),(int)fy2(i),(int)fx2(i+i1),(int)fy2(i+i1));
}

g.setColor(new Color(0,133,0)); 
n=6;x=x2; // x,y原点
for (i=0;i<=2*PI;i+=i1/55){
g.drawLine((int)fx2(i),(int)fy2(i),(int)fx2(i+i1),(int)fy2(i+i1));
}

g.setColor(new Color(0,0,255)); 
n=7;x=x2+100; // x,y原点
for (i=0;i<=2*PI;i+=i1/55){
g.drawLine((int)fx2(i),(int)fy2(i),(int)fx2(i+i1),(int)fy2(i+i1));
}


// 18.绘制下变形曲线2
g.setColor(new Color(255,0,0)); 
n=8;x=x2-100;y=y2+20;r=45; // x,y原点
for (i=0;i<=2*PI;i+=i1/55){
g.drawLine((int)fx2(i),(int)fy2(i),(int)fx2(i+i1),(int)fy2(i+i1));
}

g.setColor(new Color(0,133,0)); 
n=9;x=x2; // x,y原点
for (i=0;i<=2*PI;i+=i1/55){
g.drawLine((int)fx2(i),(int)fy2(i),(int)fx2(i+i1),(int)fy2(i+i1));
}

g.setColor(new Color(0,0,255)); 
n=10;x=x2+100; // x,y原点
for (i=0;i<=2*PI;i+=i1/55){
g.drawLine((int)fx2(i),(int)fy2(i),(int)fx2(i+i1),(int)fy2(i+i1));
}

// 19.刷新右窗口绘制心形曲线
g.setColor(new Color(0,255,200));
g.fillRect(405,63,324,416); // 画右青色填充窗口 g.setColor(new Color(255,0,0));
g.drawString("心形曲线",540,100); // 紫字

// 20.上3,4,5叶红绿蓝花
g.setColor(new Color(255,0,0)); 
n=3;x=x2-100;y=y1;r=35; // 3叶红花,x,y原点
for (i=0;i<=2*PI;i+=i1/55){
g.drawLine((int)fx(i),(int)fy(i),(int)fx(i+i1),(int)fy(i+i1));
}

g.setColor(new Color(0,133,0)); 
n=2;x=x2; // 4叶绿花,x,y原点
for (i=0;i<=2*PI;i+=i1/55){
g.drawLine((int)fx(i),(int)fy(i),(int)fx(i+i1),(int)fy(i+i1));
}

g.setColor(new Color(0,0,255)); 
n=5;x=x2+100; // 5叶蓝花,x,y原点
for (i=0;i<=2*PI;i+=i1/55){
g.drawLine((int)fx(i),(int)fy(i),(int)fx(i+i1),(int)fy(i+i1));
}


// 21.中7,8,9叶红绿蓝花
g.setColor(new Color(255,0,0)); 
n=7;x=x2-100;y=y0;r=40; // 7叶红花,x,y原点
for (i=0;i<=2*PI;i+=i1/55){
g.drawLine((int)fx(i),(int)fy(i),(int)fx(i+i1),(int)fy(i+i1));
}

g.setColor(new Color(0,133,0)); 
n=4;x=x2; // 8叶绿花,x,y原点
for (i=0;i<=2*PI;i+=i1/55){
g.drawLine((int)fx(i),(int)fy(i),(int)fx(i+i1),(int)fy(i+i1));
}

g.setColor(new Color(0,0,255)); 
n=9;x=x2+100; // 9叶蓝花,x,y原点
for (i=0;i<=2*PI;i+=i1/55){
g.drawLine((int)fx(i),(int)fy(i),(int)fx(i+i1),(int)fy(i+i1));
}


// 22.下11,12,13叶红绿蓝花
g.setColor(new Color(255,0,0)); 
n=11;x=x2-100;y=y2+20;r=45; // 11叶红花,x,y原点
for (i=0;i<=2*PI;i+=i1/55){
g.drawLine((int)fx(i),(int)fy(i),(int)fx(i+i1),(int)fy(i+i1));
}

g.setColor(new Color(0,133,0)); 
n=6;x=x2; // 12叶绿花,x,y原点
for (i=0;i<=2*PI;i+=i1/55){
g.drawLine((int)fx(i),(int)fy(i),(int)fx(i+i1),(int)fy(i+i1));
}

g.setColor(new Color(0,0,255)); 
n=13;x=x2+100; // 13叶蓝花,x,y原点
for (i=0;i<=2*PI;i+=i1/55){
g.drawLine((int)fx(i),(int)fy(i),(int)fx(i+i1),(int)fy(i+i1));
}

// 23.设置背景两次(黄色,橙色),可以使用画面不断刷新重画.
setBackground(new Color(255,255,0)); 
setBackground(new Color(0,199,0));

}
}