Java實(shí)現(xiàn)兩人五子棋游戲(三) 畫(huà)出棋子
上一篇文章講的是Java實(shí)現(xiàn)兩人五子棋游戲(二) 畫(huà)出棋盤(pán),已經(jīng)畫(huà)好棋盤(pán),接下來(lái)要實(shí)現(xiàn)控制功能,主要功能:
1)選擇棋子
2)畫(huà)棋子
3)判斷勝負(fù)
4)交換行棋方
先實(shí)現(xiàn)畫(huà)棋子PART
-------------畫(huà)棋子代碼示例如下--------------

首先,定義一個(gè)棋子類(lèi),這個(gè)類(lèi)有兩個(gè)屬性,棋子顏色(0-表示黑色,1-表示白色),是否落子(我計(jì)劃用一個(gè)二維數(shù)組才存儲(chǔ)棋子的落子信息)
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;
}
}
接著我們上一部分的畫(huà)好棋盤(pán)的代碼部分,新增畫(huà)棋子的代碼,我用兩個(gè)棋子(一白一黑,分別位于棋盤(pán)的【8,8】,【7,7】)來(lái)檢驗(yàn)畫(huà)棋子的代碼
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.Color;
import javax.swing.JPanel;
public class DrawChessBoard extends JPanel{
final static int BLACK=0;
final static int WHITE=1;
public int chessColor = BLACK;
public Image boardImg;
final private int ROWS = 19;
Chessman[][] chessStatus=new Chessman[ROWS][ROWS];
public DrawChessBoard() {
boardImg = Toolkit.getDefaultToolkit().getImage("res/drawable/chessboard2.png");
if(boardImg == null)
System.err.println("png do not exist");
//test draw chessman part simple
Chessman chessman=new Chessman(0, true);
chessStatus[7][7]=chessman;
Chessman chessman2 = new Chessman(1, true);
chessStatus[8][8]=chessman2;
//test draw chessman part simple
}
@Override
protected void paintComponent(Graphics g) {
// TODO Auto-generated method stub
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;
g.drawImage(boardImg, x, y, null);
int margin = x;
int span_x=imgWidth/ROWS;
int span_y=imgHeight/ROWS;
//畫(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;i++)
{
for(int j=0;j<ROWS;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;
int chessman_width=20;
float radius_b=20;
float radius_w=50;
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);
}
}
}
}
}
主模塊代碼不變
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標(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實(shí)現(xiàn)五子棋小游戲
- java實(shí)現(xiàn)單機(jī)版五子棋
- java基于swing實(shí)現(xiàn)的五子棋游戲代碼
- Java實(shí)現(xiàn)五子棋網(wǎng)絡(luò)版
- Java實(shí)現(xiàn)兩人五子棋游戲(二) 畫(huà)出棋盤(pán)
- java實(shí)現(xiàn)單人版五子棋游戲
- Java編程實(shí)現(xiàn)五子棋人人對(duì)戰(zhàn)代碼示例
- Java實(shí)現(xiàn)五子棋AI算法
- Java棋類(lèi)游戲?qū)嵺`之單機(jī)版五子棋
- Java實(shí)現(xiàn)五子棋游戲(控制臺(tái)版)
相關(guān)文章
查看本地啟動(dòng)SpringBoot的本地端口號(hào)的幾種方式
這篇文章主要介紹了查看本地啟動(dòng)SpringBoot的本地端口號(hào)的幾種方式,文中通過(guò)代碼示例講解的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2024-10-10
java 如何掃描指定包下類(lèi)(包括jar中的java類(lèi))
這篇文章主要介紹了java 如何掃描指定包下類(lèi)(包括jar中的java類(lèi)),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-04-04
JavaWeb購(gòu)物車(chē)項(xiàng)目開(kāi)發(fā)實(shí)戰(zhàn)指南
之前沒(méi)有接觸過(guò)購(gòu)物車(chē)的東東,也不知道購(gòu)物車(chē)應(yīng)該怎么做,所以在查詢了很多資料,總結(jié)一下購(gòu)物車(chē)的功能實(shí)現(xiàn),下面這篇文章主要給大家介紹了關(guān)于JavaWeb購(gòu)物車(chē)項(xiàng)目開(kāi)發(fā)的相關(guān)資料,需要的朋友可以參考下2022-06-06
jar包運(yùn)行時(shí)提示jar中沒(méi)有主清單屬性的解決
這篇文章主要介紹了jar包運(yùn)行時(shí)提示jar中沒(méi)有主清單屬性的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-02-02

