1.松树:
import java.awt.*; import java.applet.*; public class Tree extends Applet { public void paint(Graphics g) { g.drawString("静听松声寒!!", 35, 50 ); tree(200.0,400.0,200.0,0.0,7,g); this.setSize(500,500); } public void tree(double x1,double y1,double x2,double y2,int n,Graphics g) { if(n>=1) { double x3,x4,y3,y4; g.drawLine((int)(x1),(int)(y1),(int)(x2),(int)(y2)); x3=(x1+x2)/2; y3=(y1+y2)/2; tree(x1,y1,x3,y3,n-1,g); tree(x3,y3,x2,y2,n-1,g); x4=(x2+y2-y3-x3)*0.7071+x3; y4=(y2-x2+x3-y3)*0.7071+y3; tree(x3,y3,x4,y4,n-1,g); x3=(x1*3+x2)/4; y3=(y1*3+y2)/4; x2=(x2*3+x1)/4; y2=(y2*3+y1)/4; x4=(x2*1.732-y2+2*x3-x3*1.732+y3)/2; y4=(x2+y2*1.732-x3+2*y3-1.732*y3)/2; tree(x3,y3,x4,y4,n-1,g); } } }
2.四叶玫瑰线
import java.awt.*; import java.applet.*; public class E extends Applet { public void paint(Graphics g) { g.setColor(Color.green); g.setFont(new Font("Serif", Font.PLAIN, 50)); g.drawString("四叶玫瑰线", 300, 500); this.setSize(800, 600); for (int i = 5; i < 200; i += 10) { for (int j = 0; j < 800; j++) { double r; int x, y; r = i * Math.sin(j * Math.PI / 200); x = 150 + (int) (r * Math.cos(j * Math.PI / 400)); y = 200 + (int) (r * Math.sin(j * Math.PI / 400)); g.drawLine(x, y, x, y); r = i * Math.cos(j * Math.PI / 200); x = 500 + (int) (r * Math.cos(j * Math.PI / 400)); y = 200 + (int) (r * Math.sin(j * Math.PI / 400)); g.drawLine(x, y, x, y); } } } }
3.方
import java.awt.*; public class D extends java.applet.Applet { public void paint(Graphics g) { recursquare(300,300,150,0,7,g); this.setSize(650,650); } public void recursquare(int x,int y,int r,int dir,int n,Graphics g) { square(x,y,r,g); if(n>0) { if(dir!=1) recursquare(x,y-r,r/2,3,n-1,g); if(dir!=2) recursquare(x+r,y,r/2,4,n-1,g); if(dir!=3) recursquare(x,y+r,r/2,1,n-1,g); if(dir!=4) recursquare(x-r,y,r/2,2,n-1,g); } } public void square(int x,int y,int r,Graphics g) { g.drawLine(x-r/2,y-r/2,x+r/2,y-r/2); g.drawLine(x-r/2,y-r/2,x-r/2,y+r/2); g.drawLine(x-r/2,y+r/2,x+r/2,y+r/2); g.drawLine(x+r/2,y-r/2,x+r/2,y+r/2); } }
4.旋转图
import java.awt.*; import java.applet.*; public class A extends Applet { public void paint(Graphics g) { p(0, 0, 200, g, 14); this.setSize(500, 500); g.setColor(Color.green); g.setFont(new Font("Serif", Font.PLAIN, 50)); g.drawString("旋转图", 300, 490); } public void p(int x0, int y0, int r, Graphics g, int n) { int x1, y1, x2, y2, x3, y3, x4, y4, x5, y5, x6, y6, x7, y7, x8, y8; x1 = x0 - r; y1 = y0 + r; x5 = x0; y5 = y0 + r; x2 = x0 + r; y2 = y0 + r; x6 = x0 + r; y6 = y0; x3 = x0 + r; y3 = y0 - r; x7 = x0; y7 = y0 - r; x4 = x0 - r; y4 = y0 - r; x8 = x0 - r; y8 = y0; a(x1, y1, x5, y5, x0, y0, x8, y8, n, g); a(x5, y5, x2, y2, x6, y6, x0, y0, n, g); a(x8, y8, x0, y0, x7, y7, x4, y4, n, g); a(x0, y0, x6, y6, x3, y3, x7, y7, n, g); } public void a(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4, int n, Graphics g) { g.drawLine(200 + x1, 200 - y1, 200 + x2, 200 - y2); g.drawLine(200 + x2, 200 - y2, 200 + x3, 200 - y3); g.drawLine(200 + x3, 200 - y3, 200 + x4, 200 - y4); g.drawLine(200 + x4, 200 - y4, 200 + x1, 200 - y1); if (n <= 1) return; int xx1, yy1, xx2, yy2, xx3, yy3, xx4, yy4; xx1 = (int) ((5 * x1 + x2) / 6); yy1 = (int) ((5 * y1 + y2) / 6); xx2 = (int) ((5 * x2 + x3) / 6); yy2 = (int) ((5 * y2 + y3) / 6); xx3 = (int) ((5 * x3 + x4) / 6); yy3 = (int) ((5 * y3 + y4) / 6); xx4 = (int) ((5 * x4 + x1) / 6); yy4 = (int) ((5 * y4 + y1) / 6); a(xx1, yy1, xx2, yy2, xx3, yy3, xx4, yy4, n - 1, g); } }
5.视觉冲击
import java.awt.*; import java.applet.Applet; public class C extends Applet { public void paint(Graphics g) { carpet(300, 300, 600, 0, g); this.setSize(600, 600); } public void carpet(int x0, int y0, int r, int n, Graphics g) { int x1, x2, y1, y2; x1 = x0 + (int) (1 * r / 3); x2 = x0 - (int) (1 * r / 3); y1 = y0 + (int) (1 * r / 3); y2 = y0 - (int) (1 * r / 3); g.fillRect(x0 - (int) (r / 6), y0 - (int) (r / 6), (int) (r / 3), (int) (r / 3)); if (n < 6) { carpet(x2, y2, (int) (r / 3), n + 1, g); carpet(x0, y2, (int) (r / 3), n + 1, g); carpet(x1, y2, (int) (r / 3), n + 1, g); carpet(x2, y0, (int) (r / 3), n + 1, g); carpet(x1, y0, (int) (r / 3), n + 1, g); carpet(x2, y1, (int) (r / 3), n + 1, g); carpet(x0, y1, (int) (r / 3), n + 1, g); carpet(x1, y1, (int) (r / 3), n + 1, g); } } }