编写一个围棋对弈程序

修改版的,窗口大小改变了,思路虽然一样,但是所有数据都得思考理解后才能写的出来,棋子类的颜色判断通过传进去的Pad类的txtfield来判断的,其他大的结构还是原版的,原版的棋子类传入Pad类,方便后面用Pad.remove(this)移除;还有Pad类的通过设置棋子类的位置大小来下棋也很新颖~

import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

/*编写一个围棋程序*/

public class Exp6_4 extends JFrame {
	
	JLabel jLabel = new JLabel("操作方法:单击左键下棋子,双击吃棋子,用右键单击棋子悔棋",JLabel.CENTER);
	Pad pad = new Pad();
	
	public Exp6_4() {
		setTitle("围棋对弈程序");
		setSize(806, 935);
		setVisible(true);
		setResizable(false);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setLayout(null);
		
		jLabel.setBounds(0,0,800, 45);
		jLabel.setOpaque(true);
		jLabel.setBackground(new Color(69, 137, 148));
		jLabel.setForeground(Color.YELLOW);
		add(jLabel);
		
		pad.setBounds(0,45,800,855);
		add(pad);		
		
		validate();
	}
	
	public static void main(String[] args) {
		new Exp6_4();
	}

}

class Pad extends JPanel implements ActionListener,MouseListener{//下方的画布
	JButton restart = new JButton("重新开始");
	JTextField textField1 = new JTextField(10);
	JTextField textField2 = new JTextField(10);
	JPanel jPanel = new JPanel();
	int x,y;//规格化坐标
	int ex,ey;

	public Pad() {
		// TODO Auto-generated constructor stub
		this.setBackground(new Color(137, 157, 192));
		this.setLayout(null);
		jPanel.setBounds(0,10,800,45);
		jPanel.add(restart);
		jPanel.add(textField1);
		jPanel.add(textField2);
		textField1.setEditable(false);
		textField2.setEditable(false);
		jPanel.setBackground(new Color(250,227,113));
		this.add(jPanel);
		
		textField1.setText("请黑棋下子");textField2.setText("");
		
		restart.addActionListener(this);
		this.addMouseListener(this);
		
		validate();
		
	}
	
	@Override
	public void paint(Graphics g) {
		// TODO Auto-generated method stub
		super.paint(g);
		int num=0;
		g.setColor(Color.cyan);
		for (int i = 0; i <= 18; i++) {
			g.drawLine(40, 95+40*i, 800-40, 95+40*i);
			g.drawLine(40+40*i, 95, 40+40*i, 855-40);
			
		}
		g.setColor(Color.BLUE);
		for (int i = 0; i <= 18; i++) {
			for (int j = 0; j <= 18; j++) {
				g.fillOval(120+40+240*i-4, 120+95+240*j-4, 8, 8);
			}
		}
		
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		if (e.getSource()==restart) {
			textField1.setText("请黑棋下子");
			textField2.setText("");
			System.out.println("jBtn:Restart");
			this.removeAll();
			this.add(jPanel);
		}
	}

	@Override
	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub
		if (e.getModifiers() == InputEvent.BUTTON1_MASK) {//鼠标左击
			System.out.println("Pad.mousePressed(MouseEvent e)");
			x = (int)e.getX();y = (int)e.getY();
			if (x<20||x>780||y>855-20||y<75) {
				;
			}else if ((ex!=((x+20)/40*40-10))||((ey!=(y-55+20)/40*40+55-10))) {
				Exp6_4_ChessPoint chessPoint = new Exp6_4_ChessPoint(this);
				chessPoint.setBounds((x+20)/40*40-10, (y-55+20)/40*40+55-10, 20, 20);
				System.out.println("((int)e.getX()),((int)e.getY())\t"+(x)+","+(y));
				System.out.println("((int)e.getX()+20)/40*40-10, ((int)e.getY())/40*40-10\t"+((x+20)/40*40-10)+","+((y-55+20)/40*40+55-10));
				ex=(x+20)/40*40-10;ey=(y-55+20)/40*40+55-10;
				this.add(chessPoint);
			}
		}
			
		
	}
	public void mouseReleased(MouseEvent e) {	}
	public void mouseClicked(MouseEvent e) 	{	}
	public void mouseEntered(MouseEvent e) 	{	}
	public void mouseExited(MouseEvent e) 	{	}
}

class Exp6_4_ChessPoint extends Canvas implements MouseListener {
	
	Pad  chessPad;
	Color color = null;
	
	public Exp6_4_ChessPoint(Pad pad) {
		// TODO Auto-generated constructor stub
		this.addMouseListener(this);
		chessPad = pad;
		check(chessPad);
		
	}
	
	public void check(Pad pad) {//检查该用什么下棋
		if (pad.textField2.getText().equals("")) {
			System.out.println("Black");
			color = Color.BLACK;
			pad.textField1.setText("");
			pad.textField2.setText("请白棋下子");
		}else if (pad.textField1.getText().equals("")) {
			System.out.println("White");
			color = Color.WHITE;		
			pad.textField1.setText("请黑棋下子");
			pad.textField2.setText("");
		}
	}
	
	@Override
	public void paint(Graphics g) {
		// TODO Auto-generated method stub
		g.setColor(color);System.out.println("paint(Graphics g)"+color.toString());
		g.fillOval(0,0,20,20);
	}
	public void mouseClicked(MouseEvent e) 	{
		 if (e.getClickCount() == 2) {
		      System.out.println("Doublc Clicked!");
		      chessPad.remove(this);
		    }
	}
	
	public void mousePressed(MouseEvent e) 	{
		if (e.getModifiers() == InputEvent.BUTTON3_MASK) {//鼠标右击事件
			System.out.println("Regret Event!");
			chessPad.remove(this);
			check(chessPad);
		}
	}
	
	public void mouseEntered(MouseEvent e) 	{	}
	public void mouseExited(MouseEvent e) 	{	}
	public void mouseReleased(MouseEvent e) {	}
	
	
}

猜你喜欢

转载自blog.csdn.net/qq_38179971/article/details/80152888