The previous article talks about Java implementing the two Gozi chess game (2) Drawing the chessboard, and the chessboard has been drawn. Next, we need to implement the control function, the main functions:
1) Choose a chess piece
2) Draw chess pieces
3) Judge the winner
4) Exchange chess
Realize the PART of the chess piece drawing first
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
First, define a chess piece class. This class has two attributes: the color of the piece (0- means black and 1- means white), and whether to make a move (I plan to use a two-dimensional array to store the chess piece's turn information)
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; } } Then we have drawn the code part of the chessboard in the previous part and added the code for drawing chess pieces. I used two chess pieces (one white and one black, located in the chessboard [8, 8], [7, 7]) to test the code for drawing chess pieces.
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; //Draw horizontal line for(int i=0;i<ROWS;i++) { g.drawLine(x, y+i*span_y, FWidth-x,y+i*span_y); } //Draw vertical line for(int i=0;i<ROWS;i++) { g.drawLine(x+i*span_x, y, x+i*span_x,FHeight-y); } //Draw chess pieces 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); } } } } } } } } The main module code remains unchanged
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 title setTitle("Stand-alone Goji"); Container containerPane =getContentPane(); containerPane.add(drawChessBoard); } public static void main(String[] args) { Main m = new Main(); m.setSize(800, 800); m.setVisible(true); } }Run it!
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.