Java實(shí)現(xiàn)兩人五子棋游戲(四) 落子動(dòng)作的實(shí)現(xiàn)
之前的兩篇文章:Java實(shí)現(xiàn)兩人五子棋游戲(二) 畫(huà)出棋盤(pán);Java實(shí)現(xiàn)兩人五子棋游戲(三) 畫(huà)出棋子

前面,我們已經(jīng)畫(huà)好的棋盤(pán)和棋子,接下來(lái),我們要通過(guò)鼠標(biāo)點(diǎn)擊屏幕獲取落子位置并落子(先不考慮行棋方和勝負(fù)判斷)。
步驟:
1)捕捉鼠標(biāo)按下的位置
2)經(jīng)過(guò)坐標(biāo)變換(由像素位置->0-19的棋盤(pán)位置)
3)更新記錄棋盤(pán)狀態(tài)的二維數(shù)組
4)重新渲染繪制棋盤(pán)。
-------------落子動(dòng)作代碼示例如下--------------

一個(gè)棋子類(lèi)Chessman.java
package xchen.test.simpleGobang;
public class Chessman {
private int color;//1-white,0-black
private boolean placed = false;
public Chessman(int color,boolean placed){
this.color=color;
this.placed=placed;
}
public boolean getPlaced() {
return placed;
}
public void setPlaced(boolean placed) {
this.placed = placed;
}
public int getColor() {
return color;
}
public void setColor(int color) {
this.color = color;
}
}
DrawChessBoard.java
package xchen.test.simpleGobang;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RadialGradientPaint;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.Color;
import javax.swing.JPanel;
public class DrawChessBoard extends JPanel implements MouseListener{
final static int BLACK=0;
final static int WHITE=1;
public int chessColor = BLACK;
int chessman_width=30;
public Image boardImg;
final private int ROWS = 19;
Chessman[][] chessStatus=new Chessman[ROWS+1][ROWS+1];
public DrawChessBoard() {
boardImg = Toolkit.getDefaultToolkit().getImage("res/drawable/chessboard2.png");
if(boardImg == null)
System.err.println("png do not exist");
addMouseListener(this);
}
@Override
protected void paintComponent(Graphics g) {
// TODO Auto-generated method stub
System.out.println("DRAW!!");
super.paintComponent(g);
int imgWidth = boardImg.getHeight(this);
int imgHeight = boardImg.getWidth(this);
int FWidth = getWidth();
int FHeight= getHeight();
int x=(FWidth-imgWidth)/2;
int y=(FHeight-imgHeight)/2;
int span_x=imgWidth/ROWS;
int span_y=imgHeight/ROWS;
g.drawImage(boardImg, x, y, null);
//畫(huà)橫線
for(int i=0;i<ROWS;i++)
{
g.drawLine(x, y+i*span_y, FWidth-x,y+i*span_y);
}
//畫(huà)豎線
for(int i=0;i<ROWS;i++)
{
g.drawLine(x+i*span_x, y, x+i*span_x,FHeight-y);
}
//畫(huà)棋子
for(int i=0;i<ROWS+1;i++)
{
for(int j=0;j<ROWS+1;j++)
{
if(chessStatus[i][j]!=null&&chessStatus[i][j].getPlaced()==true)
{
System.out.println("draw chessman "+i+" "+j);
int pos_x=x+i*span_x;
int pos_y=y+j*span_y;
float radius_b=40;
float radius_w=80;
float[] fractions = new float[]{0f,1f};
java.awt.Color[] colors_b = new java.awt.Color[]{Color.BLACK,Color.WHITE};
Color[] colors_w = new Color[]{Color.WHITE,Color.BLACK};
RadialGradientPaint paint;
if(chessStatus[i][j].getColor()==1)
{
System.out.println("draw white chess");
paint = new RadialGradientPaint(pos_x-chessman_width/2f, pos_y-chessman_width/2f, radius_w*2, fractions, colors_w);
}else{
System.out.println("draw black chess");
paint = new RadialGradientPaint(pos_x-chessman_width/2f, pos_y-chessman_width/2f, radius_b*2, fractions, colors_b);
}
((Graphics2D)g).setPaint(paint);
((Graphics2D)g).fillOval(pos_x-chessman_width/2,pos_y-chessman_width/2,chessman_width,chessman_width);
}
}
}
}
@Override
//當(dāng)用戶按下鼠標(biāo)按鈕時(shí)發(fā)生
public void mousePressed(MouseEvent e) {
int point_x=e.getX();
int point_y=e.getY();
int imgWidth = boardImg.getHeight(this);
int imgHeight = boardImg.getWidth(this);
int FWidth = getWidth();
int FHeight= getHeight();
int x=(FWidth-imgWidth)/2;
int y=(FHeight-imgHeight)/2;
int span_x=imgWidth/ROWS;
int span_y=imgHeight/ROWS;
System.out.println("press");
int status_x = 0;
int status_y = 0;
if(point_x>=x && point_x<=x+imgWidth && point_y>=y && point_y <= y+imgHeight)
{
System.out.println("合法");
for(int i=0;i<ROWS+1;i++)
{
if(point_x>=x-chessman_width/2+1+i*span_x)
{
if(point_x<=x+chessman_width/2-1+i*span_x)//如果是width/2會(huì)在中間點(diǎn)出現(xiàn)兩個(gè)匹配值
{
System.out.println("point x "+i+" "+point_x+" "+(x-chessman_width/2+i*span_x)+" "+(x+chessman_width/2+i*span_x));
status_x = i;
}
}
}
for(int i=0;i<ROWS+1;i++)
{
if(point_y>=y-chessman_width/2+1+i*span_y)
{
if(point_y <= y+chessman_width/2-1+i*span_y)
{
System.out.println("point y "+i+" "+point_y+" "+(y-chessman_width/2+1+i*span_y)+" "+(y+chessman_width/2-1+i*span_y));
status_y = i;
}
}
}
Chessman chessman = new Chessman(BLACK, true);
chessStatus[status_x][status_y]=chessman;
repaint();
}
System.out.println(status_x+" "+status_y+" "+chessStatus[status_x][status_y].getColor()+" "+chessStatus[status_x][status_y].getPlaced());
}
@Override
//當(dāng)用戶按下并松開(kāi)鼠標(biāo)按鈕時(shí)發(fā)生
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(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
}
}
主模塊代碼不變
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標(biāo)題
setTitle("單機(jī)五子棋");
Container containerPane =getContentPane();
containerPane.add(drawChessBoard);
}
public static void main(String[] args) {
Main m = new Main();
m.setSize(800, 800);
m.setVisible(true);
}
}
運(yùn)行一下

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Java五子棋單機(jī)版源碼分享
- Java實(shí)現(xiàn)五子棋網(wǎng)絡(luò)版
- Java實(shí)現(xiàn)兩人五子棋游戲(七) 屏幕提示信息
- Java實(shí)現(xiàn)兩人五子棋游戲(六) 行棋方變換
- Java實(shí)現(xiàn)兩人五子棋游戲(五) 判斷是否有一方勝出
- Java實(shí)現(xiàn)兩人五子棋游戲(三) 畫(huà)出棋子
- Java實(shí)現(xiàn)兩人五子棋游戲(二) 畫(huà)出棋盤(pán)
- Java實(shí)現(xiàn)五子棋AI算法
- Java編程實(shí)現(xiàn)五子棋人人對(duì)戰(zhàn)代碼示例
- java實(shí)現(xiàn)單機(jī)版五子棋
相關(guān)文章
MyBatis中基于別名typeAliases的設(shè)置
這篇文章主要介紹了MyBatis中基于別名typeAliases的設(shè)置,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07
Java使用synchronized修飾方法來(lái)同步線程的實(shí)例演示
synchronized下的方法控制多線程程序中的線程同步非常方便,這里就來(lái)看一下Java使用synchronized修飾方法來(lái)同步線程的實(shí)例演示,需要的朋友可以參考下2016-06-06
Spring data JPA只查詢(xún)部分字段問(wèn)題及解決
這篇文章主要介紹了Spring data JPA只查詢(xún)部分字段問(wèn)題及解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08
Java均攤復(fù)雜度和防止復(fù)雜度的震蕩原理分析
這篇文章主要介紹了Java均攤復(fù)雜度和防止復(fù)雜度的震蕩,結(jié)合實(shí)例形式分析了Java均攤復(fù)雜度和防止復(fù)雜度的震蕩相關(guān)概念、原理、實(shí)現(xiàn)方法與注意事項(xiàng),需要的朋友可以參考下2020-03-03
hibernate存取json數(shù)據(jù)的代碼分析
這篇文章主要介紹了hibernate存取json數(shù)據(jù)的代碼分析,需要的朋友可以參考下2017-09-09
springboot2 生產(chǎn)部署注意事項(xiàng)及示例代碼
這篇文章主要介紹了springboot2 生產(chǎn)部署注意事項(xiàng)及示例代碼,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-04-04
如何基于sqlite實(shí)現(xiàn)kafka延時(shí)消息詳解
這篇文章主要給大家介紹了關(guān)于如何基于sqlite實(shí)現(xiàn)kafka延時(shí)消息的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2022-01-01

