资源简介
棋盘覆盖---JAVA版,带有图形化界面的,挺经典的哦.
代码片段和文件信息
package 棋盘;
import java.applet.applet;
import java.awt.event.*;
import java.awt.*;
public class ChessBoard extends applet implements ActionListener{
/**
* @param args
*/
private int tr = 100 tc = 100 dr = 0 dc = 1
dx = 40 size=4 s = 0
t1 = 0 t2 = 0;
private Button button1;
Label label1 label2 label3;
TextField text1 text2 text3;
public void init()
{
label1 = new Label(“SIZE“);
add(label1);
text1 = new TextField(“4“4);
text1.addActionListener(this);
add(text1);
label2 = new Label(“dr:“);
add(label2);
text2 = new TextField(“0“4);
text2.addActionListener(this);
add(text2);
label3 = new Label(“dc:“);
add(label3);
text3 = new TextField(“1“4);
text3.addActionListener(this);
add(text3);
button1 = new Button(“执行“);
button1.addActionListener(this);
add(button1);
}
public void paint(Graphics g)
{
int x1x2y1y2;
x1 = tr;
x2 = tr + size*dx;
g.setColor(Color.BLUE);
for(int i = 0;i <= size;i++)
{
y1 = tr + i*dx;
g.drawLine(x1y1x2y1);
}
y1 = tc;
y2 = tc + size*dx;
for(int j = 0;j <= size;j++)
{
x1 = tc + j*dx;
g.drawLine(x1y1x1y2);
}
g.fillRect(tr+ dc*dxtc + dr*dxdxdx);
ChessBoard(00drdcsizeg);
}
void ChessBoard(int t1 int t2 int drint dc int size Graphics g)
{
if(size == 1) return;
int s = size/2;
Color color = new Color((int)(Math.random()*256)
(int)(Math.random()*256)(int)(Math.random()*256));
if(dr ChessBoard(t1t2drdcsg);
else
{
g.setColor(color);
g.fillRect(tr+(t2+s-1)*dxtc+(t1+s-1)*dxdxdx);
ChessBoard(t1t2t1+s-1t2+s-1sg);
}
if(dr=t2+s)
ChessBoard(t1t2+sdrdcsg);
else
{
g.setColor(color);
g.fillRect(tr+(t2+s)*dxtc+(t1+s-1)*dxdxdx);
ChessBoard(t1t2+st1+s-1t2+ssg);
}
if(dr>=t1+s && dc ChessBoard(t1+st2drdcsg);
else
{
g.setColor(color);
g.fillRect(tr+(t2+s-1)*dxtc+(t1+s)*dxdxdx);
ChessBoard(t1+st2t1+st2+s-1sg);
}
if(dr>=t1+s && dc>=t2+s)
ChessBoard(t1+st2+sdrdcsg);
else
{
g.setColor(color);
g.fillRect(tr+(t2+s)*dxtc+(t1+s)*dxdxdx);
ChessBoard(t1+st2+st1+st2+ssg);
}
}
public void actionPerformed(ActionEvent e)
{
size = Integer.parseInt(text1.getText());
dr = Integer.parseInt(text2.getText());
dc = Integer.parseInt(text3.getText());
repaint();
}
}
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 2558 2008-10-12 23:47 ChessBoard.java
----------- --------- ---------- ----- ----
2558 1
评论
共有 条评论