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

📄 cyberthelloframe.java

📁 JCreator下开发
💻 JAVA
字号:
//整体框架
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JOptionPane;
 

public class CyberthelloFrame extends JFrame
	implements ActionListener
{
	private static final Color bgColor = Color.black;
	private static final Color textFgColor = Color.white;
	private JPanel mainPanel;
	private GridBagLayout mainLayout;
	private JLabel gameInProgressLabel;
	private JLabel blackGraphic;
	private JLabel blackLabel;
	private JLabel blackScoreLabel;
	private JLabel whiteGraphic;
	private JLabel whiteLabel;
	private JLabel whiteScoreLabel;
	private ImageIcon user;
	private ImageIcon AI;
	private CyberthelloPane cyberthelloPane;
	private JMenuItem fileNewGame_MenuItem;
	private JMenuItem fileExit_MenuItem;
	private OthelloGame currentGame;
	public CyberthelloFrame()
	{
		currentGame = null;
		setSize(488, 590);
		buildMenu();
		buildUI();
		addWindowListener(
			new WindowAdapter()
			{	public void windowClosing(WindowEvent e)
				{
					fileExit();
				}
			});//注册窗口时间监听器
		refreshUI();//刷新界面
	}
	private void buildMenu()//菜单栏,有“开始新游戏”和“退出”两个选项
	{
		JMenuBar menuBar = new JMenuBar();
		JMenu fileMenu = new JMenu("游戏");
		fileNewGame_MenuItem = new JMenuItem("开始新游戏");
		fileNewGame_MenuItem.addActionListener(this);
		fileMenu.add(fileNewGame_MenuItem);
		fileExit_MenuItem = new JMenuItem("退出");
		fileExit_MenuItem.addActionListener(this);
		fileMenu.add(fileExit_MenuItem);
		menuBar.add(fileMenu);
		setJMenuBar(menuBar);
	}
	private void buildUI()//用户界面布局
	{
		mainPanel = new JPanel();
		mainPanel.setBackground(bgColor);
		setContentPane(mainPanel);
		mainLayout = new GridBagLayout();
		mainPanel.setLayout(mainLayout);	
		gameInProgressLabel = new JLabel("Blah blah");
		gameInProgressLabel.setForeground(textFgColor);
		mainPanel.add(gameInProgressLabel);
		mainLayout.setConstraints(
			gameInProgressLabel,
			new GridBagConstraints(
				0, 0, 3, 1, 0.0, 0.0,
				GridBagConstraints.CENTER, GridBagConstraints.NONE,
				new Insets(5, 5, 5, 5), 0, 0));
		user = new ImageIcon("D:\\ntshrui.ico");
		AI = new ImageIcon("D:\\shell.ico");
		blackGraphic = new JLabel("");
		blackGraphic.setForeground(textFgColor);
		mainPanel.add(blackGraphic);
		mainLayout.setConstraints(
			blackGraphic,
			new GridBagConstraints(
				0, 1, 1, 1, 1.0, 0.0,
				GridBagConstraints.EAST, GridBagConstraints.NONE,
				new Insets(10, 5, 2, 5), 0, 0));
		blackLabel = new JLabel("黑方: ");
		blackLabel.setForeground(textFgColor);
		mainPanel.add(blackLabel);
		mainLayout.setConstraints(
			blackLabel,
			new GridBagConstraints(
				0, 1, 1, 1, 1.0, 0.0,
				GridBagConstraints.CENTER, GridBagConstraints.NONE,
				new Insets(10, 5, 2, 5), 0, 0));
		blackScoreLabel = new JLabel("2");
		blackScoreLabel.setForeground(textFgColor);
		mainPanel.add(blackScoreLabel);
		mainLayout.setConstraints(
			blackScoreLabel,
			new GridBagConstraints(
				1, 1, 1, 1, 1.0, 0.0,
				GridBagConstraints.WEST, GridBagConstraints.NONE,
				new Insets(10, 5, 2, 5), 0, 0));
		whiteGraphic = new JLabel("");
		whiteGraphic.setForeground(textFgColor);
		mainPanel.add(whiteGraphic);
		mainLayout.setConstraints(
			whiteGraphic,
			new GridBagConstraints(
				0, 2, 1, 1, 0.0, 0.0,
				GridBagConstraints.EAST, GridBagConstraints.NONE,
				new Insets(2, 5, 5, 5), 0, 0));
		whiteLabel = new JLabel("白方: ");
		whiteLabel.setForeground(textFgColor);
		mainPanel.add(whiteLabel);
		mainLayout.setConstraints(
			whiteLabel,
			new GridBagConstraints(
				0, 2, 1, 1, 0.0, 0.0,
				GridBagConstraints.CENTER, GridBagConstraints.NONE,
				new Insets(2, 5, 5, 5), 0, 0));
		whiteScoreLabel = new JLabel("2");
		whiteScoreLabel.setForeground(textFgColor);
		mainPanel.add(whiteScoreLabel);
		mainLayout.setConstraints(
			whiteScoreLabel,
			new GridBagConstraints(
				1, 2, 1, 1, 0.0, 0.0,
				GridBagConstraints.WEST, GridBagConstraints.NONE,
				new Insets(2, 5, 5, 5), 0, 0));
		cyberthelloPane = new CyberthelloPane(this);
		mainPanel.add(cyberthelloPane);
		mainLayout.setConstraints(
			cyberthelloPane,
			new GridBagConstraints(
				0, 3, 3, 1, 1.0, 1.0,
				GridBagConstraints.CENTER, GridBagConstraints.BOTH,
				new Insets(10, 10, 10, 10), 0, 0));
	}
	public void refreshUI()//刷新界面
	{
		cyberthelloPane.repaint();
		if (currentGame == null)//没有游戏在进行
		{
			gameInProgressLabel.setText("游戏尚未开始");
			blackLabel.setText("");
			blackScoreLabel.setText("");
			whiteLabel.setText("");
			whiteScoreLabel.setText("");
		}
		else//游戏进行中
		{
			gameInProgressLabel.setText("游戏中");
			if (currentGame.isGameOver())//游戏结束
			{
				gameInProgressLabel.setText("游戏结束");
				if(currentGame.blackWin()&&currentGame.blackIsHuman())
					JOptionPane.showMessageDialog(null,"游戏结束,玩家胜","游戏结果",JOptionPane.INFORMATION_MESSAGE);
				else if(currentGame.blackWin()&&!currentGame.blackIsHuman())
					JOptionPane.showMessageDialog(null,"游戏结束,电脑胜","游戏结果",JOptionPane.INFORMATION_MESSAGE);
				else if(currentGame.whiteWin()&&currentGame.whiteIsHuman())
					JOptionPane.showMessageDialog(null,"游戏结束,玩家胜","游戏结果",JOptionPane.INFORMATION_MESSAGE);
				else if(currentGame.whiteWin()&&!currentGame.whiteIsHuman())
					JOptionPane.showMessageDialog(null,"游戏结束,电脑胜","游戏结果",JOptionPane.INFORMATION_MESSAGE);
			}
			//下面设置黑子
			if (currentGame.blackIsHuman())//黑方是玩家
			{
				blackLabel.setText("玩家执黑子: ");
				blackGraphic.setIcon(user);
			}
			else//黑方是电脑
			{
				blackLabel.setText("电脑执黑子: ");
				blackGraphic.setIcon(AI);
			}
			blackScoreLabel.setText(""+currentGame.getBlackScore());//获取当前棋盘上黑子的个数
			//下面设置白子
			if (currentGame.whiteIsHuman())//白方是玩家
			{
				whiteLabel.setText("玩家执白子: ");
				whiteGraphic.setIcon(user);
			}
			else//白方是电脑
			{
				whiteLabel.setText("电脑执白子: ");
				whiteGraphic.setIcon(AI);
			}
			whiteScoreLabel.setText(String.valueOf(currentGame.getWhiteScore()));//获取当前棋盘上白子的个数
		}
	}
	private synchronized void fileNewGame()//新游戏多进程
	{
		CyberthelloNewGameDialog d = new CyberthelloNewGameDialog(this);
		d.setLocationRelativeTo(this);
		d.setVisible(true);
		if (d.okPressed())//OK
		{
		    if (currentGame != null)//当前有游戏正在进行,那么先结束当前游戏
		    {
				currentGame.endGame();
				notify();  // 唤醒等待进程
		    }
			currentGame = new OthelloGame(
			        d.blackIsHuman(),
			        d.whiteIsHuman(),
					this);//定义选项:玩家执黑子或者玩家执白子
			cyberthelloPane.setGame(currentGame);
			refreshUI();
			currentGame.start();
		}
	}
	private void fileExit()//退出游戏
	{
		System.exit(0);
	}

	public synchronized void makeMove(int row, int col)//走棋
	{
		if (currentGame == null)//当前没有游戏,那么直接跳出
		    return;
		if (currentGame.isBlacksMove())//当前游戏进行到轮到黑方走棋
		{
			if (!currentGame.blackIsHuman())//黑方不是玩家
				return;//直接跳出
			boolean legalMove = currentGame.makeHumanMove(row, col, Cyberthello.BLACK);//黑方是玩家,那么判断当前黑方是否有路可走
			if (!legalMove)//无路可走,直接跳出
				return;
		}
		else//当前游戏进行到轮到白方走棋,处理方式同上
		{
			if (!currentGame.whiteIsHuman())
				return;
			boolean legalMove = currentGame.makeHumanMove(row, col, Cyberthello.WHITE);
			if (!legalMove)
				return;
		}
		notify();
		refreshUI();
	}
	public void actionPerformed(ActionEvent e)//选择“游戏”菜单里的选项所做的相应的反应
	{
		Object obj = e.getSource();
		if (obj == fileNewGame_MenuItem)
		{
			fileNewGame();
		}
		else if (obj == fileExit_MenuItem)
		{
			fileExit();
		}
	}
}

⌨️ 快捷键说明

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