Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize simple Gobang Mini Game with Java

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces "how to achieve simple Gobang Mini Game with Java". In daily operation, I believe many people have doubts about how to achieve simple Gobang Mini Game with Java. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the doubts about "how to achieve simple Gobang Mini Game with Java". Next, please follow the editor to study!

1. Practical objectives:

1. Master JavaGUI interface design

two。 Master monitoring of mouse events (MouseListener,MouseMotionListener)

Second, the content of practice:

Design a simple Gobang program, can achieve Gobang playing process. As shown in the following figure

Gobang operation interface

1. Gobang chessboard

Package cn.edu.ouc.fiveChess; import java.awt.Color;import java.awt.Cursor;import java.awt.Dimension;import java.awt.Graphics;import java.awt.Graphics2D;import java.awt.Image;import java.awt.RadialGradientPaint;import java.awt.RenderingHints;import java.awt.Toolkit;import java.awt.event.MouseEvent;import java.awt.event.MouseListener;import java.awt.event.MouseMotionListener;import java.awt.geom.Ellipse2D; import javax.swing.* / * Gobang-chessboard class * / public class ChessBoard extends JPanel implements MouseListener {public static final int MARGIN=30;// edge distance public static final int GRID_SPAN=35;// grid spacing public static final int ROWS=15;// chessboard rows number of public static final int COLS=15;// chessboard rows Point [] chessList=new Point [(ROWS+1) * (COLS+1)]; / / each array element is null boolean isBlack=true;// and starts with black chess first by default / / whether the game ends int chessCount;// the number of pieces on the current board int xIndex,yIndex;// the index of the current newly played pieces Image img; Image shadows; Color colortemp; public ChessBoard () {/ / setBackground (Color.blue); / / sets the background color to orange img=Toolkit.getDefaultToolkit () .getImage ("board.jpg"); shadows=Toolkit.getDefaultToolkit () .getImage ("shadows.jpg"); addMouseListener (this) AddMouseMotionListener (new MouseMotionListener () {public void mouseDragged (MouseEvent e) {} public void mouseMoved (MouseEvent e) {int x1 = (e.getX ()-MARGIN+GRID_SPAN/2) / GRID_SPAN / / change the coordinate position of the mouse click to the grid index int y1 = (e.getY ()-MARGIN+GRID_SPAN/2) / GRID_SPAN / / the game is over and cannot be played / / cannot be played outside the chessboard / / there is already a chess piece in the XMagi y position and cannot be played under if (x1ROWS | | y1COLS | | gameOver | | findChess (x1Magne y1)) setCursor (new Cursor (Cursor.DEFAULT_CURSOR)); / / set to the default state else setCursor (new Cursor (Cursor.HAND_CURSOR)) }});} / draw public void paintComponent (Graphics g) {super.paintComponent (g); / / draw chessboard int imgWidth= img.getWidth (this); int imgHeight=img.getHeight (this); / / get the width and height of the picture int FWidth=getWidth (); int FHeight=getHeight () / / get the width and height of the window int x = (FWidth-imgWidth) / 2; int y = (FHeight-imgHeight) / 2; g.drawImage (img, x, y, null); for / / continue the search in another case: oblique / / Northwestern search for for (int xonomxIndexPak 1) continueCount++; else break (getChess (xmemy)! = null) continueCount++; else break } / / looking for for in the southeast (int xindexIndexpositions 1 Magi yindexIndexpositx

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report