首页 > 代码库 > 进击的五子棋第一季

进击的五子棋第一季

RunGmae类

1 package com.lovo;2 3 public class GameRun {4     public static void main(String[] args) {5         new MyFrame().setVisible(true);6     }7 }

 

MyFrame类

 1 package com.lovo; 2  3 import java.awt.Color; 4 import java.awt.Graphics; 5 import java.awt.Image; 6 import java.awt.event.ActionEvent; 7 import java.awt.event.ActionListener; 8 import java.awt.image.BufferedImage; 9 10 import javax.swing.JFrame;11 import javax.swing.Timer;12 13 public class MyFrame extends JFrame{14     private static final long serialVersionUID = -3544738451252364271L;15     private Board board = new Board();16     //在内存中创建一张图17     private Image offImage = new BufferedImage(800, 800, BufferedImage.TYPE_INT_RGB);18     public MyFrame(){19         this.setTitle("五子棋");20         this.setSize(800, 800);21         this.setResizable(false);22         this.getContentPane().setBackground(new Color(197, 194, 129));//设置窗口背景23         this.setDefaultCloseOperation(EXIT_ON_CLOSE);24         this.setLocationRelativeTo(null);    //窗口居中25         Timer timer =new Timer(100, new ActionListener() {26             @Override27             public void actionPerformed(ActionEvent e) {28                 board.makeAMove();29                 repaint();//通知系统重新调用paint()30             }31         });32         timer.start();33     }34 35     @Override36     public void paint(Graphics g) {//操作系统调用该方法绘图37         Graphics newG = offImage.getGraphics();//得到内存中图片的画笔38         super.paint(newG);                //有才能设置背景,在内存中画出窗口39         board.draw(newG);                //在内存中画出棋盘40         g.drawImage(offImage, 0, 0, 800, 800,null);//把内存里的棋盘放到屏幕上41     }42 }

board类

 1 package com.lovo; 2  3 import java.awt.BasicStroke; 4 import java.awt.Color; 5 import java.awt.Graphics; 6 import java.awt.Graphics2D; 7 import java.awt.Stroke; 8  9 public class Board {10     private int[][] b = new int[15][15];11     private boolean blackTurn = true;12 /**13  * 画棋盘14  * @param g15  */16     public void draw(Graphics g) {17         g.setColor(Color.BLACK);    //加了双缓冲后默认是白色,设置为黑色18         Graphics2D g2d = (Graphics2D) g;    //用Graphics2D可以调粗细19         Stroke oldsStroke = g2d.getStroke();//保存现场20         g2d.setStroke(new BasicStroke(3));    //设置画笔粗细21         g.drawRect(50, 50, 700, 700);    //矩形框22         g2d.setStroke(oldsStroke);        //回复现场23         for (int i = 0; i < 13; i++) {24             g.drawLine(50, 100 + i * 50, 750, 100 + i * 50);25             g.drawLine(100 + i * 50, 50, 100 + i * 50, 750);26         }27         //画天元和三三点28         g.fillOval(390, 390, 20, 20);//实心椭圆29         g.fillOval(195, 195, 10, 10);30         g.fillOval(595, 195, 10, 10);31         g.fillOval(595, 595, 10, 10);32         g.fillOval(195, 595, 10, 10);33         for (int i = 0; i < b.length; i++) {//行的循环,控制纵坐标34             for (int j = 0; j < b[i].length; j++) {//列的循环,控制横坐标35                 if (b[i][j] != 0) {36                     g.setColor(b[i][j] == 1 ? Color.BLACK : Color.WHITE);37                     g.fillOval(25 + 50 * j, 25 + 50 * i, 50, 50);38                 }39             }40         }41     }42     public void makeAMove() {43         int row = (int) (Math.random() * 15);44         int col = (int) (Math.random() * 15);45         if (b[row][col] == 0) {46             b[row][col] = blackTurn ? 1 : 2;//0表示没有走棋,1表示黑棋,2表示白棋47             blackTurn = !blackTurn;            //交换走棋48         }49         50     }51 }

 

进击的五子棋第一季