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

📄 controlpanel.java

📁 经典的传教士过河问题的java swt实现
💻 JAVA
字号:
/**
 * File: ControlPanel.java
 * User: 吴永坚
 * Date: 2006.11.8
 * Describe: 俄罗斯方块的 Java 实现
 */
package Tetris;
import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.EtchedBorder;
import java.awt.*;
import java.awt.event.*;

/**
 * 控制面板类,继承自JPanel.
 * 上边安放预显窗口、等级、得分、控制按钮
 * 主要用来控制游戏进程。
 */
class ControlPanel extends JPanel {
	private JTextField
	        tfLevel = new JTextField("" + ErsBlocksGame.DEFAULT_LEVEL),
	        tfScore = new JTextField("0"),
	        tfScore1 = new JTextField("0"),
	        tfScore2 = new JTextField("0"),
	        tfScore3 = new JTextField("0"),
			tfScore4 = new JTextField("0");
	private JButton
	        btCplay = new JButton("电脑游戏"),
	        btHplay = new JButton("人工游戏"),
	        btPause = new JButton("暂停游戏"),
	        btResume= new JButton("恢复游戏"),
	        btStop = new JButton("结束游戏");

	private JPanel plTip = new JPanel(new BorderLayout());
	private TipPanel plTipBlock = new TipPanel();
	private JPanel plInfo = new JPanel(new GridLayout(4, 1));
	private JPanel plButton = new JPanel(new GridLayout(5, 1));

	private Timer timer;
	private ErsBlocksGame game;

	private Border border = new EtchedBorder(
	        EtchedBorder.RAISED, Color.white, new Color(148, 145, 140));

