这是个画三角型的程序
public void drawTrisngle(float x1,float y1,float x2,float y2,Graphics g,int n){ System.out.println(n); if(n<12){ float x3=(x1+x2)/2; double y3=y2+Math.abs(x2-x1)*Math.sqrt(3)/2; g.drawLine((int)x1,(int)y1,(int)x2,(int)y2); g.drawLine((int)x1,(int)y1,(int)x3,(int)y3); g.drawLine((int)x2,(int)y2,(int)x3,(int)y3); drawTrisngle((x1+x2)/2,y1,x2,y2,g,1+n); drawTrisngle(x1,y1,(x1+x2)/2,y2,g,++n); drawTrisngle(((x1+x2)/2+x1)/2,(y1+(float)y3)/2,((x1+x2)/2+x2)/2,(y1+(float)y3)/2,g,n++); } }
注意在递归调用的时候计数器 n+1,n++,和n++是有区别的。以后要记得要统一用n+1
2.重绘
这你用了数组来保存每个五子棋的位置
public void mouseClicked(MouseEvent e){ x=e.getX(); y=e.getY(); count++; if(count%2==0) g.fillOval(x-16, y-16, 33, 33); if(count%2==1) g.drawOval(x-16, y-16, 33, 33); //设置个数组储存坐标 System.out.println(count); array[0][count]=x; array[1][count]=y; }
这几个程序代码是传递数组的值得
//用于传递数组值的方法 public int[][] getArray(){ return this.array; }0
主程序是这样接受值得
// 把棋子中的值传过来 a = DL.getArray();
在输出数组的时候记住会遇到空指针的情况,这是因为有些数组没有被赋值
搜易在输出的之后要加一句//这里的数组用的是static 初始值都为0
//若没用static 需加入if(*[]!=null)
3重绘
这里我们要继承和重写父类中的方法,才能实现重绘
public class qipanFrame extends JFrame { private static int[][] a; public static void main(String[] args) { // JFrame jf=new JFrame(); qipanFrame jf = new qipanFrame();
棋盘重绘的实现
public void paint(Graphics g) { super.paint(g); qipan(g); Aqizi(g); } private void Aqizi(Graphics g) { //这里的数组用的是static 初始值都为0 //若没用static 需加入if(*[]!=null) int t, f; for (t = 0; t < 2; t++) for (f = 0; f < 100; f++) { if (a[t][f] != 0) { if (f% 2 == 0) g.fillOval(a[0][f], a[1][f], 33, 33); if (f % 2 == 1) g.drawOval(a[0][f], a[1][f], 33, 33); } } } public void qipan(Graphics g) { for (int n = 0; n < 30; n++) { g.drawLine((n + 1) * 50, 0, (n + 1) * 50, 1600); g.drawLine(0, (n + 1) * 50, 1600, (n + 1) * 50); } }