⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gamemain.java

📁 java编写的五子棋源代码具有很好的学习价值,对与五子棋的游戏开发
💻 JAVA
字号:
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.imageio.*;
import java.util.*;
import java.net.*;
import java.io.*;
import java.awt.Rectangle;

public class GameMain {
 public static void main(String[] args) throws Exception {
   new Gui();
 }
}

class SocketThread extends Thread {
 private ObjectOutputStream out = null;

 private Socket socket = null;

 private ServerSocket server = null;

 private ObjectInputStream in = null;

 private UserInfo link_user = null;

 private Chessboard board = null;

 public SocketThread(Chessboard board) throws Exception {
   this.board = board;
 }

 private void InitThread() throws Exception {
   this.setDaemon(true);
 }

 public void run() {
   try {
     server = new ServerSocket(9999);
     socket = server.accept();
     in = new ObjectInputStream(new BufferedInputStream(socket
         .getInputStream()));
     link_user = new UserInfo(in.readUTF(), (Color) in.readObject());
     while (true) {
       String x = in.readUTF();
       String y = in.readUTF();
       if (!x.equals("") && !y.equals("")) {
         board
             .drawChessman(Integer.parseInt(x), Integer
                 .parseInt(y));
       }
     }
   } catch (Exception ex) {
   }

 }
}

class UserInfo {
 private String name = "";

 private Color color = null;

 public UserInfo(String name, Color color) {
   this.name = name;
   this.color = color;
 }

 public String getName() {
   return name;
 }

 public Color getColor() {
   return color;
 }

 public void setName(String name) {
   this.name = name;
 }

 public void setColor(Color color) {
   this.color = color;
 }
}

class LinkServer extends Thread {
 public LinkServer() throws Exception {

 }

 public void run() {

 }
}

class Gui extends JFrame {
 private SocketThread socket_thread = null;

 private LinkServer link_server = null;

 private Chessboard board = null;

 public Gui() throws Exception {
   this.InitGui();
   this.setSize(1024, 768);
   this.setUndecorated(true);
   this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
   this.setResizable(false);
   this.setJMenuBar(new ShowMenuBar(board));
   this.setVisible(true);
 }

 private void InitGui() throws Exception {
   this.getContentPane().add(board = new Chessboard());
 }
}

class ShowMenuBar extends JMenuBar {
 private Chessboard board;

 public ShowMenuBar(Chessboard board) {
   this.board = board;
 }
}

class Chessboard extends JPanel {
 private Graphics2D g2d = null;

 private BufferedImage chessboard = null;

 private int COORDINATE = 0;

 private final int SIZE = 684;

 private final int NUM = 18;

 private final int boardx = 170;

 private final int boardy = 50;

 private int grid = 0;

 private Color color = null;

 private Color color_1 = null;

 private int[][] manmap = new int[19][19];

 private boolean isGo = false;

 private Eatman eat = null;

 public Chessboard() throws Exception {
   this.InitChessboard();
   MouseAction action = new MouseAction(this);
   eat = new Eatman();
   this.addMouseListener(action);
   this.addMouseMotionListener(action);

 }

 public void paintComponent(Graphics g) {
   super.paintComponent(g);
   if (this.chessboard != null)
     g.drawImage(chessboard, boardx, boardy, null);
 }

 private void InitChessboard() throws Exception {
   chessboard = new BufferedImage(this.SIZE + 1, this.SIZE + 1,
       BufferedImage.TYPE_INT_BGR);
   g2d = (Graphics2D) chessboard.getGraphics();
   this.drawBoard(g2d);
   this.repaint();
 }

