package cup.edu.lxq.game;
import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Composite;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.net.URL;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import cup.edu.lxq.game.Evaluate;
import cup.edu.lxq.game.Scan;
import cup.edu.lxq.game.Sort;
public class ChessBoard extends JPanel implements MouseListener
{
private Image whiteChessImage; //白棋图片
private Image blackChessImage; //黑棋图片
private Image winChessImage; //胜利后的图片
private int orient; //棋子的方向
Dimension size;
private boolean start = false; //游戏是否开始
private boolean flagWin = false; //是否一方胜出
private boolean isBlack = true; //是否轮到黑方下棋
private int step = 0; //记录第几步棋
Scan scanPeople=new Scan();
Scan scanComputer=new Scan();
Evaluate evaluatePeople=new Evaluate();
Evaluate evaluateComputer=new Evaluate();
Sort sort=new Sort();
//记录当前棋子的位置
private int x = 0;
private int y = 0;
//记录白棋下过的位置
private int x1 = -1;
private int y1 = -1;
//记录鼠标点击的位置 0表示无棋子,1表示黑棋,2表示白棋
protected int[][] allChess = new int[15][15];
public boolean isBlack() {
return isBlack;
}
public void setBlack(boolean isBlack) {
this.isBlack = isBlack;
}
public void setStep(int step)
{
this.step = step;
}
public int getSetp()
{
return step;
}
public void setAllChess()
{
for(int i = 0; i < 15; i++)
for(int j = 0; j < 15; j++)
{
allChess[i][j] = 0;
}
}
public boolean isStart() {
return start;
}
public void setStart(boolean start) {
this.start = start;
}
public boolean isFlagWin() {
return flagWin;
}
public void setFlagWin(boolean flagWin) {
this.flagWin = flagWin;
}
public ChessBoard()
{
URL whiteURL = getClass().getResource("/res/whiteChessman.png");
URL blackURL = getClass().getResource("/res/blackChessman.png");
URL winChessURL = getClass().getResource("/res/winChess.gif");
//添加鼠标监听
this.addMouseListener(this);
//初始化白棋和黑棋图片
whiteChessImage = new ImageIcon(whiteURL).getImage();
blackChessImage = new ImageIcon(blackURL).getImage();
winChessImage = new ImageIcon(winChessURL).getImage();
size = new Dimension(this.getWidth(), this.getHeight());
this.setPreferredSize(size);
this.setOpaque(false);
this.setLayout(null);
}
@Override
public void paint(Graphics g1)
{
// TODO Auto-generated method stub
Graphics2D g = (Graphics2D) g1;
super.paint(g);
this.drawPanel(g);
}
private void drawPanel(Graphics2D g)
{
//TODO Auto-generated method stub
Composite composite = g.getComposite();
Color color = g.getColor();
g.setComposite(AlphaComposite.SrcOver.derive(0.6f));
g.setColor(new Color(0xaabbaa));
g.fill3DRect(0, 0, getWidth(), this.getHeight(), true);
g.setComposite(composite);
g.setColor(color);
int w = getWidth(); // 棋盘宽度
int h = getHeight(); // 棋盘高度
int chessW = w / 15, chessH = h / 15; // 棋子宽度和高度
int left = chessW / 2 + (w % 15) / 2; // 棋盘左边界
int right = left + chessW * 14; // 棋盘右边界
int top = chessH / 2 + (h % 15) / 2; // 棋盘上边界
int bottom = top + chessH * 14; // 棋盘下边界
for (int i = 0; i < 15; i++) {
// 画每条横线
g.drawLine(left, top + (i * chessH), right, top + (i * chessH));
}
for (int i = 0; i < 15; i++) {
// 画每条竖线
g.drawLine(left + (i * chessW), top, left + (i * chessW), bottom);
}
g.fillOval(284, 284, 6, 6);
g.fillOval(132, 284, 6, 6);
g.fillOval(436, 284, 6, 6);
g.fillOval(132, 437, 6, 6);
g.fillOval(284, 437, 6, 6);
g.fillOval(436, 436, 6, 6);
g.fillOval(132, 132, 6, 6);
g.fillOval(284, 132, 6, 6);
g.fillOval(436, 132, 6, 6);
for(int i = 0; i < 15; i++)
for(int j = 0; j < 15; j++)
{
int tempX = i * 38 + 20; //计算棋子的横坐标
int tempY = j * 38 + 20; //计算棋子的纵坐标
//画黑棋
if(allChess[i][j] == 1)
{
g.drawImage(blackChessImage, tempX - 17, tempY - 17, this);
}
//画白棋
if(allChess[i][j] == 2)
{
//画出这个白棋
if(x1 == i && y1 == j && this.flagWin == false)
{
g.drawImage(whiteChessImage, tempX - 17, tempY - 17, this);
g.drawImage(winChessImage, tempX - 17, tempY - 17, this);
}
else
{
g.drawImage(whiteChessImage, tempX - 17, tempY - 17, this);
}
}
}
//画连五的棋子
if(this.flagWin == true)
{
drawWinChess(g);
}
}
private void drawWinChess(Graphics2D g)
{
// TODO Auto-generated method stub
int color = allChess[x][y];
int realX = x * 38 + 20;
int realY = y * 38 + 20;
g.drawImage(winChessImage, realX - 17, realY - 17, this);
if(orient == 0)
{
int tempY = y;
while(y-1>=0 && allChess[x][--y]==color)
{
realX = x * 38 + 20;
realY = y * 38 + 20;
g.drawImage(winChessImage, realX - 17, realY - 17, this);
}
y = tempY;
while(y+1<15 && allChess[x][++y]==color)
{
realX = x * 38 + 20;
realY = y * 38 + 20;
g.drawImage(winChessImage, realX - 17, realY - 17, this);
}
}
else if(orient == 1)
{
int tempX = x;
int tempY = y;
while(x-1>=0 && y-1>=0 && allChess[--x][--y]==color)
{
realX = x * 38 + 20;
realY = y * 38 + 20;
g.drawImage(winChessImage, realX - 17, realY - 17, this);
}
x=tempX; y=tempY;
while(x+1<15 && y+1<15 && allChess[++x][++y]==color)
{
realX = x * 38 + 20;
realY = y * 38 + 20;
g.drawImage(winChessImage, realX - 17, realY - 17, this);
}
}
else if(orient == 2)
{
int tempX = x;
while(x-1>=0 && allChess[--x][y]==color)
{
realX = x * 38 + 20;
realY = y * 38 + 20;
g.drawImage(winChessImage, realX - 17, realY - 17, this);
}
x = tempX;
while(x+1<15 && y+1<15 && allChess[++x][y]==color)
{
realX = x * 38 + 20;
realY = y * 38 + 20;
g.drawImage(winChessImage, realX - 17, realY - 17, this);
}
}
else
{
int tempX = x;
int tempY = y;
while(x-1>=0 && y+1<15 && allChess[--x][++y]==color)
{
realX = x * 38 + 20;
realY = y * 38 + 20;
g.drawImage(winChessImage, realX - 17, realY - 17, this);
}
x=tempX; y=tempY;
while(x+1<15 && y-1>=0 && allChess[++x][--y]==color)
{
realX = x * 38 + 20;
realY = y * 38 + 20;
g.drawImage(winChessImage, realX - 17, realY - 17, this);
}
}
}
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e)
{
// TODO Auto-generated method stub
x = e.getX();
y = e.getY();
//System.out.println(x + "," + y);
if (start)
{
if (x >= 12 && x <= 558 && y >= 12 && y <= 558 && this.flagWin == false)
{
//确保落子的位置靠近后边的交叉点
if ((x - 20) % 38 > 19) {
x = x + 19;
}
if ((y - 20) % 38 > 19) {
y = y + 19;
}
//求出该点对应的allChess坐标
x = (x - 20) / 38;
y = (y - 20) / 38;
if(isBlack == true)
{
manSetChess(x, y);
}
if(isBlack == false)
{
computerSetChess();
this.repaint();
}
}
}
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
//电脑下棋
private void computerSetChess()
{
// TODO
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
JAVAlFiveChess.rar (35个子文件)
JAVAlFiveChess
.project 390B
bin
cup
edu
lxq
game
ChessPanel$1.class 861B
Scan.class 2KB
ChessPanel$2.class 861B
ChessPanel.class 4KB
ChessBoard.class 10KB
MainFrame.class 1KB
ChessPanel$3.class 734B
ChessPanel$5.class 1KB
ChessPanel$4.class 734B
Evaluate.class 6KB
Sort.class 967B
res
blackChessman.png 4KB
winChess.gif 5KB
whiteChess.png 6KB
infoPanelLeft.png 16KB
blackChess.png 6KB
whiteChessman.png 4KB
infoPanel.png 17KB
.settings
org.eclipse.jdt.core.prefs 629B
src
cup
edu
lxq
game
MainFrame.java 968B
Sort.java 1KB
ChessPanel.java 6KB
Evaluate.java 11KB
Scan.java 3KB
ChessBoard.java 12KB
res
blackChessman.png 4KB
winChess.gif 5KB
whiteChess.png 6KB
infoPanelLeft.png 16KB
blackChess.png 6KB
whiteChessman.png 4KB
infoPanel.png 17KB
FiveChess.jar 76KB
.classpath 302B
共 35 条
- 1
S090071586
- 粉丝: 38
- 资源: 8
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
前往页