When I was about to learn JavaGUI programming, I wrote a small game Goji.
Currently, only a stand-alone version has been implemented, and everything is simply produced. If you have any questions, please give me some advice.
The chessboard and chess pieces here are not pictures, and they are all drawn using Graphics in Java.
The source code has been submitted to GitHub, click here to GitHub source code
The following list of Gozi chess categories:
import java.awt.BorderLayout;import java.awt.Color;import java.awt.Container;import java.awt.Graphics;import java.awt.event.MouseEvent;import java.awt.event.MouseListener;import java.util.Vector;import javax.swing.JFrame;import javax.swing.JOptionPane;public class FiveInARow extends JFrame implements MouseListener { Vector v = new Vector(); //All chess information for each step Vector white = new Vector(); //White chess information Vector black = new Vector(); //Black chess information boolean b; //Used to judge whether the white flag or black chess int whiteCount, blackCount; //Calculate the number of regret chess steps int w = 25; //Spacing size int px = 100, py = 100; //The size of the board int pxw = px + w, pyw = py + w; int width = w * 16, height = w * 16; int vline = width + px; //The length of the vertical line int hline = height + py; //Length of horizontal line/** *Construction method*/ public FiveInARow() { super("stand-alone Goji"); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//Close button Container con = this.getContentPane(); con.setLayout(new BorderLayout()); this.addMouseListener(this);//Add listener this.setSize(600, 600);//Set the form size this.setBackground(Color.orange); this.setVisible(true); } /** * Draw chess board and chess pieces* @param e */ public void paint(Graphics g) { g.clearRect(0, 0, this.getWidth(), this.getHeight());//Clear the artboard g.setColor(Color.BLACK);//Draw the grid color g.drawRect(px, py, width, height);//Grid size g.drawString("Stand-alone Gozi mini game, right-click to regret chess, welcome to use", 180, 70); for (int i=0; i<15; i++) { g.drawLine(pxw+i*w, py, pxw+i*w, hline);//Each horizontal and vertical line g.drawLine(px, pyw+i*w, vline, pyw+i*w); } for (int x=0; x<v.size(); x++) { String str = (String)v.get(x); String tmp[] = str.split("-"); int a = Integer.parseInt(tmp[0]); int b = Integer.parseInt(tmp[1]); a = a * w + px; b = b * w + py; if (x%2 == 0) { g.setColor(Color.WHITE); } else { g.setColor(Color.BLACK); } g.fillArc(aw/2, bw/2, w, w, 0, 360); } } @Override public void mouseClicked(MouseEvent e) { if (e.getButton() == e.BUTTON1) { int x = e.getX(); int y = e.getY(); x = (x - x % w) + (x % w > w / 2 ? w : 0); y = (y - y % w) + (y % w > w / 2 ? w : 0); x = (x - px) / w; y = (y - py) / w; if (x >= 0 && y >= 0 && x <= 16 && y <= 16) { if (v.contains(x+"-"+y)) { System.out.println("There is already a chess!"); } else { v.add(x+"-"+y); this.repaint(); if (v.size() % 2 == 0) { black.add(x+"-"+y); this.victory(x, y, black);// System.out.println("Black Chess"); } else { white.add(x+"-"+y); this.victory(x, y, white);// System.out.println("White Chess"); }// System.out.println(e.getX()+"-"+e.getY()); } } else {// System.out.println(e.getX()+"-"+e.getY()+"|"+x+"-"+y+"/t exceeds the boundary"); } } if (e.getButton() == e.BUTTON3) { // Method to right-click the chess// System.out.println("Right-click the chess-- chess"); if (v.isEmpty()) { JOptionPane.showMessageDialog(this, "There is no chess to regret"); } else { if (v.size() % 2 == 0) { //Judge whether it is a white chess regretting the chess, or black chess regretting the chess blackCount++; if (blackCount > 3) { JOptionPane.showMessageDialog(this, "Black chess has regretted 3 steps"); } else { v.remove(v.lastElement()); this.repaint(); } } else { whiteCount++; if (whiteCount > 3) { JOptionPane.showMessageDialog(this, "Black chess has regretted 3 steps"); } else { v.remove(v.lastElement()); this.repaint(); } } } } } } } /** * Method to judge victory* @param x * @param y * @param contains */ private void victory(int x, int y, Vector contains) { int cv = 0; //Number of chess pieces in vertical direction int ch = 0; //Number of horizontal chess pieces int ci1 = 0; //Number of chess pieces in slope direction 1 int ci2 = 0; //Number of chess pieces in slope direction 2 //Calculate the number of chess pieces in horizontal direction for (int i=1; i<5; i++) { if (contain.contains((x+i)+"-"+y)) { ch++; } else { break; } } for (int i=1; i<5; i++) { if (contain.contains((xi)+"-"+y)) { ch++; } else { break; } } //Calculate the number of chess pieces in the vertical direction for (int i=1; i<5; i++) { if (contain.contains(x+"-"+(y+i))) { cv++; } else { break; } } for (int i=1; i<5; i++) { if (contain.contains(x+"-"+(yi))) { cv++; } else { break; } } //Calculate the number of chess pieces in the 45° inclined direction for (int i=1; i<5; i++) { if (contain.contains((x+i)+"-"+(y+i))) { ci1++; } else { break; } } for (int i=1; i<5; i++) { if (contain.contains((xi)+"-"+(yi))) { ci1++; } else { break; } } //Calculate the number of chess pieces in the 135° slope direction for (int i=1; i<5; i++) { if (contain.contains((x+i)+"-"+(yi))) { ci2++; } else { break; } } for (int i=1; i<5; i++) { if (contain.contains((x+i)+"-"+(yi))) { ci2++; } else { break; } } for (int i=1; i<5; i++) { if (contain.contains((xi)+"-"+(y+i))) { ci2++; } else { break; } } if (ch>=4 ||cv>=4 ||ci1>=4 ||ci2>=4) { System.out.println(v.size()+"Step"); if (v.size() % 2 == 0) { //Judge whether it is black or white to win JOptionPane.showMessageDialog(null, "Black"); } else { JOptionPane.showMessageDialog(null, "White"); } this.v.clear(); this.black.clear(); this.white.clear(); this.repaint(); } } @Override public void mouseEntered(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseExited(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mousePressed(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mousePressed(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseReleased(MouseEvent e) { // TODO Auto-generated method stub }}PS: I plan to write another online version of Goji, but unfortunately I don’t know much about the Internet in Java. I have to study for a while before continuing to complete it and test my personal learning results.
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.