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();
}
});
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论















收起资源包目录











































































共 60 条
- 1
资源评论

- qq_287032692016-03-21还可以 就是要修改路径
- 三国小和尚2015-01-15初学,还是有用的
- poinsonapple2014-04-14还不错,初学者可以看看
- u0106495842013-09-26不知道路径怎么改。运行时有好多警告。
- xiuxiu1l2m3n2013-01-05修改了build path没错了

fengzeyu1021
- 粉丝: 1
- 资源: 1
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助


安全验证
文档复制为VIP权益,开通VIP直接复制
