import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
public class MyJFrame extends JFrame implements MouseListener {undefined
int qx = 20, qy = 40, qw = 490, qh = 490;//棋盘位置、宽⾼
int bw = 150, bh = 50, bx = 570, by = 150;//按钮宽⾼、位置
int x = 0, y = 0;//保存棋⼦坐标
int[][] SaveGame = new int[15][15];//保存每个棋⼦
int qc = 1;//记录⽩棋=2,⿊棋=1
int qn = 0;//判断棋⼦是否重复
boolean canplay = true;//判断游戏是否开始和结束
String go = "⿊⼦先⾏";//游戏信息
//---------------------------------------------------------------------------------------------------------------------
//窗体
public void myJFrame() {undefined
this.setTitle("五⼦棋"); //标题
this.setSize(800, 550); //窗⼝⼤⼩
this.setResizable(false); //窗⼝是否可以改变⼤⼩=否
this.setDefaultCloseOperation(MyJFrame.EXIT_ON_CLOSE);//窗⼝关闭⽅式为关闭窗⼝同时结束程序int width = DefaultToolkit().getScreenSize().width;//获取屏幕宽度
int height = DefaultToolkit().getScreenSize().height;//获取屏幕⾼度
// System.out.println("宽度:"+width);//测试
// System.out.println("⾼度:"+height);//测试
this.setLocation((width - 800) / 2, (height - 600) / 2); //设置窗⼝默认位置以屏幕居中
this.addMouseListener(this);
this.setVisible(true); //窗⼝是否显⽰=是
}
//---------------------------------------------------------------------------------------------------------------------
//覆写paint⽅法,绘制界⾯
public void paint(Graphics g) {undefined
//双缓冲技术防⽌屏幕闪烁
BufferedImage bi = new BufferedImage(800, 550, BufferedImage.TYPE_INT_ARGB);
Graphics g2 = bi.createGraphics();
/
/获取图⽚路径
BufferedImage image = null;
try {undefined
image = ad(new File("D:/#Java/五⼦棋/tp/wzqbj.jpg"));
} catch (IOException e) {undefined
e.printStackTrace();
}
g2.drawImage(image, 10, 10, this);//显⽰图⽚
g2.setColor(Color.BLACK);//设置画笔颜⾊
g2.setFont(new Font("华⽂⾏楷", 10, 50));//设置字体
g2.drawString("晓时五⼦棋", 525, 100);//绘制字符
/
/棋盘
g2.HSBColor(30, (float) 0.10, (float) 0.90));//设置画笔颜⾊g2.fillRect(qx, qy, qw, qh);//绘制棋盘背景矩形
//开始按钮
g2.setColor(Color.WHITE);//设置画笔颜⾊
g2.fillRect(bx, by, bw, bh);//绘制开始按钮
g2.setFont(new Font("华⽂⾏楷", 10, 30));//设置字体
g2.setColor(Color.black);//设置画笔颜⾊
g2.drawString("开始", 615, 185);//绘制字符
//悔棋按钮
g2.setColor(Color.LIGHT_GRAY);//设置画笔颜⾊
g2.fillRect(bx, by + 60, bw, bh);//绘制悔棋按钮
g2.setFont(new Font("华⽂⾏楷", 10, 30));//设置字体
g2.setColor(Color.WHITE);//设置画笔颜⾊
g2.drawString("悔棋", 615, 245);//绘制字符
//认输按钮
g2.setColor(Color.GRAY);//设置画笔颜⾊
g2.fillRect(bx, by + 120, bw, bh);//绘制认输按钮
g2.setFont(new Font("华⽂⾏楷", 10, 30));//设置字体
g2.setColor(Color.WHITE);//设置画笔颜⾊
g2.drawString("认输", 615, 305);//绘制字符
//游戏信息栏
g2.HSBColor(30, (float) 0.10, (float) 0.90));//设置画笔颜⾊
g2.fillRect(550, 350, 200, 150);//绘制游戏状态区域
g2.setColor(Color.black);//设置画笔颜⾊
g2.setFont(new Font("⿊体", 10, 20));//设置字体
g2.drawString("游戏信息", 610, 380);//绘制字符
g2.drawString(go, 610, 410);//绘制字符
g2.drawString("作者:晓时⾕⾬", 560, 440);//绘制字符
g2.drawString("联系⽅式:", 560, 465);//绘制字符
简单的java游戏代码g2.drawString("qq 717535996", 560, 490);//绘制字符
g2.setColor(Color.BLACK);//设置画笔颜⾊
//绘制棋盘格线
for (int x = 0; x <= qw; x += 35) {undefined
g2.drawLine(qx, x + qy, qw + qx, x + qy);//绘制⼀条横线
g2.drawLine(x + qx, qy, x + qx, qh + qy);//绘制⼀条竖线
}
//绘制标注点
for (int i = 3; i <= 11; i += 4) {undefined
for (int y = 3; y <= 11; y += 4) {undefined
g2.fillOval(35 * i + qx - 3, 35 * y + qy - 3, 6, 6);//绘制实⼼圆}
}
//绘制棋⼦
for (int i = 0; i < 15; i++) {undefined
for (int j = 0; j < 15; j++) {undefined
if (SaveGame[i][j] == 1)//⿊⼦
{undefined
int sx = i * 35 + qx;
int sy = j * 35 + qy;
g2.setColor(Color.BLACK);
g2.fillOval(sx - 13, sy - 13, 26, 26);//绘制实⼼圆
}
if (SaveGame[i][j] == 2)//⽩⼦
{undefined
int sx = i * 35 + qx;
int sy = j * 35 + qy;
g2.setColor(Color.WHITE);
g2.fillOval(sx - 13, sy - 13, 26, 26);//绘制实⼼圆
g2.setColor(Color.BLACK);
g2.drawOval(sx - 13, sy - 13, 26, 26);//绘制空⼼圆
}
}
}
g.drawImage(bi, 0, 0, this);
// g.drawRect(20, 20, 20, 20);//绘制空⼼矩形
}
//---------------------------------------------------------------------------------------------------------------------//判断输赢
private boolean WinLose() {undefined
boolean flag = false;//输赢
int count = 1;//相连数
int color = SaveGame[x][y];//记录棋⼦颜⾊
//判断横向棋⼦是否相连
int i = 1;//迭代数
while (color == SaveGame[x + i][y]) {undefined
count++;
i++;
}
i = 1;//迭代数
while (color == SaveGame[x - i][y]) {undefined
count++;
i++;
}
if (count >= 5) {undefined
flag = true;
}
//判断纵向棋⼦是否相连
count = 1;
i = 1;//迭代数
while (color == SaveGame[x][y + i]) {undefined
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论