	/**
	 * 控制面板类的构造函数
	 * @param game ErsBlocksGame, ErsBoxesGame类的一个实例引用,
	 * 方便直接控制ErsBoxesGame类的行为。
	 */
	public ControlPanel(final ErsBlocksGame game) {
		setLayout(new GridLayout(3, 1, 3, 4));
		this.game = game;

		plTip.add(new JLabel("下一方块"), BorderLayout.NORTH);
		plTip.add(plTipBlock);
		plTip.setBorder(border);
		
		//plInfo.add(new JLabel("难度"));
		//plInfo.add(tfLevel);
		//plInfo.add(new JLabel("消除行数"));
		//plInfo.add(tfScore);
		
		plInfo.add(new JLabel("  4行"));
		plInfo.add(tfScore4);
		plInfo.add(new JLabel("  3行"));
		plInfo.add(tfScore3);
		plInfo.add(new JLabel("  2行"));
		plInfo.add(tfScore2);
		plInfo.add(new JLabel("  1行"));
		plInfo.add(tfScore1);
		plInfo.setBorder(border);

		tfLevel.setEditable(false);
		tfScore.setEditable(false);
		tfScore1.setEditable(false);
		tfScore2.setEditable(false);
		tfScore3.setEditable(false);
		tfScore4.setEditable(false);

		plButton.add(btCplay);
		plButton.add(btHplay);
		plButton.add(btPause);
		plButton.add(btResume);
		plButton.add(btStop);
		plButton.setBorder(border);

		add(plTip);
		add(plInfo);
		add(plButton);

		addKeyListener(new ControlKeyListener());

		btCplay.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ae) {
				game.computerPlayGame();
			}
		});
		btHplay.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ae) {
				game.humanPlayGame();
			}
		});
		btPause.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ae) {
				game.pauseGame();
			}
		});
		btResume.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ae) {
				game.resumeGame();
			}
		});
		btStop.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ae) {
				game.stopGame();
			}
		});

		addComponentListener(new ComponentAdapter() {
			public void componentResized(ComponentEvent ce) {
				plTipBlock.fanning();
			}
		});

		timer = new Timer(500, new ActionListener() {
			public void actionPerformed(ActionEvent ae) {
				tfScore1.setText("" + game.getDelLine1());
				tfScore2.setText("" + game.getDelLine2());
				tfScore3.setText("" + game.getDelLine3());
				tfScore4.setText("" + game.getDelLine4());
				int scoreForLevelUpdate =
				        game.getScoreForLevelUpdate();
				if (scoreForLevelUpdate >= ErsBlocksGame.PER_LEVEL_SCORE
				        && scoreForLevelUpdate > 0)
					game.levelUpdate();
			}
		});
		timer.start();
	}

	/**
	 * 设置预显窗口的样式,
	 * @param style int,对应ErsBlock类的STYLES中的28个值
	 */
	public void setTipStyle(int style) {
		plTipBlock.setStyle(style);
	}

	/**
	 * 取得用户设置的游戏等级。
	 * @return int, 难度等级,1 - ErsBlocksGame.MAX_LEVEL
	 */
	public int getLevel() {
		int level = 0;
		try {
			level = Integer.parseInt(tfLevel.getText());
		} catch (NumberFormatException e) {
		}
		return level;
	}

	/**
	 * 让用户修改游戏难度等级。
	 * @param level 修改后的游戏难度等级
	 */
	public void setLevel(int level) {
		if (level > 0 && level < 11) tfLevel.setText("" + level);
	}

	/**
	 * 设置"人工开始"按钮的状态。
	 */
	public void setHumanPlayButtonEnabled(boolean enable) {
		btHplay.setEnabled(enable);
	}
	
	/**
	 * 设置"游戏开始"按钮的状态。
	 */
	public void setComputerPlayButtonEnabled(boolean enable) {
		btCplay.setEnabled(enable);
	}
	
	/**
	 * 设置"游戏暂停"按钮的状态。
	 */
	public void setPauseButtonEnabled(boolean pause) {
		btPause.setEnabled(pause);
	}
	
	/**
	 * 设置"游戏恢复"按钮的状态。
	 */
	public void setResumeButtonEnabled(boolean resume){
		btResume.setEnabled(resume);
	}

	/**
	 * 重置控制面板
	 */
	public void reset() {
		tfScore.setText("0");
		plTipBlock.setStyle(0);
	}

	/**
	 * 重新计算TipPanel里的boxes[][]里的小框的大小
	 */
	public void fanning() {
		plTipBlock.fanning();
	}

	/**
	 * 预显窗口的实现细节类
	 */
	private class TipPanel extends JPanel {
		private Color backColor = Color.darkGray;
		private Color frontColor = Color.lightGray;
		private ErsBox[][] boxes =
		        new ErsBox[ErsBlock.BOXES_ROWS][ErsBlock.BOXES_COLS];

		private int style, boxWidth, boxHeight;
		private boolean isTiled = false;

		/**
		 * 预显窗口类构造函数
		 */
		public TipPanel() {
			for (int i = 0; i < boxes.length; i++) {
				for (int j = 0; j < boxes[i].length; j++)
					boxes[i][j] = new ErsBox(false);
			}
		}

		/**
		 * 预显窗口类构造函数
		 * @param backColor Color, 窗口的背景色
		 * @param frontColor Color, 窗口的前景色
		 */
		public TipPanel(Color backColor, Color frontColor) {
			this();
			this.backColor = backColor;
			this.frontColor = frontColor;
		}

		/**
		 * 设置预显窗口的方块样式
		 * @param style int,对应ErsBlock类的STYLES中的28个值
		 */
		public void setStyle(int style) {
			this.style = style;
			repaint();
		}

		/**
		 * 覆盖JComponent类的函数,画组件。
		 * @param g 图形设备环境
		 */
		public void paintComponent(Graphics g) {
			super.paintComponent(g);

			if (!isTiled) fanning();

			int key = 0x8000;
			for (int i = 0; i < boxes.length; i++) {
				for (int j = 0; j < boxes[i].length; j++) {
					Color color = (((key & style) != 0) ? frontColor : backColor);
					g.setColor(color);
					g.fill3DRect(j * boxWidth, i * boxHeight,
					        boxWidth, boxHeight, true);
					key >>= 1;
				}
			}
		}

		/**
		 * 根据窗口的大小,自动调整方格的尺寸
		 */
		public void fanning() {
			boxWidth = getSize().width / ErsBlock.BOXES_COLS;
			boxHeight = getSize().height / ErsBlock.BOXES_ROWS;
			isTiled = true;
		}
	}

	/**
	 * 键盘监听类,继承自KeyAdapter
	 */
	private class ControlKeyListener extends KeyAdapter {
		public void keyPressed(KeyEvent ke) {
			if (!game.isPlaying() || game.autoPlay) return;

			ErsBlock block = game.getCurBlock();
			switch (ke.getKeyCode()) {
				case KeyEvent.VK_DOWN:
					block.moveDown();
					break;
				case KeyEvent.VK_LEFT:
					block.moveLeft();
					break;
				case KeyEvent.VK_RIGHT:
					block.moveRight();
					break;
				case KeyEvent.VK_UP:
					block.turnNext();
					break;
				default:
					break;
			}
		}
	}
}

⌨️ 快捷键说明

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