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

📄 cyberthellopane.java

📁 JCreator下开发
💻 JAVA
字号:
//扩展JPanel类,实现游戏面板

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.*;
import java.awt.image.*; 
import javax.imageio.*;

public class CyberthelloPane extends JPanel
{
	private static final Color bgColor = new Color(0, 128, 0);//背景颜色
	private OthelloGame game;
	private CyberthelloFrame parentFrame;
	private int usableHeight;//可用高度,即棋盘的高度
	private int usableWidth;//可用宽度,即棋盘的宽度
	private int boardLeftOffset;//左边距
	private int boardTopOffset;//上边距
	private int squareHeight;//单个小格子的高度
	private int squareWidth;//单个小格子的宽度
	public CyberthelloPane(CyberthelloFrame cf)
	{
		super();
		game = null;
		setBackground(bgColor);
		parentFrame = cf;
		addMouseListener(new CyberthelloPaneMouseListener());//添加监听器
	}
	public void setGame(OthelloGame game)
	{
		this.game = game;
		repaint();
	}
	private void recalcSizes()
	{
		Dimension d = getSize();//面板的尺寸
		usableHeight = d.height - (d.height % 8) - 16;//可用高度,面板的尺寸减去边距
		usableWidth = d.width - (d.width % 8) - 16;
		boardLeftOffset = 8;//左边距
		boardTopOffset = 8;//上边距
		squareHeight = usableHeight / 8;//每个格子的大小
		squareWidth = usableWidth / 8;
	}
	public void paint(Graphics g)//画棋子
	{
		super.paint(g);
		Graphics2D g2 = (Graphics2D) g;
		if (game == null)//当前没有游戏,当然什么都不要做
			;
		else
		{
			recalcSizes();//求得格子的大小
			//下面画棋盘的格子
			g2.setPaint(Color.black);
			for (int i = 0; i <= 8; ++i)
			{
				g2.draw(
					new Line2D.Double(
						boardLeftOffset, boardTopOffset + (i * squareHeight),
						boardLeftOffset + (8 * squareWidth), boardTopOffset + (i * squareHeight)));
						
				g2.draw(
					new Line2D.Double(
						boardLeftOffset + (i * squareWidth), boardTopOffset,
						boardLeftOffset + (i * squareWidth), boardTopOffset + (8 * squareHeight)));
			}
			//下面画棋子,即正方形的内切圆		
			for (int row = 0; row < 8; ++row)
			{
				for (int col = 0; col < 8; ++col)
				{
					int squareContents = game.getSquareContents(row, col);
					if (squareContents != Cyberthello.EMPTY)
					{
						if (squareContents == Cyberthello.BLACK)
						{
							g2.setPaint(Color.black);//黑子
						}
						else if (squareContents == Cyberthello.WHITE)
						{
							g2.setPaint(Color.white);//白子
						}

						int ellipseWidth = (squareWidth / 5) * 4;
						int ellipseHeight = (squareHeight / 5) * 4;
						int ellipseWidthOffset = (squareWidth - ellipseWidth) / 2;
						int ellipseHeightOffset = (squareHeight - ellipseHeight) / 2;
						g2.fill(
							new Ellipse2D.Double(
								boardLeftOffset + (col * squareWidth) + ellipseWidthOffset, 
								boardTopOffset + (row * squareHeight) + ellipseHeightOffset,
								ellipseWidth, ellipseHeight));//填色
					}
				}
			}
		}
	}
	private int xCoordToCol(int xCoord)//计算这个坐标落在横向数的第几个格子
	{
		if (xCoord < boardLeftOffset)
		{
			return -1;
		}		
		xCoord -= boardLeftOffset;
		
		if (xCoord > squareWidth * 8)
		{
			return -1;
		}	
		return xCoord / squareWidth;
	}	
	private int yCoordToRow(int yCoord)//计算这个坐标落在纵向数的第几个格子
	{
		if (yCoord < boardTopOffset)
		{
			return -1;
		}		
		yCoord -= boardTopOffset;
		
		if (yCoord > squareHeight * 8)
		{
			return -1;
		}		
		return yCoord / squareHeight;
	}
	public class CyberthelloPaneMouseListener extends MouseAdapter//鼠标监听
	{
		private int pressedCol, pressedRow;
		public void mousePressed(MouseEvent e)//响应鼠标的点击,并且计算鼠标点击的是哪个格子
		{
			recalcSizes();
			pressedCol = xCoordToCol(e.getX());
			pressedRow = yCoordToRow(e.getY());
		}
		public void mouseReleased(MouseEvent e)//响应鼠标释放
		{
			int releasedX = e.getX();//鼠标释放的位置
			int releasedY = e.getY();		
			recalcSizes();
			int releasedCol = xCoordToCol(releasedX);//计算鼠标释放在哪个格子
			int releasedRow = yCoordToRow(releasedY);
		    if (pressedCol == releasedCol && pressedRow == releasedRow)//点击和释放发生在同一个格子内
			{
				if (releasedCol != -1 && releasedRow != -1)//位置有效
				{
					parentFrame.makeMove(releasedRow, releasedCol);//在该位置下棋
					repaint();//画个棋子
				}
			}
			pressedRow = pressedCol = -1; 
		}
	}
}

⌨️ 快捷键说明

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