五子棋的FPanel类实现
import javax.swing.*;
import java.awt.*;
import java.util.*;
import java.net.*;
import java.io.*;
import java.awt.event.*;
class FPanel extends JComponent
{
protected Image offscreen; //保存当前固定的GUI图象
private Dimension offscreensize; //offscreen的大小
private Graphics offg; //offscreen的画笔
int x,y;//棋盘左上角的坐标值
int X0=1,Y0=1;//棋盘的行距及列宽
int xe=-1,ye=-1;//当前鼠标的逻辑坐标
int xc=-1, yc=-1;//当前棋子逻辑坐标
private int result=0;//下一个棋子的结果
Boolean flag1=false;//绘制棋子位置的方框
Boolean flag2=false;//绘制黑子还是白子
Boolean flag3=false;//是主动paint()还是系统paint()
public void init()
{
X0=1;
Y0=1;
xe=-1;
ye=-1;
xc=-1;
yc=-1;
flag1=false;
flag2=false;
flag3=false;
offscreen=null;
setCursor(new Cursor(Cursor.WAIT_CURSOR));
result=0;
}
public void setResult(int x)
{
if(x==1)
{
result=1;
}
else if(x==-1)
{
result=-1;
}
else {
result=0;
}
}
public void seteXY(int x,int y)//通知类现在鼠标的逻辑位置
{
xe=x;
ye=y;
}
protected void paintComponent(Graphics g)
{
setBackground(new Color(100,100,100));
if(offscreen==null)
{
drawPad();
}
if(flag3)
{
if(flag1)
{
g.drawImage(offscreen,0,0,this);
draw_x(g);
draw_k(g);
}else{
drawChess(xe,ye);
if(result!=0)
{
drawResult(offg,result);
}
g.drawImage(offscreen,0,0,this);
if(result==0)
{
draw_x(g);
}
}
}else{
g.drawImage(offscreen,0,0,this);
}
}
public void update(Graphics g)
{
paint(g);
}
public void drawPad()
{
Dimension d=getSize();
X0=d.width/15;
Y0=d.height/15;
x=X0/2;
y=Y0/2;
if ((offscreen==null)||(d.width!=offscreensize.width)||(d.height!=offscreensize.height))
{
offscreen=createImage(d.width,d.height);
offscreensize = d;
offg=offscreen.getGraphics();
offg.setFont(getFont());
}
offg.setColor(new Color(250,200,50));
offg.fillRect(0,0,d.width,d.height);
offg.setColor(Color.black);
for(int i=0;i<15;i++)
{
offg.drawLine( x + X0 * i, y , x + X0 * i , y + Y0 * 14 );
offg.drawLine( x , y + i*Y0 ,x + 14 * X0 ,y + i * Y0 );
offg.fillRect(x+7*X0-4,y+7*Y0-4,8,8);
offg.fillRect(x+3*X0-4,y+3*Y0-4,8,8);
offg.fillRect(x+11*X0-4,y+3*Y0-4,8,8);
offg.fillRect(x+3*X0-4,y+11*Y0-4,8,8);
offg.fillRect(x+11*X0-4,y+11*Y0-4,8,8);
}
}
private void drawChess(int x1,int y1)
{
Graphics2D g=(Graphics2D)offg;
if(flag2)
{
g.setColor(Color.white);
flag2=false;
}else {
g.setColor(Color.black);
flag2=true;
}
flag3=false;
flag1=true;
xc=x1;
yc=y1;
g.fillOval(x1*X0+2,y1*Y0+2,X0-4,Y0-4);
}
private void draw_k(Graphics g)
{
int xa=xe*X0+x;
int ya=ye*Y0+y;
g.setColor(new Color(255,0,0));
g.drawRect(xa-x,ya-y,X0,Y0);
}
private void draw_x(Graphics g)
{
if(xc!=-1&&yc!=-1)
{
int xa=xc*X0+x;
int ya=yc*Y0+y;
g.setColor(new Color(255,0,0));
g.fillRect(xa-5,ya-5,10,10);
}
}
public void drawResult(Graphics g,int x)
{
if(x==1)
{
g.setColor(new Color(0,255,0));
g.setFont(new Font("宋体", 30, 40));
g.drawString("五子棋,你胜了!",160,180);
}else if(x==-1)
{
g.setColor(new Color(255,0,0));
g.setFont(new Font("宋体", 30, 40));
g.drawString("五子棋,你输了!",160,180);
}
}
}
做一个错误声明:
FPanle中有很多变量不是私有的,破坏了类的封装特性;当时由于没有注意面向对象的要求,在FPanel中
使用了一些共享的控制变量,在ChessFrame类中实现控制;
另外,对外仅实现了repaint(),paint()等接口,在ChessFrame仅用了repaint(),就完成了,绘制功能;其他的自定义方法都是私有的,这在工程上是彻底失败的!
下一个改进版本,一定改过来!