import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class Qipan extends Panel{
public static final int _rectlen = 22,_rectnum = 19;
//默认的方格长度及数目
protected Vector qizis;
//保存所有已下的棋子,包括棋盘上的所有棋子和被提掉的
//若某一次落子没有造成提子,则qizis保存的就是这个棋子;
//若某一次落子造成了提子,则qizis中保存的就是一个Vector
//这个Vector中包括所有被这个棋子提掉的棋子以及棋子本身
//这个棋子本身位于这个Vector的最后
protected int num;//已下数目
protected int currentTurn;//轮到谁下
protected int rectnum,rectlen;//方格的数目及长度
protected int qizilen;//棋子的直径
protected Qizi[][] map;//在棋盘上的所有棋子;
protected Image offscreen;
protected Graphics offg;
protected int size;//棋盘的宽度及高度
protected int top,left;//棋盘左边及上边的边距;
protected Point mouseLoc;//鼠标的位置,即map数组的下标
protected ControlPanel controlPanel;//控制面板
public int getWidth() {
return size+controlPanel.getWidth()+35;
}
public int getHeight() {
return size;
}
public Qipan() {
//棋盘外观
top = left = 20;
rectnum = _rectnum;
rectlen = _rectlen;
qizilen = rectlen*9/10;
size = 2*left+rectnum*rectlen;
//this.addMouseListener(new ML());
//this.addMouseMotionListener(new MML());
this.setLayout(new BorderLayout());
controlPanel = new ControlPanel();
this.setSize(getWidth(),size);
add(controlPanel,BorderLayout.EAST);
start();
}
public void addNotify() {
System.out.println("执行addNotify");
super.addNotify();//一定得有
offscreen = createImage(size,size);
offg = offscreen.getGraphics();
}
public void paint(Graphics g) {
System.out.println("执行paint");
offg.setColor(new Color(180,150,150));
offg.fillRect(0, 0, size, size);
offg.setColor(Color.black);
for(int i=0;i<rectnum+1;i++) {
int x1 = left+i*rectlen;
int x2 = x1;
int y1 = top;
int y2 = top+rectnum*rectlen;
offg.drawLine(x1, y1, x2, y2);
x1 = left;
x2 = left+rectnum*rectlen;
y1 = top+i*rectlen;
y2 = y1;
offg.drawLine(x1, y1, x2, y2);
}
g.drawImage(offscreen,0,0,this);
}
public void update(Graphics g) {
System.out.println("执行update");
paint(g);
}
public void start() {
System.out.println("执行start");
qizis = new Vector();
num = 0;
map = new Qizi[rectnum+1][rectnum+1];
currentTurn = Qizi._back;
controlPanel.setLabel();
repaint();
}
class ControlPanel extends Panel {
protected Label lblTurn = new Label("",Label.CENTER);
protected Label lblNum = new Label("",Label.CENTER);
protected Label lblMsg = new Label("",Label.CENTER);
protected Choice choice = new Choice();
protected Button back = new Button("<<悔棋");
protected Button forward = new Button("前进>>");
protected Button start = new Button("重新开始");
public int getWidth() {
return 45;
}
public int getHeight() {
return size;
}
public ControlPanel() {
this.setSize(this.getWidth(), this.getHeight());
this.setLayout(new GridLayout(12,1,0,10));
setLabel();
choice.add("19 X 19");
choice.add("15 X 15");
choice.add("13 X 13");
choice.add("11 X 11");
choice.add("7 X 7");
//choice.addItemListener(new IL());
add(lblTurn);
add(lblNum);
add(start);
add(choice);
add(lblMsg);
add(back);
add(forward);
//back.addActionListener(new AL());
//forward.addActionListener(new AL());
//start.addActionListener(new AL());
this.setBackground(new Color(120,120,200));
//this.setVisible(true)
}
public Insets getInsets() {
return new Insets(5,5,5,5);
}
public void setLabel() {
lblTurn.setText(Qipan.this.currentTurn==Qizi._back?"黑方":"白方");
lblTurn.setForeground(Qipan.this.currentTurn==Qizi._back?Color.black:Color.white);
lblNum.setText("第 "+(Qipan.this.num+1)+" 手");
lblNum.setForeground(Qipan.this.currentTurn==Qizi._back?Color.black:Color.white);
}
public void setMsg(String msg) {
lblMsg.setText(msg);
}
}
}
评论1