在好例子网,分享、交流、成长!
您当前所在位置:首页Java 开发实例Android平台开发 → 俄罗斯方块课程设计

俄罗斯方块课程设计

Android平台开发

下载此实例
  • 开发语言:Java
  • 实例大小:0.23M
  • 下载次数:57
  • 浏览次数:389
  • 发布时间:2018-12-12
  • 实例类别:Android平台开发
  • 发 布 人:d90116
  • 文件格式:.rar
  • 所需积分:2
 相关标签: 俄罗斯方块 设计

实例介绍

【实例简介】

【实例截图】

from clipboard

【核心代码】

package cn.itcast.tetris.game;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;

import javax.swing.JColorChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.border.EtchedBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import cn.itcast.tetris.controller.Controller;
import cn.itcast.tetris.entities.Ground;
import cn.itcast.tetris.entities.ShapeFactory;
import cn.itcast.tetris.listener.GameListener;
import cn.itcast.tetris.util.Global;
import cn.itcast.tetris.view.GamePanel;


/**
 * 主界面, 实现了 GameListener 接口
 * 
 * @version 1.0, 01/01/08
 * 
 * @author 汤阳光
 * 
 */
public class MainFrame extends JFrame implements GameListener {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * 
	 * @param args
	 */
	public static void main(String args[]) {
		try {
			Controller controller = new Controller(new ShapeFactory(),
					new Ground(), new GamePanel());
			MainFrame frame = new MainFrame(controller);
			/* 显示窗口 */
			frame.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private final Controller controller;

	private final GameOptionPanel gameOptionPanel;
	private final GamePanel gamePanel;
	private final Ground ground;
	private final ShapeFactory shapeFactory;

	public MainFrame(Controller c) {
		super();
		this.setTitle("传智播客版俄罗斯方块");
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setLayout(null);
		this.setResizable(false);

		if(c.getGameInfoLabel() == null)
			c.setGameInfoLabel(new JLabel());
		this.controller = c;
		

		this.shapeFactory = c.getShapeFactory();
		this.ground = c.getGround();
		this.gamePanel = c.getGamePanel();
		this.gameOptionPanel = new GameOptionPanel();
		final JLabel infoLabel = c.getGameInfoLabel();

		/* 监听器 */
		MyGroundListener mgl = new MyGroundListener();
		ground.addGroundListener(mgl);
		/* 控制器 */
		// controller = new Controller(shapeFactory, ground, gamePanel,
		// infoLabel);
		gameOptionPanel.getNewGameButton().setEnabled(true);
		gameOptionPanel.getStopGameButton().setEnabled(false);

		this.addFocusListener(new FocusAdapter() {
			public void focusGained(FocusEvent arg0) {
				// controller.continueGame();
			}

			public void focusLost(FocusEvent arg0) {
				controller.pauseGame();
				if (gameOptionPanel.getPauseButton().isEnabled())
					gameOptionPanel.getPauseButton().setText("继续游戏");
			}
		});
		
		gamePanel.addFocusListener(new FocusAdapter() {
			public void focusGained(FocusEvent arg0) {
				// controller.continueGame();
			}

			public void focusLost(FocusEvent arg0) {
				controller.pauseGame();
				if (gameOptionPanel.getPauseButton().isEnabled())
					gameOptionPanel.getPauseButton().setText("继续游戏");
			}
		});

		gameOptionPanel.getNewGameButton().addActionListener(
				new ActionListener() {
					/**
					 * 开始游戏的按钮
					 */
					public void actionPerformed(ActionEvent e) {

						// TODO Auto-generated method stub
						if (controller.isPlaying()) {
							return;
						}
						int lineNum = gameOptionPanel.getLineNum();
						int obstacleNum = gameOptionPanel.getObstacleNum();

						controller.newGame();
						/* 放到后面 */
						ground.generateSomeStochasticObstacle(obstacleNum,
								lineNum);
					}
				});

		gameOptionPanel.getStopGameButton().addActionListener(
				new ActionListener() {
					/**
					 * 停止游戏的按钮
					 */
					public void actionPerformed(ActionEvent e) {

						controller.stopGame();
					}
				});

		gameOptionPanel.getPauseButton().setEnabled(false);
		gameOptionPanel.getPauseButton().addActionListener(
				new ActionListener() {
					/**
					 * 暂停/继续游戏的按钮
					 */
					public void actionPerformed(ActionEvent e) {
						if (controller.isPausingGame()) {
							controller.continueGame();

						} else {
							controller.pauseGame();
						}
						if (controller.isPausingGame())
							gameOptionPanel.getPauseButton().setText("继续游戏");
						else
							gameOptionPanel.getPauseButton().setText("暂停游戏");
					}
				});

		gameOptionPanel.getCheckBox_drawGridding().addChangeListener(
				new ChangeListener() {
					public void stateChanged(ChangeEvent arg0) {
						gameOptionPanel.getButton_griddingColor().setVisible(
								gameOptionPanel.getCheckBox_drawGridding()
										.isSelected());
						MainFrame.this.refreshOption();
					}
				});
		gameOptionPanel.getCheckBox_colorfulShape().addChangeListener(
				new ChangeListener() {
					public void stateChanged(ChangeEvent arg0) {
						gameOptionPanel.getButton_shapeColor().setVisible(
								gameOptionPanel.getCheckBox_colorfulShape()
										.isSelected());
						MainFrame.this.refreshOption();
					}
				});
		gameOptionPanel.getCheckBox_colorfulObstacle().addChangeListener(
				new ChangeListener() {
					public void stateChanged(ChangeEvent arg0) {
						gameOptionPanel.getButton_obstacleColor().setVisible(
								gameOptionPanel.getCheckBox_colorfulObstacle()
										.isSelected());
						MainFrame.this.refreshOption();
					}
				});

		gameOptionPanel.getButton_shapeColor().addActionListener(
				new ActionListener() {
					public void actionPerformed(ActionEvent arg0) {
						Color shapeColor = JColorChooser
								.showDialog(MainFrame.this, "请选择图形的颜色",
										new Color(0xFF4500));
						if (shapeColor != null)
							shapeFactory.setDefaultShapeColor(shapeColor);
					}
				});
		gameOptionPanel.getButton_griddingColor().addActionListener(
				new ActionListener() {
					public void actionPerformed(ActionEvent arg0) {
						Color griddingColor = JColorChooser.showDialog(
								MainFrame.this, "请选择网格的颜色", Color.LIGHT_GRAY);
						if (griddingColor != null)
							ground.setGriddingColor(griddingColor);
					}
				});
		gameOptionPanel.getButton_obstacleColor().addActionListener(
				new ActionListener() {
					public void actionPerformed(ActionEvent arg0) {
						Color obstacleColor = JColorChooser.showDialog(
								MainFrame.this, "请选择障碍物的颜色", Color.DARK_GRAY);
						if (obstacleColor != null)
							ground.setObstacleColor(obstacleColor);
					}
				});
		gameOptionPanel.getButton_fullLineColor().addActionListener(
				new ActionListener() {
					public void actionPerformed(ActionEvent arg0) {
						Color fullLineColor = JColorChooser.showDialog(
								MainFrame.this, "请选择满行的效果的颜色", Color.DARK_GRAY);
						if (fullLineColor != null) {
							ground.setFullLineColor(fullLineColor);
						}
					}
				});
		gameOptionPanel.getButtonBackgroundColor().addActionListener(
				new ActionListener() {
					public void actionPerformed(ActionEvent arg0) {
						Color backgroundColor = JColorChooser
								.showDialog(MainFrame.this, "请选择背景的颜色",
										new Color(0xcfcfcf));
						if (backgroundColor != null)
							gamePanel.setBackgroundColor(backgroundColor);
					}
				});

		gameOptionPanel.getButton_default().addActionListener(
				new ActionListener() {
					/**
					 * 恢复默认设置的按钮
					 */
					public void actionPerformed(ActionEvent e) {

						gamePanel
								.setBackgroundColor(GamePanel.DEFAULT_BACKGROUND_COLOR);
						gameOptionPanel.getCheckBox_drawGridding().setSelected(
								false);
						ground.setGriddingColor(Ground.DEFAULT_GRIDDING_COLOR);
						gameOptionPanel.getCheckBox_colorfulShape()
								.setSelected(true);
						shapeFactory
								.setDefaultShapeColor(ShapeFactory.DEFAULT_SHAPE_COLOR);
						gameOptionPanel.getCheckBox_colorfulObstacle()
								.setSelected(true);
						ground.setObstacleColor(Ground.DEFAULT_OBSTACLE_COLOR);
						gameOptionPanel.getTextField_obstacleNum()
								.setText("30");
						gameOptionPanel.getTextField_lineNum().setText("0");
						gameOptionPanel.getTextField_stayTime().setText("300");
						ground.setFullLineColor(Ground.DEFAULT_FULL_LINE_COLOR);
					}
				});

		/** ****** */

		/**
		 * subPanel
		 */
		JPanel subPanel = new JPanel();
		subPanel.setLayout(null);
		subPanel.setBorder(new EtchedBorder(EtchedBorder.LOWERED));

		subPanel.setSize(gamePanel.getSize().width   3,
				infoLabel.getSize().height   gamePanel.getSize().height   2);
		infoLabel.setBounds(5, 0, infoLabel.getSize().width - 5, infoLabel
				.getSize().height);
		gamePanel.setBounds(1, infoLabel.getSize().height,
				gamePanel.getSize().width, gamePanel.getSize().height);
		subPanel.add(infoLabel);
		subPanel.add(gamePanel);

		int left = 10, left2 = 5;
		gameOptionPanel.setBounds(left, 21, gameOptionPanel.getSize().width,
				gameOptionPanel.getSize().height);
		subPanel.setBounds(left   left2   gameOptionPanel.getSize().width, 1,
				subPanel.getSize().width, subPanel.getSize().height);

		/**
		 * 说明
		 */
		JPanel infoPanel = new JPanel();
		infoPanel.setBorder(new EtchedBorder(EtchedBorder.LOWERED));
		infoPanel.setLayout(null);
		infoPanel.setBounds(10, 25   gameOptionPanel.getSize().height,
				gameOptionPanel.getSize().width, subPanel.getSize().height
						- gameOptionPanel.getSize().height - 25);

		final JLabel infoTitleLable = new JLabel();
		infoTitleLable.setFont(new Font("宋体", Font.PLAIN, 12));
		infoTitleLable.setText(Global.TITLE_LABEL_TEXT);
		infoTitleLable.setBounds(10, 5, infoPanel.getSize().width - 10, 20);

		final JTextArea InfoTextArea = new JTextArea();
		InfoTextArea.setFont(new Font("宋体", Font.PLAIN, 12));
		InfoTextArea.setText(Global.INFO_LABEL_TEXT);
		InfoTextArea.setFocusable(false);
		InfoTextArea.setBackground(this.getBackground());
		InfoTextArea.setBounds(10, 25, infoPanel.getSize().width - 20,
				infoPanel.getSize().height - 50);

		infoPanel.add(infoTitleLable);
		infoPanel.add(InfoTextArea);

		gameOptionPanel.getCheckBox_colorfulObstacle().setFocusable(false);
		gameOptionPanel.getCheckBox_colorfulShape().setFocusable(false);
		gameOptionPanel.getCheckBox_drawGridding().setFocusable(false);

		/* 设置主界面大小 */
		this
				.setSize(
						gameOptionPanel.getSize().width
								  gamePanel.getSize().width   left   left2   15,
						gamePanel.getSize().height > gameOptionPanel.getSize().height   20 ? gamePanel
								.getSize().height   60
								: gameOptionPanel.getSize().height   60);

		// this.pack();
		/* 让窗口居中 */
		this.setLocation(this.getToolkit().getScreenSize().width / 2
				- this.getWidth() / 2, this.getToolkit().getScreenSize().height
				/ 2 - this.getHeight() / 2);

		/**
		 * 添加监听器
		 */
		gamePanel.addKeyListener(controller);
		gameOptionPanel.addKeyListener(controller);
		this.addKeyListener(controller);
		controller.addGameListener(this);
		subPanel.addKeyListener(controller);

		this.getContentPane().add(gameOptionPanel);
		this.getContentPane().add(infoPanel);
		this.getContentPane().add(subPanel);
	}

	public void gameOver() {
		// TODO Auto-generated method stub

		gameOptionPanel.getTextField_lineNum().setFocusable(true);
		gameOptionPanel.getTextField_stayTime().setFocusable(true);
		gameOptionPanel.getTextField_obstacleNum().setFocusable(true);
		gameOptionPanel.getPauseButton().setEnabled(false);

		gameOptionPanel.getStopGameButton().setEnabled(false);
		gameOptionPanel.getNewGameButton().setEnabled(true);

		gameOptionPanel.getPauseButton().setText("暂停/继续");
	}

	public void gameStart() {
		// TODO Auto-generated method stub

		gameOptionPanel.getTextField_lineNum().setFocusable(false);
		gameOptionPanel.getTextField_stayTime().setFocusable(false);
		gameOptionPanel.getTextField_obstacleNum().setFocusable(false);

		gameOptionPanel.getPauseButton().setEnabled(true);

		gameOptionPanel.getNewGameButton().setEnabled(false);
		gameOptionPanel.getStopGameButton().setEnabled(true);
	}

	public void gameContinue() {
		// TODO Auto-generated method stub
		gameOptionPanel.getPauseButton().setText("暂停游戏");
	}

	public void gamePause() {
		// TODO Auto-generated method stub
		gameOptionPanel.getPauseButton().setText("继续游戏");
	}

	private void refreshOption() {
		int stayTime = gameOptionPanel.getStayTime();

		ground.setDrawGridding(gameOptionPanel.isDrawGridding());
		shapeFactory.setColorfulShape(!gameOptionPanel.isColorfulShape());
		ground.setColorfulSupport(!gameOptionPanel.isColorfulObstacle());

		Global.STAY_TIME = stayTime;

	}
}

实例下载地址

俄罗斯方块课程设计

不能下载?内容有错? 点击这里报错 + 投诉 + 提问

好例子网口号:伸出你的我的手 — 分享

网友评论

发表评论

(您的评论需要经过审核才能显示)

查看所有0条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。

关于好例子网

本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明

;
报警