package cn.itcast.snake.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.snake.controller.Controller;
import cn.itcast.snake.entities.Food;
import cn.itcast.snake.entities.Ground;
import cn.itcast.snake.entities.Snake;
import cn.itcast.snake.listener.GameListener;
import cn.itcast.snake.util.Global;
import cn.itcast.snake.view.GamePanel;
/**
* 主界面, 实现了 GameListener 接口
*
* @version 1.0, 01/01/08
*
* @author 汤阳光
*
*/
public class MainFrame extends JFrame implements GameListener {
/**
*
*/
private static final long serialVersionUID = 1L;
/**
* Launch the application
*
* @param args
*/
public static void main(String args[]) {
try {
MainFrame frame = new MainFrame(new Controller(new Snake(),
new Food(), new Ground(), new GamePanel(), new JLabel()));
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
private final GameOptionPanel optionPanel;
private final GamePanel gamePanel;
private final Snake snake;
private final Ground ground;
private final Food food;
private final JLabel infoLabel;
private final Controller controller;
/**
* Create the frame
*/
public MainFrame(Controller c) {
super();
this.controller = c;
this.setTitle("传智播客版贪吃蛇");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(null);
this.setResizable(false);
int left = 10;
optionPanel = new GameOptionPanel();
/** =======* */
gamePanel = c.getGamePanel();
snake = c.getSnake();
ground = c.getGround();
food = c.getFood();
infoLabel = c.getGameInfoLabel() == null ? new JLabel() : c
.getGameInfoLabel();
c.setGameInfoLabel(infoLabel);
optionPanel.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);
}
});
optionPanel.getButton_backgroundColor().addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
Color backgroundColor = JColorChooser
.showDialog(MainFrame.this, "请选择背景的颜色",
new Color(0xcfcfcf));
if (backgroundColor != null)
gamePanel.setBackgroundColor(backgroundColor);
}
});
optionPanel.getButton_foodColor().addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
Color foodColor = JColorChooser.showDialog(
MainFrame.this, "请选择食物的颜色", Color.DARK_GRAY);
if (foodColor != null)
food.setColor(foodColor);
}
});
optionPanel.getButton_headColor().addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
Color headColor = JColorChooser
.showDialog(MainFrame.this, "请选择蛇头的颜色",
new Color(0xFF4500));
if (headColor != null)
snake.setHeadColor(headColor);
}
});
optionPanel.getButton_bodyColor().addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
Color bodyColor = JColorChooser.showDialog(
MainFrame.this, "请选择蛇身体的颜色", Color.DARK_GRAY);
if (bodyColor != null)
snake.setBodyColor(bodyColor);
}
});
this.addFocusListener(new FocusAdapter() {
public void focusLost(FocusEvent arg0) {
controller.pauseGame();
if (optionPanel.getPauseButton().isEnabled())
optionPanel.getPauseButton().setText("继续游戏");
}
});
gamePanel.addFocusListener(new FocusAdapter() {
public void focusGained(FocusEvent arg0) {
// controller.continueGame();
}
public void focusLost(FocusEvent arg0) {
controller.pauseGame();
if (optionPanel.getPauseButton().isEnabled())
optionPanel.getPauseButton().setText("继续游戏");
}
});
optionPanel.getRadioButton_map2().addChangeListener(
new ChangeListener() {
public void stateChanged(ChangeEvent e) {
controller.setMap(optionPanel.getRadioButton_map2()
.isSelected() ? 2 : 1);
}
});
optionPanel.getNewGameButton().addActionListener(new ActionListener() {
/**
* 开始游戏的按钮
*/
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if (controller.isPlaying()) {
return;
}
controller.newGame();
}
});
optionPanel.getStopGameButton().addActionListener(new ActionListener() {
/**
* 停止游戏的按钮
*/
public void actionPerformed(ActionEvent e) {
controller.stopGame();
}
});
optionPanel.getPauseButton().setEnabled(false);
optionPanel.getStopGameButton().setEnabled(false);
optionPanel.getPauseButton().addActionListener(new ActionListener() {
/**
* 暂停/继续游戏的按钮
*/
public void actionPerformed(ActionEvent e) {
if (controller.isPausingGame()) {
controller.continueGame();
} else {
controller.pauseGame();
}
if (controller.isPausingGame())
optionPanel.getPauseButton().setText("继续游戏");
else
optionPanel.getPauseButton().setText("暂停游戏");
}
});
optionPanel.getCheckBox_drawGridding().addChangeListener(
new ChangeListener() {
public void stateChanged(ChangeEvent arg0) {
optionPanel.getButton_griddingColor().setVisible(
optionPanel.getCheckBox_drawGridding()
.isSelected());
ground.setDrawGridding(optionPanel
.getCheckBox_drawGridding().isSelected());
}
});
optionPanel.getButton_default().addActionListener(new ActionListener() {
/**
* 恢复默认设置的按钮
*/
public void actionPerformed(ActionEvent e) {
gamePanel
.setBackgroundColor(GamePanel.DEFAULT_BACKGROUND_COLOR);
optionPanel.getCheckBox_drawGridding().setSelected(false);
ground.setGriddingColor(Ground.DEFAULT_GRIDDING_COLOR);
snake.setHeadColor(Snake.DEFAULT_HEAD_COLOR);
snake.setBodyColor(Snake.DEFAULT_BODY_COLOR);
optionPanel.getRadioButton_map1().setSelected(true);
}
});
/** ******************* */
infoLabel.setBounds(10, 0, infoLabel.getSize().width - 10, infoLabel
.getSize().height);
gamePanel.setBounds(0, infoLabel.getSize().height,
gamePanel.getSize().width, gamePanel.getSize().height);
/**
* subPanel
*/
JPanel subPanel = new JPanel();
subPanel.setLayout(null);
subPanel.setBorder(new EtchedBorder(EtchedBorder.LOWERED));
subPanel.setFocusable(false);
subPanel.setSize(gamePanel.getSize().width + 1,
infoLabel.getSize().height + gamePanel.getSize().height + 1);
subPanel.setBounds(left, 5, subPanel.getSize().width, subPanel
.getSize().height);
subPanel.add(infoLabel);
subPanel.add(gamePanel);
optionPanel.setBounds(left, subPanel.getSize().height + 10, optionPanel
.getSize().width, optionPanel.getSize().height);
/**
* 说明
*/
JPanel infoPanel = new JPanel();
infoPanel.setBorder(new EtchedBorder(EtchedBorder.LOWERED));
infoPanel.setLayout(null);
infoPanel.setBounds(left + optionPanel.getSize().width + 5, subPanel
.getSize().height + 10, gamePanel.getSize().width
- optionPanel.getSize().width - 5 + 1,
optionPanel.getSize().height);
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
经典贪吃蛇源代码包含设计文档和API (119个子文件)
游戏开发学习指南.chm 373KB
MainFrame.class 8KB
Controller.class 7KB
GameOptionPanel.class 6KB
Snake.class 6KB
Ground.class 4KB
Global.class 3KB
GamePanel.class 2KB
Food.class 2KB
MainFrame$13.class 2KB
Snake$SnakeDriver.class 2KB
MainFrame$11.class 1KB
MainFrame$12.class 1KB
MainFrame$7.class 1KB
MainFrame$6.class 1KB
MainFrame$2.class 1KB
MainFrame$4.class 1KB
MainFrame$8.class 1KB
MainFrame$1.class 1KB
MainFrame$5.class 1KB
MainFrame$3.class 1KB
GameOptionPanel$1.class 1KB
MainFrame$9.class 967B
MainFrame$10.class 920B
GameListener.class 218B
SnakeListener.class 182B
.classpath 232B
stylesheet.css 1KB
inherit.gif 57B
致高校IT教师的一封信.htm 36KB
先就业后付款.htm 18KB
传智播客系列视频下载.htm 14KB
Snake.html 32KB
Controller.html 31KB
GameOptionPanel.html 29KB
MainFrame.html 25KB
Ground.html 23KB
GamePanel.html 23KB
index-7.html 21KB
Food.html 17KB
index-16.html 16KB
Global.html 16KB
Snake.html 15KB
Ground.html 13KB
Food.html 13KB
serialized-form.html 12KB
package-use.html 12KB
GamePanel.html 11KB
index-4.html 11KB
index-3.html 11KB
SnakeListener.html 10KB
GameListener.html 10KB
help-doc.html 10KB
GameListener.html 10KB
index-9.html 9KB
package-use.html 9KB
SnakeListener.html 9KB
overview-tree.html 8KB
package-summary.html 8KB
index-15.html 8KB
Controller.html 8KB
index-12.html 7KB
index-6.html 7KB
index-1.html 7KB
constant-values.html 7KB
package-tree.html 7KB
index-10.html 7KB
package-use.html 7KB
index-14.html 7KB
index-17.html 7KB
package-use.html 7KB
index-8.html 7KB
package-summary.html 7KB
index-13.html 6KB
index-19.html 6KB
index-2.html 6KB
index-5.html 6KB
index-11.html 6KB
index-18.html 6KB
package-tree.html 6KB
package-summary.html 6KB
package-summary.html 6KB
package-summary.html 6KB
overview-summary.html 6KB
package-tree.html 6KB
package-tree.html 6KB
package-tree.html 6KB
package-summary.html 6KB
package-tree.html 6KB
GameOptionPanel.html 6KB
MainFrame.html 6KB
Global.html 6KB
package-use.html 5KB
package-use.html 5KB
deprecated-list.html 5KB
allclasses-frame.html 2KB
overview-frame.html 2KB
allclasses-noframe.html 2KB
index.html 1KB
package-frame.html 1KB
共 119 条
- 1
- 2
资源评论
- a5111a2012-06-27好像不是贪食蛇的,我试了 出现的是一些英文字母
jolinzh
- 粉丝: 1
- 资源: 12
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功