package Driver;
import java.awt.BorderLayout;
import java.util.Observer;
import java.util.Observable;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.AbstractButton;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import Warrior.Warrior;
public class Driver extends JFrame implements Observer, ActionListener{
private static final long serialVersionUID = 8152334258705014734L;
int screenWidth = 600;
int processingHeight = 600;
int bottomHeight = 110;
int buttonHeight = 25;
int buttonWidth = 125;
JButton downButtom;
JButton upButtom;
JButton leftButtom;
JButton rightButtom;
JButton update;
JLabel cheatingCode;
JLabel CurrentLife;
JLabel enemyPower;
JLabel battleInfor;
JTextField cheatingField;
Observable observable;
Display display;
Warrior warrior;
public Driver() {
super("Embedded PApplet");
this.setLayout(new BorderLayout());
JPanel processingPanel = new JPanel();
//You must add one to account for the border.
processingPanel.setPreferredSize(new Dimension(screenWidth+1, processingHeight+1));
processingPanel.setLayout(new BorderLayout());
display = new Display(screenWidth, processingHeight,this);
processingPanel.add(display, BorderLayout.NORTH);
JPanel bottomPanel = new JPanel();
bottomPanel.setPreferredSize(new Dimension(screenWidth+1, bottomHeight));
bottomPanel.setLayout(new BorderLayout());
//bottomPanel.add(createButtonPanel(), BorderLayout.EAST);
//bottomPanel.add(createTextFieldPanel(), BorderLayout.NORTH);
bottomPanel.add(createTextPanel(), BorderLayout.WEST);
add(processingPanel, BorderLayout.CENTER);
add(bottomPanel, BorderLayout.SOUTH);
// important to call this whenever embedding a PApplet.
// It ensures that the animation thread is started and
// that other internal variables are properly set.
display.init();
}
public Driver(Observable observable){
this.observable=observable;
observable.addObserver(this);
}
private JPanel createTextPanel() {
JPanel textPanel = new JPanel();
textPanel.setLayout(new BorderLayout());
CurrentLife = new JLabel("Current Life:" + String.valueOf(display.LifeOfWarrior())+" "
+"Current Power:"+String.valueOf(display.getPower()));
battleInfor = new JLabel();
enemyPower= new JLabel();
textPanel.add(CurrentLife, BorderLayout.NORTH);
textPanel.add(enemyPower, BorderLayout.CENTER);
textPanel.add(battleInfor, BorderLayout.SOUTH);
return textPanel;
}
private JPanel createTextFieldPanel() {
JPanel textFieldPanel = new JPanel();
textFieldPanel.setLayout(new BorderLayout());
update= new JButton("Update");
update.setHorizontalTextPosition(AbstractButton.LEADING);
update.setActionCommand("Update");
update.addActionListener(this);
textFieldPanel.add(update, BorderLayout.EAST);
cheatingCode=new JLabel("CheatingCode: ");
textFieldPanel.add(cheatingCode, BorderLayout.WEST);
cheatingField = new JTextField();
cheatingField.setColumns(10);
textFieldPanel.add(cheatingField, BorderLayout.CENTER);
return textFieldPanel;
}
private JPanel createButtonPanel() {
JPanel buttonPanel = new JPanel();
JPanel centerButton= new JPanel();
JPanel downButtonPanel= new JPanel();
buttonPanel.setPreferredSize(new Dimension(buttonWidth * 3, buttonHeight));
buttonPanel.setLayout(new BorderLayout());
upButtom = new JButton("Up");
upButtom.setHorizontalTextPosition(AbstractButton.LEADING);
upButtom.setActionCommand("Up");
upButtom.addActionListener(this);
centerButton.add(upButtom, BorderLayout.CENTER);
leftButtom = new JButton("Left");
leftButtom.setHorizontalTextPosition(AbstractButton.LEADING);
leftButtom.setActionCommand("Left");
leftButtom.addActionListener(this);
downButtonPanel.add(leftButtom, BorderLayout.WEST);
downButtom = new JButton("Down");
downButtom.setHorizontalTextPosition(AbstractButton.LEADING);
downButtom.setActionCommand("Down");
downButtom.addActionListener(this);
downButtonPanel.add(downButtom, BorderLayout.CENTER);
rightButtom = new JButton("Right");
rightButtom.setHorizontalTextPosition(AbstractButton.LEADING);
rightButtom.setActionCommand("Right");
rightButtom.addActionListener(this);
downButtonPanel.add(rightButtom, BorderLayout.EAST);
buttonPanel.add(centerButton, BorderLayout.NORTH);
buttonPanel.add(downButtonPanel, BorderLayout.CENTER);
return buttonPanel;
}
public void actionPerformed(ActionEvent e) {
if ("Up".equals(e.getActionCommand())) {
warrior.up();
} else if ("Down".equals(e.getActionCommand())) {
warrior.down();
} else if("Left".equals(e.getActionCommand())){
warrior.left();
} else if("Right".equals(e.getActionCommand())){
warrior.right();
} else if("Update".equals(e.getActionCommand())){
display.handleUpdateButton(cheatingField.getText());
}
}
public void update(Observable o, Object obj){
this.warrior=(Warrior)o;
textPanelChanged();
}
public void textPanelChanged() {
// TODO Auto-generated method stub
enemyPower.setText("Boss's power:" + String.valueOf(display.getStaffPower())+" "+ "Enemy's power: "
+String.valueOf(display.getStaffPower()));
warrior.renew();
if(warrior.meetStaff()){
CurrentLife.setText("Current Life:" + String.valueOf(warrior.Life())+" "+
"Current Power:"+String.valueOf(warrior.getPower()));
battleInfor.setText(warrior.getBattleInfor());
warrior.meetChanged();
}
}
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new Driver();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main(String args[]) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
game.rar (60个子文件)
WayToTreasure
.project 389B
bin
State
NoKeyState.class 611B
HasKeyState.class 613B
State.class 369B
Location
Medicine.class 3KB
Weapon.class 3KB
Treasure.class 3KB
Boss.class 3KB
Visitor.class 143B
Wall.class 2KB
Location.class 207B
Enemy.class 3KB
Gate.class 3KB
Key.class 3KB
Driver
Display.class 5KB
Driver$1.class 497B
Driver.class 6KB
java.policy.applet 141B
Warrior
Warrior.class 5KB
Visitor
Visit.class 511B
DrawVisitor.class 3KB
src
State
State.java 196B
HasKeyState.java 353B
NoKeyState.java 351B
Location
Key.java 2KB
Location.java 216B
Treasure.java 2KB
Gate.java 2KB
Medicine.java 2KB
Enemy.java 2KB
Boss.java 2KB
Wall.java 1KB
Weapon.java 2KB
Visitor.java 117B
Driver
Driver.java 6KB
Display.java 4KB
Warrior
Warrior.java 4KB
Visitor
Visit.java 676B
DrawVisitor.java 3KB
.classpath 351B
data
warrior_mask.jpg 22KB
background.jpg 73KB
key.jpg 1KB
medicine.jpg 1KB
weapon.jpg 1KB
gate.jpg 1KB
key_mask.jpg 10KB
treasure.jpg 931B
win.jpg 11KB
gameOver.jpg 35KB
boss_mask.jpg 11KB
treasure_mask.jpg 10KB
weapon_mask.jpg 10KB
enemy_mask.jpg 10KB
enemy.jpg 1KB
boss.jpg 1KB
wall.jpg 1KB
medicine_mask.jpg 10KB
warrior.jpg 15KB
core.jar 225KB
共 60 条
- 1
资源评论
- 三国小和尚2015-01-15初学,还是有用的
- xiuxiu1l2m3n2013-01-05修改了build path没错了
- u0106495842013-09-26不知道路径怎么改。运行时有好多警告。
- qq_287032692016-03-21还可以 就是要修改路径
- poinsonapple2014-04-14还不错,初学者可以看看
fengzeyu1021
- 粉丝: 1
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功