 public void drawChessman(int mx, int my) {
   if (this.chessboard != null) {
     for (int x = 0; x < 19; x++) {
       for (int y = 0; y < 19; y++) {
         Rectangle rec = new Rectangle(grid * x - grid / 2 + boardx,
             grid * y - grid / 2 + boardy, grid, grid);
         if (rec.contains(mx, my)) {
           if (manmap[x][y] == 0) {
             if (isGo) {
               g2d.setColor(Color.white);
               manmap[x][y] = 1;
               isGo = false;
             } else {
               g2d.setColor(Color.black);
               manmap[x][y] = 2;
               isGo = true;
             }
             if (eat.EatmanClick(manmap)) {
               JOptionPane.showMessageDialog(null, "有人挂了");
             }
             g2d.fillOval(grid * x - grid / 2, grid * y - grid
                 / 2, grid - 2, grid - 2);
             this.repaint();
           }
         }
       }
     }
   }
 }

 private void drawBoard(Graphics2D g2d) {
   g2d.setColor(new Color(239, 182, 99));
   g2d.fillRect(0, 0, this.SIZE, this.SIZE);
   g2d.setColor(Color.black);
   g2d.drawRect(0, 0, this.SIZE, this.SIZE);
   grid = this.SIZE / this.NUM;
   int temp = 0;
   for (int i = 0; i < this.NUM; i++) {
     temp = grid * i;
     g2d.drawLine(temp, 0, temp, this.SIZE);
     g2d.drawLine(0, temp, SIZE, temp);
     if (i == 3 || i == 9 || i == 15) {
       g2d.fillRect(temp - 5, grid * 3 - 5, 10, 10);
       g2d.fillRect(temp - 5, grid * 9 - 5, 10, 10);
       g2d.fillRect(temp - 5, grid * 15 - 5, 10, 10);
     }
   }
 }

 public int getSIZE() {
   return this.SIZE;
 }

 public int getBoardX() {
   return this.boardx;
 }

 public int getBoardY() {
   return this.boardy;
 }
}

class MouseAction extends MouseAdapter implements MouseMotionListener {
 private Chessboard chessboard = null;

 private int color = 1;

 public MouseAction(Chessboard chessboard) {
   this.chessboard = chessboard;
 }

 public void mouseDragged(MouseEvent e) {

 }

 public void mouseMoved(MouseEvent e) {

 }

 public void mouseClicked(MouseEvent e) {
   Rectangle rec = new Rectangle(chessboard.getBoardX(), chessboard
       .getBoardY(), chessboard.getSIZE(), chessboard.getSIZE());
   if (rec.contains(e.getX(), e.getY())) {
     chessboard.drawChessman(e.getX(), e.getY());
   }
 }
}

class Eatman {
 private int[][] board;

 private int num = 1;

 private int jx = 0;

 private int jy = 0;

 public Eatman() {
 }

 public boolean EatmanClick(int[][] board) {
   this.board = board;
   for (int x = 0; x < board.length; x++) {
     for (int y = 0; y < board[x].length; y++) {
       if (board[x][y] != 0) {
         jx = x;
         jy = y;
         this.manAction(x, y, 1);
         if (num == 5)
           return true;
       }

     }
   }
   return false;
 }

 private void manAction(int x, int y, int act) {
   if (num != 5) {
     if (act == 1) {
       if (board[x][y] == board[x + 1][y]) {
         num++;
         this.manAction(x + 1, y, 1);
       } else {
         num = 1;
         this.manAction(jx, jy, 2);
       }
     } else if (act == 2) {
       if (board[x][y] == board[x][y + 1]) {
         num++;
         this.manAction(x, y + 1, 2);
       } else {
         num = 1;
         this.manAction(jx, jy, 3);
       }
     } else if (act == 3) {
       if (board[x][y] == board[x + 1][y + 1]) {
         num++;
         this.manAction(x + 1, y + 1, 3);
       } else {
         num = 1;
         this.manAction(jx, jy, 4);
       }
     } else if (act == 4) {
       if (board[x][y] == board[x - 1][y + 1]) {
         num++;
         this.manAction(x - 1, y + 1, 4);
       } else {
         num = 1;
       }
     }
   }
 }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -