Java实现两人五子棋游戏(二) 画出棋盘
作者:v_xchen_v 时间:2022-08-26 15:09:57
本文为大家分享了java画出五子棋游戏棋盘的方法,供大家参考,具体内容如下
棋盘模块:
画五子棋棋盘:19条横线、19条竖线
步骤一:显示棋盘
我有一张名为chessboard.png的棋盘,位置为根目录/res/drawable/chessboard/png,现在我要显示这张图片。
DrawChessBoard.java
package xchen.test.simpleGobang;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import javax.swing.JPanel;
public class DrawChessBoard extends JPanel{
public Image boardImg;
public DrawChessBoard() {
boardImg = Toolkit.getDefaultToolkit().getImage("res/drawable/chessboard.png");
if(boardImg == null)
System.err.println("png do not exist");
}
@Override
protected void paintComponent(Graphics g) {
// TODO Auto-generated method stub
super.paintComponent(g);
int imgWidth = boardImg.getWidth(this);
int imgHeight = boardImg.getHeight(this);
int FWidth = getWidth();
int FHeight= getHeight();
int x=(FWidth-imgWidth)/2;
int y=(FHeight-imgHeight)/2;
g.drawImage(boardImg, x, y, null);
}
}
Main.java
package xchen.test.simpleGobang;
import java.awt.Container;
import javax.swing.JFrame;
import xchen.test.simpleGobang.DrawChessBoard;
public class Main extends JFrame{
private DrawChessBoard drawChessBoard;
public Main() {
drawChessBoard = new DrawChessBoard();
//Frame标题
setTitle("单机五子棋");
Container containerPane =getContentPane();
containerPane.add(drawChessBoard);
}
public static void main(String[] args) {
Main m = new Main();
m.setVisible(true);
}
}
运行一下
步骤二:为棋盘画上横竖线
DrawChessBoard.java
package xchen.test.simpleGobang;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import javax.swing.JPanel;
public class DrawChessBoard extends JPanel{
public Image boardImg;
final private int ROWS = 19;
public DrawChessBoard() {
boardImg = Toolkit.getDefaultToolkit().getImage("res/drawable/chessboard2.png");
if(boardImg == null)
System.err.println("png do not exist");
}
@Override
protected void paintComponent(Graphics g) {
// TODO Auto-generated method stub
super.paintComponent(g);
int imgWidth = boardImg.getWidth(this);
int imgHeight = boardImg.getHeight(this);
int FWidth = getWidth();
int FHeight= getHeight();
int x=(FWidth-imgWidth)/2;
int y=(FHeight-imgHeight)/2;
g.drawImage(boardImg, x, y, null);
int margin = x;
int span_x=imgWidth/ROWS;
int span_y=imgHeight/ROWS;
//画横线
for(int i=0;i<ROWS;i++)
{
g.drawLine(x, y+i*span_y, FWidth-x,y+i*span_y);
}
//画竖线
for(int i=0;i<ROWS;i++)
{
g.drawLine(x+i*span_x, y, x+i*span_x,FHeight-y);
}
}
}
Main.java不变
运行一下
遇到的问题:
1)Eclipse不识别文件夹下的图片
问题:文件夹中有图片,但是在Eclipse项目栏中不显示
解决办法:在Eclipse中,选中根目录,F5 refresh,就显示出来了。
更多有趣的经典小游戏实现专题,分享给大家:
C++经典小游戏汇总
python经典小游戏汇总
python俄罗斯方块游戏集合
JavaScript经典游戏 玩不停
java经典小游戏汇总
javascript经典小游戏汇总
来源:https://blog.csdn.net/v_xchen_v/article/details/53420203
标签:java,五子棋
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
一篇文章带你了解XGBoost算法
2021-10-10 01:14:45
![](https://img.aspxhome.com/file/2023/2/61082_0s.jpg)
详解Java变量与常量
2023-11-09 21:00:14
![](https://img.aspxhome.com/file/2023/5/59005_0s.png)
Java8中的LocalDateTime你会使用了吗
2023-11-15 07:02:07
Java实现MD5消息摘要算法
2023-06-18 06:37:35
java图形用户界面实现菜单功能
2023-11-23 11:50:58
![](https://img.aspxhome.com/file/2023/2/59032_0s.jpg)
Android中外接键盘的检测的实现
2023-07-27 21:15:13
C#中 MessageBox的使用技巧
2023-06-25 16:15:48
![](https://img.aspxhome.com/file/2023/2/85682_0s.webp)
关于MyBatis模糊查询的几种实现方式
2023-05-09 04:23:12
Java 超详细讲解数据结构中的堆的应用
2021-08-19 08:10:53
![](https://img.aspxhome.com/file/2023/8/62408_0s.png)
Java中Function的使用及说明
2023-08-12 03:04:29
![](https://img.aspxhome.com/file/2023/5/58025_0s.png)
SpringBoot路径映射实现过程图解
2023-11-13 04:01:11
![](https://img.aspxhome.com/file/2023/6/59376_0s.png)
Windows编写jar启动脚本和关闭脚本的操作方法
2021-05-28 04:36:58
![](https://img.aspxhome.com/file/2023/8/61298_0s.png)
ShardingSphere数据分片算法及测试实战
2023-11-28 02:23:03
![](https://img.aspxhome.com/file/2023/8/59768_0s.png)
Java实现登录与注册页面
2023-04-17 03:21:20
![](https://img.aspxhome.com/file/2023/1/61001_0s.jpg)
Java四种权限修饰符知识点详解
2023-11-11 06:12:59
Java里的static在Kotlin里如何实现
2023-08-24 20:25:33
Spring注解驱动之BeanDefinitionRegistryPostProcessor原理解析
2023-11-24 23:24:21
![](https://img.aspxhome.com/file/2023/8/59758_0s.jpg)
springboot 通过代码自动生成pid的方法
2023-11-27 02:54:57
![](https://img.aspxhome.com/file/2023/6/60366_0s.png)
SpringIOC框架的简单实现步骤
2021-10-28 21:44:35
java实现五子棋小游戏
2021-12-25 06:58:56
![](https://img.aspxhome.com/file/2023/0/63380_0s.jpg)