📄 eventhandle.java
字号:
package rich;
import java.awt.Container;
import java.awt.GridLayout;
import javax.swing.*;
public class EventHandle{
JFrame f;
Container contentPane;
String title;
JPanel panel;
JLabel message;
int messageType;
int optionType ;
JOptionPane optionPane;
String event;
String Owner;
MapControl map;
int result;
Character player;
Ground ground;
SpecialEvent special;
int action;
EventHandle(MapControl map){
this.map = map;
f = new JFrame(" -Informaton- ");
contentPane = f.getContentPane();
panel = new JPanel();
f.show();
}
public void Event(Character player,Ground ground){
this.player = player;
this.ground = ground;
if(ground.getOwner()==null){
Owner = "null";
}else{
Owner = ground.getOwner().getName();
}
panel.setLayout(new GridLayout(1,2));
JPanel left = new JPanel();
JPanel right = new JPanel();
right.setLayout(new GridLayout(7,1));
message = new JLabel(ground.getIcon());
left.add(message);
message = new JLabel();
message.setText("Address : "+ground.getAddress());
right.add(message);
message = new JLabel();
message.setText("Level : "+ground.getLevel());
right.add(message);
message = new JLabel();
message.setText("Cost : "+ground.getCost());
right.add(message);
message = new JLabel();
message.setText("Profit : "+ground.getProfit());
right.add(message);
message = new JLabel();
message.setText("Owner : "+Owner);
right.add(message);
message = new JLabel();
message.setText("Player Cash In Hand : "+player.getCashInHand());
right.add(message);
message = new JLabel();
message.setText("Player Cash In Bank : "+player.getCashInBank());
right.add(message);
message = new JLabel();
panel.add(left);
panel.add(right);
if(ground.getGroundType()=="house"){
action = player.getCashInHand()-ground.getCost();
title = "-House-";
message = new JLabel();
if(!ground.gotOwner()){ // buy house
if(action > 0){
message.setText("you want buy it? Cash in Hand will be "+action);
messageType = JOptionPane.QUESTION_MESSAGE;
optionType = JOptionPane.OK_CANCEL_OPTION;
event = "buy";
}
else{
message.setText("Sorry , not enough money");
messageType = JOptionPane.WARNING_MESSAGE;
optionType = JOptionPane.DEFAULT_OPTION;
event = "cant buy";
}
}else if(ground.getOwner().equals(player)){ // update building
action = player.getCashInHand()-ground.getProfit();
if(action > 0){
message.setText("Do you want Level Up your hourse ? cash in hand will be "+action);
messageType = JOptionPane.QUESTION_MESSAGE;
optionType = JOptionPane.OK_CANCEL_OPTION;
event = "Level up";
}else{
message.setText("Sorry , not enough money");
messageType = JOptionPane.WARNING_MESSAGE;
optionType = JOptionPane.DEFAULT_OPTION;
event = "cant Level up";
}
}else{ // pay roadtax
action = player.getCashInHand() - ground.getProfit();
if(action >0){
message.setText("now u stay at "+ground.getOwner().getName()+" house,you must pay $"+ground.getProfit());
messageType = JOptionPane.QUESTION_MESSAGE;
optionType = JOptionPane.DEFAULT_OPTION;
event = "pay";
}
else{
message.setText(player.getName()+" not enough money to pay , "+player.getName()+" lose.");
messageType = JOptionPane.QUESTION_MESSAGE;
optionType = JOptionPane.DEFAULT_OPTION;
event = "cant pay";
}
}
}else if(ground.getGroundType()=="bank"){ // bank Event
title = "-Bank-";
event = "bank";
}else if(ground.getGroundType()=="special"){ //special event
title = "-Special chance-";
special = new SpecialEvent();
message = new JLabel();
message.setText(special.getSpecialEvent().getEvent());
messageType = JOptionPane.QUESTION_MESSAGE;
optionType = JOptionPane.DEFAULT_OPTION;
if(special.getSpecialEvent().getType()=="expend"){
action = player.getCashInHand() - special.getSpecialEvent().getQuantity();
if(action<0){
event = "cant pay";
}
else{
event = "special Event";
}
}else{
event = "special Event";
}
}
contentPane.add(panel);
f.setLocation(350,120);
f.pack();
// ***** show message ******** //
if (player.getPlayerType() == "Player") {
if (ground.getGroundType() == "house") {
result = JOptionPane.showConfirmDialog(null, message, title,optionType, messageType);
}
if (ground.getGroundType() == "bank") {
try {
String s = JOptionPane.showInputDialog("Now you can take $"+ player.getCashInBank());
if (s == null || s.equals("")) {
result = 0;
} else {
result = Integer.parseInt(s);
}
} catch (Exception e) {
JOptionPane.showMessageDialog(null,"Format error,cant have space !!","Format error",1);
}
}
if (ground.getGroundType() == "special") {
result = JOptionPane.showConfirmDialog(null, message, title,optionType, messageType);
}
}
if(player.getPlayerType()=="Computer"){
if (event == "cant pay") {
result = JOptionPane.showConfirmDialog(null, message, title,optionType, messageType);
} else {
if (ground.getGroundType() == "house") {
result = JOptionPane.OK_OPTION;
}
if (ground.getGroundType() == "bank") {
result = player.getCashInBank();
}
if (ground.getGroundType() == "special") {
result = JOptionPane.OK_OPTION;
}
}
}
eventHanding(result,event);
}
private void eventHanding(int result,String event){
if(event == "buy"){
if(result == JOptionPane.OK_OPTION){
player.expent(ground.getCost());
ground.setOwner(player);
f.setVisible(false);
}
else{
f.setVisible(false);
}
}
if(event == "cant buy"){
if(result == JOptionPane.OK_OPTION){
f.setVisible(false);
}
}
if(event == "Level up"){
if(result == JOptionPane.OK_OPTION){
player.expent(ground.getProfit());
ground.levelUp();
map.resetMap(ground.getAddress());
f.setVisible(false);
}
else{
f.setVisible(false);
}
}
if(event == "cant Level up"){
if(result == JOptionPane.OK_OPTION){
f.setVisible(false);
}
}
if(event == "pay"){
if(result == JOptionPane.OK_OPTION){
player.expent(ground.getProfit());
ground.getOwner().income(ground.getProfit());
f.setVisible(false);
}
}
if(event == "cant pay"){
if(result == JOptionPane.OK_OPTION){
f.setVisible(false);
System.exit(0);
}
}
if(event == "bank"){
if(result <= player.getCashInBank()){
player.carrying(result);
player.income(result);
f.setVisible(false);
}else{
JOptionPane.showMessageDialog(null,"your cash in bank not enough!","your cash in bank not enough!",0);
f.setVisible(false);
}
}
if(event == "special Event"){
if(special.getSpecialEvent().getType()=="expent"){
player.expent(special.getSpecialEvent().getQuantity());
f.setVisible(false);
}
else{
player.income(special.getSpecialEvent().getQuantity());
f.setVisible(false);
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -