• 功能还不全的五子棋,现在只实现了走棋


    只是基础知识的应用,棋的功能还在完善中.......

    代码如下:

    package game;
    
    import java.awt.Graphics;
    /**
     * 是否在窗口画图
     * @author mt
     *
     */
    public interface Drawable {
    	public abstract void draw(Graphics g);
    }
    
    
    package game;
    
    import java.awt.Color;
    import java.awt.Graphics;
    
    public class Circle implements Drawable {
    	private final int SIZE =  15;  //用于决定圆的大小
    	private int x,y;   //起点坐标
    	private Color color;  //颜色
    	/**
    	 * 构造器
    	 * @param x 起点x坐标
    	 * @param y  起点y坐标
    	 */
    	public Circle(int x, int y) {
    		this.x = x;
    		this.y = y;
    	}
    	/**
    	 * 设置颜色
    	 * @param color  颜色
    	 */
    	public void setColor(Color color) {
    		this.color = color;
    	}
    /**
     * 画圆的方法
     */
    	public void draw(Graphics g) {
    		g.setColor(color);
    		g.fillOval(x - SIZE, y - SIZE,  SIZE * 2,  SIZE * 2);
    	}
    
    }
    
    
    package game;
    /**
     * 点类
     * @author mt
     *
     */
    public class Dot {
    	private int x;        //点的x坐标
    	private int y;       //点的y坐标
    	private boolean isUsed;   //判断是否用过
    	/**
    	 * 构造器
    	 * @param x   点的x坐标
    	 * @param y   点的y坐标
    	 */
    	public Dot(int x, int y) {
    		this.x = x;
    		this.y = y;
    	}
    
    	public int getX() {
    		return x;
    	}
    	public void setX(int x) {
    		this.x = x;
    	}
    	public int getY() {
    		return y;
    	}
    	public void setY(int y) {
    		this.y = y;
    	}
    	public boolean isUsed() {
    		return isUsed;
    	}
    	public void setUsed(boolean isUsed) {
    		this.isUsed = isUsed;
    	}
    	
    	
    	
    }
    
    
    package game;
    
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.awt.image.BufferedImage;
    import java.util.ArrayList;
    import java.util.List;
    
    import javax.swing.JFrame;
    
    @SuppressWarnings("serial")
    public class MyJFrame extends JFrame {
    	private BufferedImage offImage = new BufferedImage(850, 850, 1);
    	private Circle chess;         //创建一个实心圆来代表棋子
    	private boolean isWhite = true; //用于判断是否走白棋 (默认白棋先走)
    	private boolean  isBlak = false;  //用于判断是否走黑棋
    	private Dot tempDot;     //用点的类创建一个临时记录用的点     (x,y)
    	private List<Dot> dot = new ArrayList<Dot>(); //创建一个装点的数组(就是棋盘上的所有线相交的255个点)
    	private List<Circle> list = new ArrayList<Circle>(); //创建一个装棋子的数组
    	public MyJFrame() {
    		this.setSize(850,850);  //设置窗口大小
    		this.setTitle("五 子 棋");  //窗口名字
    		this.setResizable(false); //设定窗口不能改变大小
    		this.setLocationRelativeTo(null);  //居中
    		this.setDefaultCloseOperation(EXIT_ON_CLOSE);  //关闭窗口是结束程序
    
    		for (int i = 0; i < 15; i++) {   //把每个255个点装进数组
    			for (int j = 0; j < 15; j++) {
    				dot.add(new Dot(50+50*i, 50 + 50*j));
    			}
    		}
    
    		this.addMouseListener(new MouseAdapter() {  //点击鼠标事件
    
    			@Override
    			public void mousePressed(MouseEvent e) {
    				int x = e.getX();    //获得点击鼠标处的x坐标
    				int y = e.getY();     //获得点击鼠标处的y坐标
    				for (Dot temp : dot) {  //让点击获得的点和255个点比较,在规定范围的内的都赋值为255个坐标里的点
    					if (temp.getX()-25 < x && x < temp.getX()+25) {
    						if(temp.getY()-25 <y&& y < temp.getY()+25) {
    						x = temp.getX();
    						y = temp.getY();
    						tempDot = temp;   //把符合条件的点赋值给临时记录点
    						}
    					}
    				}
    				 //判断该用黑色还是白色棋子以及是否被用过
    				if ( !tempDot.isUsed()) { 
    					chess = new Circle(x, y);
    					if (isWhite) {
    						chess.setColor(Color.WHITE);
    						isWhite = false;
    						isBlak = true;
    					} else if (isBlak) {
    						chess.setColor(Color.BLACK);
    						isBlak = false;
    						isWhite = true;
    					}
    				}
    				list.add(chess);  //把棋子装到数组
    				tempDot.setUsed(true); //设置棋子为用过
    				repaint();
    			}
    
    
    		});
    	}
    
    
    	@Override
    	public void paint(Graphics g) {
    		Graphics g3 = offImage.getGraphics();
    
    		super.paint(g3);
    		g3.setColor(Color.ORANGE);   //画一个橙色的填充矩形
    		g3.fillRect(30, 30, 800, 800);
    		g3.setColor(Color.BLACK);
    		g3.drawRect(30, 30, 800, 800); //画一个矩形(看起来像有边框)
    		for (int i = 0; i < 15; i++) {//画棋盘
    			g3.drawLine(50 , 50+ 50 * i , 750, 50+ 50 * i);
    			g3.drawLine(50+ 50 * i, 50 , 50+ 50 * i, 750);
    		}
    
    		for (Circle temp : list) {
    			temp.draw(g3);
    		}
    		if (chess != null) {
    			chess.draw(g3);
    		}
    
    		g.drawImage(offImage, 0, 0, null);
    	}
    }
    
    
    
    package game;
    
    public class Test {
    	public static void main(String[] args) {
    		new MyJFrame().setVisible(true);
    	}
    }
    
    

  • 相关阅读:
    button标签和input button
    获取select标签的值
    window.loaction和window.location.herf
    数组重复计数,对象方法
    js对象详解
    面试经典题型整理
    一些js小知识点整理
    事件委托能够优化js性能
    网页加载的一般顺序
    http状态码
  • 原文地址:https://www.cnblogs.com/mt1500/p/4498598.html
Copyright © 2020-2023  润新知