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,五子棋
0
投稿
猜你喜欢
Java Kafka实现延迟队列的示例代码
2022-01-11 16:32:11
c# 实例——绘制波浪线(附源码)
2023-03-02 12:53:54
IntelliJ IDEA Project窗口的一些设置详解
2023-11-09 04:54:44
springboot如何获取相对路径文件夹下静态资源的方法
2023-07-12 06:23:09
ShardingSphere jdbc集成多数据源的实现步骤
2023-11-25 07:54:56
SpringCloud超详细讲解微服务网关Gateway
2021-09-11 14:54:49
C#随机生成Unicode类型字符串
2023-12-05 08:42:29
详解Springboot分布式限流实践
2021-07-12 14:29:18
C# 使用CancellationTokenSource取消多线程
2023-11-15 00:31:54
Quartz与Spring集成的两种方法示例
2022-05-22 05:15:41
实例详解SpringBoot默认的JSON解析方案
2023-07-21 07:34:20
Java系统的高并发解决方法详解
2021-08-23 08:39:50
SpringBoot整合canal实现数据同步的示例代码
2022-05-07 19:51:24
springboot集成spring cache缓存示例代码
2021-10-20 07:57:54
Spring JPA之find拓展方法示例详解
2021-12-11 03:50:49
spring boot多数据源动态切换代码实例
2022-03-11 00:37:09
Android UI设计系列之ImageView实现ProgressBar旋转效果(1)
2021-06-07 11:08:21
java调用外部程序的方法及代码演示
2023-11-13 22:42:55
Flutter基于Dart Unwrapping Multiple Optional小技巧
2023-07-05 11:44:19
Java LinkedHashMap深入分析源码
2022-07-31 03:05:43