This article shares with you the method of drawing Gozi Chess game board in Java for your reference. The specific content is as follows
Checkerboard module:
Draw Gozi Chess Board: 19 horizontal lines, 19 vertical lines
Step 1: Show the chessboard
I have a chessboard called chessboard.png, located at the root directory /res/drawable/chessboard/png, and now I want to display this image.
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 title setTitle("Stand-alone Goji"); Container containerPane =getContentPane(); containerPane.add(drawChessBoard); } public static void main(String[] args) { Main m = new Main(); m.setVisible(true); } }Run it
Step 2: Draw horizontal and vertical lines for the chessboard
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; //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); } } }Main.java runs unchanged
Problems encountered:
1) Eclipse does not recognize pictures under folders
Problem: There are pictures in the folder, but they are not displayed in the Eclipse project bar
Solution: In Eclipse, select the root directory, F5 refresh, and it will be displayed.
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.