Get the App
SLTechnology News&Howtos  ›  Development  › 

How to realize Gobang stand-alone version with Java

Shulou Source: shulou.com Published: 2022-05-31 18:33:50 10月02日 Update

This article mainly introduces Java how to achieve Gobang stand-alone version of the relevant knowledge, the content is detailed and easy to understand, the operation is simple and fast, has a certain reference value, I believe that after reading this Java how to achieve Gobang stand-alone version of the article will have a harvest, let's take a look.

Java Gobang design process:

1. Create a window and design a chessboard interface

two。 Achieve mouse click, chess pieces appear, black and white chess in turn

3. Be able to judge whether to win or lose

4. Add button function

Implementation result diagram:

Import java.awt.BorderLayout;import java.awt.Color;import java.awt.Cursor;import java.awt.FlowLayout;import java.awt.Graphics;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.KeyEvent;import java.awt.event.KeyListener;import java.awt.event.MouseEvent;import java.awt.event.MouseListener;import java.awt.image.BufferedImage;import java.io.File;import java.io.IOException;import javax.imageio.ImageIO;import javax.swing.JButton Import javax.swing.JFrame;import javax.swing.JOptionPane;import javax.swing.JPanel; public class Test {public static void main (String [] args) {new MyFrame ();}} class MyFrame extends JFrame implements MouseListener {/ / Save coordinates int x; int y; int x1; int y1 / / number of sunspots / / number of white spots / / 1 is black, 2 is white / / default starts with black flag int flag=1; / / indicates whether the game is over / / true game starts, false game ends, no more boolean canPlay=true / / Save the coordinates of previously played pieces / /'0' for no pieces,'1' for black,'2' for white int [] [] allChess=new int [19] [19]; / / int [] [] allChess=new int [25] [25]; / / the total number of current pieces int chessSum=0; BufferedImage bgImage = null; JButton withdraw=new JButton ("repentance"); JButton restart=new JButton ("start over") JButton exit=new JButton ("quit"); JPanel south=new JPanel (); public MyFrame () {this.setTitle (Gobang); setSize (630700); setLayout (new BorderLayout ()); setLocationRelativeTo (null); setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); try {bgImage=ImageIO.read ("C:\ Users\\ us\\ Desktop\ 1.jpg")) } catch (IOException E1) {e1.printStackTrace ();} addMouseListener (this); / / add the form to the listening south.setLayout (new FlowLayout (FlowLayout.LEFT,60,30)); south.add (restart); south.add (withdraw); south.add (exit) / / initialize button event listener inner class MybuttonListener buttonListener = new MybuttonListener (); / / register three button events restart.addActionListener (buttonListener); withdraw.addActionListener (buttonListener); exit.addActionListener (buttonListener); / / add button panels to the southern this.add of the form (south,BorderLayout.SOUTH); setVisible (true) } public void paint (Graphics g) {int tempSum=chessSum; / / chessboard g.drawImage (bgImage,8,30,this); for (int colum=58;colum)

Tags: Gobang Gobang upper representative button Liancheng stand-alone stand-alone event monitor chessboard white knowledge black black content coordinates total horizontal Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft Linux Shulou Technology Huawei MySQL