-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMoveMenu.java
More file actions
executable file
·159 lines (140 loc) · 5.36 KB
/
MoveMenu.java
File metadata and controls
executable file
·159 lines (140 loc) · 5.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.awt.event.*;
import java.text.NumberFormat;
import javax.swing.text.NumberFormatter;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.util.*;
public class MoveMenu extends JFrame implements ActionListener{
//private JPanel label_input;
private ArrayList<JFormattedTextField> textFields;
//private JFormattedTextField[] textFields;
private Nodes sender;
private Nodes receiver;
private ArrayList<Tuple> unitTypes;
private ArrayList<AUnit> units;
private GameBoard g;
private Nodes[] Buildings;
public MoveMenu(Nodes inputNode, Nodes outputNode, Nodes[] Building, GameBoard gb){
g = gb;
sender = inputNode;
receiver = outputNode;
setTitle("How many units do you want to move?");
unitTypes = new ArrayList<Tuple>();
units = inputNode.getUnits();
Buildings = Building;
//System.out.println(units.size() + " units found");
for (int i = 0; i < units.size();i++){
if (units.get(i).getMoves() > 0){
String name = units.get(i).getType();
Boolean newType = true;
for (int j = 0; j < unitTypes.size();j++){
if (unitTypes.get(j).getName().equals(name)){
newType = false;
unitTypes.get(j).increment_number();
break;
}
}
if (newType == true){
unitTypes.add(new Tuple(new String(units.get(i).getType()),new Integer(1)));
}
}
}
textFields = new ArrayList<JFormattedTextField>(unitTypes.size());
for (int i = 0; i < unitTypes.size();i++){
textFields.add(new JFormattedTextField(NumberFormat.getIntegerInstance()));
}
//new GridLayout(2,2)
ArrayList<JPanel> panels = new ArrayList<JPanel>();
ArrayList<JLabel> labels = new ArrayList<JLabel>();
setLayout(new GridLayout(unitTypes.size() + 1,1));
// Create the entry fields for how many units to move
for (int i = 0; i < unitTypes.size();i++){
panels.add(new JPanel());
//JPanel label_input = new JPanel();
//JLabel label = new JLabel("How many of your " + unitTypes.get(i).getNumber() + " " + unitTypes.get(i).getName() + " do you want to move?");
labels.add(new JLabel("How many of your " + unitTypes.get(i).getNumber() + " " + unitTypes.get(i).getName() + " do you want to move?"));
//label_input.setLayout(new GridLayout(unitTypes.size(),2));
//panels.get(i).setLayout(new GridLayout(unitTypes.size(),2));
textFields.get(i).setColumns(10);
textFields.get(i).addActionListener(this);
/*
label_input.add(label);
label_input.add(textFields.get(i));
this.add(label_input, BorderLayout.NORTH); */
panels.get(i).add(labels.get(i));
panels.get(i).add(textFields.get(i));
this.add(panels.get(i),BorderLayout.AFTER_LAST_LINE);
pack();
}
// Create the two buttons
JPanel submitPanel = new JPanel();
submitPanel.setLayout(new FlowLayout());
JButton move = new JButton("Move Units");
move.addActionListener(this);
submitPanel.add(move);
JButton cancel = new JButton("Cancel");
cancel.addActionListener(this);
submitPanel.add(cancel);
//this.add(submitPanel,BorderLayout.CENTER);
this.add(submitPanel);
this.setSize(325,300);
//this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
//Set window to middle of screen
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
int w = this.getSize().width;
int h = this.getSize().height;
int x = (dim.width-w)/2;
int y = (dim.height-h)/2;
this.setLocation(x, y);
}
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equals("Move Units"))
{
for (int i = 0; i < unitTypes.size();i++){
try{
Long numberToMove = (Long)textFields.get(i).getValue();
//System.out.println("Found " + numberToMove + " " + unitTypes.get(i).getName());
Boolean typeFound = true;
if(numberToMove<1)
{
numberToMove = new Long(1);
}
else if(numberToMove>=units.size())
{
numberToMove = new Long(units.size() - 1);
}
while(numberToMove > 0 && typeFound == true){
typeFound = false;
ArrayList<AUnit> inputUnits = sender.getUnits();
for (int j = 0; j < inputUnits.size();j++){
if (inputUnits.get(j).getMoves() > 0){
if (inputUnits.get(j).getType().equals(unitTypes.get(i).getName())){
typeFound = true;
inputUnits.get(j).reduceMoves();
receiver.addUnit(inputUnits.get(j));
sender.removeUnit(inputUnits.get(j));
numberToMove--;
break;
}
}
}
}
}
catch (Exception except){}
g.resetMove();
this.dispose();
}
}
if(e.getActionCommand().equals("Cancel"))
{
this.dispose();
}
g.updateSidePanel(sender, Buildings);
}
}