Java 1028 继承

package com.lovo;

/**
 * 棋盘类
 */

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Stroke;

public class Board {
	private int[][] b = new int[15][15];
	private boolean blackTurn = true; // 是否轮到黑方走棋

	public void draw(Graphics g) {
		g.setColor(Color.BLACK);
		
		Graphics2D g2d = (Graphics2D) g; // 画笔加粗需要引用的处理;
		Stroke oldStroke = g2d.getStroke(); // 记录画笔原来的粗细(保存现场)
		g2d.setStroke(new BasicStroke(5)); // 修改画笔的粗细
		g.drawRect(50, 50, 700, 700); // 绘制边框
		g2d.setStroke(oldStroke); // 将画笔还远为原来的粗细(恢复现场)

		for (int i = 0; i < 13; i++) {
			// g.drawOval(375, 375, 20, 20); //空心
			// g.fillOval(375, 375, 20, 20); //实心
			g.drawLine(50, 100 + 50 * i, 750, 100 + 50 * i);
			g.drawLine(100 + 50 * i, 50, 100 + 50 * i, 750);
		}
		
		/**
		 * 绘制天元
		 */
		g.fillOval(390, 390, 20, 20);

		/**
		 * 绘制四个星
		 */
		g.fillOval(195, 195, 10, 10);
		g.fillOval(595, 595, 10, 10);
		g.fillOval(195, 595, 10, 10);
		g.fillOval(595, 195, 10, 10);
		
		for(int i =0; i < b.length; i++){            //行控制纵坐标
			for(int j =0; j < b[i].length; j++){     //列控制横坐标
				if(b[i][j] != 0){
					g.setColor(b[i][j] == 1? Color.BLACK : Color.WHITE);
					g.fillOval(25 + 50 * j, 25 + 50 * i, 50, 50);
				}
			}
		}

	}

	public void makeAMove() {
		int row = (int) (Math.random() * 15);
		int col = (int) (Math.random() * 15);
		if (b[row][col] == 0) {
			b[row][col] = blackTurn ? 1 : 2;
			blackTurn = !blackTurn; // 交换走棋方
		}
	}

}


package com.lovo;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Stroke;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;

import javax.swing.JFrame;
import javax.swing.Timer;



public class MyFrame extends JFrame {
	private Board board = new Board();         //创建棋盘类的对象;
	private Image offImage = new BufferedImage(800, 800, BufferedImage.TYPE_INT_RGB);
	
	/**
	 * 无参构造器;
	 */
	public MyFrame(){	
		this.setTitle("五子棋");
		this.setSize(800, 800);
		this.setResizable(false);
		this.setLocationRelativeTo(null);                        //设置窗口剧中
		this.getContentPane().setBackground(new Color(0xff99cc));//加0x 颜色值不能缩写
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);            //关闭窗口时结束应用程序     
		
		Timer timer = new Timer(1000, new ActionListener() {    //swing
			
			@Override
			public void actionPerformed(ActionEvent e) {
				board.makeAMove();
				repaint();  //操作系统通知窗口重绘; 调用repaint方法!
			}
		});
		timer.start();
	}

	@Override
	public void paint(Graphics g) {         //重写此方法的目的是在窗口上绘制自己想要的东西
		Graphics newG = offImage.getGraphics();
		super.paint(newG);                     //消除窗口残影
		board.draw(newG);                      //棋盘对象调用draw方法;
		g.drawImage(offImage, 0, 0, 800, 800, null);
	}

}

package com.lovo;
/**
 * JFrame 继承;
 * @author 周博
 *
 */

public class GameRunner {
	public static void main(String[] args) {
//		MyFrame f = new MyFrame();
//		f.setVisible(true);
		new MyFrame().setVisible(true);   //简写
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值