-
Notifications
You must be signed in to change notification settings - Fork 0
/
MoveMenu.java
125 lines (117 loc) · 5.28 KB
/
MoveMenu.java
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
import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
public class MoveMenu extends JPanel {
private static final long serialVersionUID = 1L;
/*** Movement Stats ***/
public Territory from;
public Territory to;
private int finit;
private int tinit;
private class myListener implements ChangeListener{
public void stateChanged(ChangeEvent e) {
int m = slider.getValue();
from.occupy(from.getOccupation(), finit - m);
to.occupy(to.getOccupation(), tinit + m);
reset();
}
}
/*** Component proportions ***/
public static final double MY_HEIGHT = 0.2;
public static final double MY_WIDTH = 0.25;
public static final double LABEL_HEIGHT = 0.10;
public static final double BAR_HEIGHT = 0.15;
public static final double TROOP_HEIGHT = 0.25;
public static final double SLIDER_WIDTH = 0.8;
public static final double SLIDER_HEIGHT = 0.2;
/*** Components ***/
private String[][] buttonNames = {{"mobilize"}};
private JTextArea[] labels;
private JLabel[] troopCount;
private JPanel buttonHolder;
public JButton[] buttons;
public JSlider slider;
/*** Constructor ***/
public MoveMenu(Territory f, Territory t, int minMove) {
//Territories
from = f;
to = t;
finit = from.getTroops() + minMove;
tinit = to.getTroops() - minMove;
//Initialize Components
setLayout(null);
setBackground(GameBoard.MAIN);
setBorder(BorderFactory.createLineBorder(GameBoard.FONT));
labels = new JTextArea[2];
troopCount = new JLabel[2];
buttonHolder = new JPanel(new GridLayout(0, buttonNames.length));
buttonHolder.setBorder(BorderFactory.createLineBorder(GameBoard.FONT));
buttons = new JButton[buttonNames.length];
for(int i = 0; i < 2; i++){
labels[i] = new JTextArea();
labels[i].setOpaque(false);
labels[i].setEditable(false);
add(labels[i]);
}
labels[0].setForeground(from.getOccupation().getColor());
labels[1].setForeground(to.getOccupation().getColor());
labels[1].setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
for(int i = 0; i < 2; i++){
troopCount[i] = new JLabel("", SwingConstants.CENTER);
troopCount[i].setBackground(GameBoard.MAIN);
add(troopCount[i]);
}
for(int i = 0; i < buttons.length; i++){
buttons[i] = new myButton(buttonNames[i], GameBoard.MAIN, GameBoard.MOUSE);
buttons[i].setForeground(GameBoard.FONT);
buttons[i].setFocusPainted(false);
buttons[i].setBorder(BorderFactory.createEmptyBorder());
buttons[i].setFont(new Font("Consolas", Font.PLAIN, buttons[i].getFont().getSize()));
buttonHolder.add(buttons[i]);
}
add(buttonHolder);
slider = new JSlider(JSlider.HORIZONTAL, minMove, from.getTroops() + minMove - 1, minMove);
slider.addChangeListener(new myListener());
slider.setMajorTickSpacing((slider.getMaximum() - slider.getMinimum()) >= 10 ? (slider.getMaximum() - slider.getMinimum()) / 10 : 1);
slider.setMinorTickSpacing(1);
slider.setPaintTicks(true);
slider.setPaintLabels(true);
slider.setBackground(GameBoard.MAIN);
slider.setForeground(GameBoard.FONT);
add(slider);
}
public void reset(){
//This
setBounds((int)(BoardState.BOARD.getWidth() * (0.5 - MY_WIDTH / 2)), (int)(BoardState.BOARD.getHeight() * (0.5 - MY_HEIGHT / 2)), (int)(BoardState.BOARD.getWidth() * MY_WIDTH), (int)(BoardState.BOARD.getHeight() * MY_HEIGHT));
//Labels
labels[0].setForeground(from.getOccupation().getColor());
labels[1].setForeground(to.getOccupation().getColor());
labels[0].setBounds(0, 0, getWidth() / 2, (int)(getHeight() * LABEL_HEIGHT));
labels[1].setBounds(labels[0].getWidth(), 0, labels[0].getWidth(), labels[0].getHeight());
labels[0].setText(from.getName().toUpperCase());
labels[1].setText(to.getName().toUpperCase());
labels[0].setMargin(new Insets(5, 5, 5, 5));
labels[1].setMargin(new Insets(5, 5, 5, 5));
labels[0].setFont(new Font("Consolas", Font.PLAIN, (int)(0.8 * labels[0].getHeight())));
labels[1].setFont(labels[0].getFont());
//Buttons
buttonHolder.setBounds(0, (int)(getHeight() * (1 - BAR_HEIGHT)), getWidth(), (int)(getHeight() * BAR_HEIGHT));
for(JButton b: buttons)
b.setFont(new Font(b.getFont().getFontName(), b.getFont().getStyle(), (int)(0.8 * buttonHolder.getHeight())));
//Troop counts
int c = (labels[0].getHeight() + buttonHolder.getBounds().y) / 3;
troopCount[0].setForeground(from.getOccupation().getColor());
troopCount[1].setForeground(to.getOccupation().getColor());
troopCount[0].setBounds(getWidth() / 2 - (int)(1.5 * TROOP_HEIGHT * getHeight()), c - (int)(TROOP_HEIGHT * getHeight()) / 2, (int)(TROOP_HEIGHT * getHeight()), (int)(TROOP_HEIGHT * getHeight()));
troopCount[1].setBounds(getWidth() / 2 + troopCount[0].getWidth() / 2, troopCount[0].getY(), troopCount[0].getWidth(), troopCount[0].getHeight());
troopCount[0].setFont(new Font("Consolas", Font.BOLD, (int)(0.8 * troopCount[0].getHeight())));
troopCount[1].setFont(new Font("Consolas", Font.BOLD, (int)(0.8 * troopCount[1].getHeight())));
troopCount[0].setText(Integer.toString(from.getTroops()));
troopCount[1].setText(Integer.toString(to.getTroops()));
//Slider
c = 3 * c / 2;
slider.setBounds((int)((1 - SLIDER_WIDTH) / 2 * getWidth()), c, (int)(SLIDER_WIDTH * getWidth()), (int)(SLIDER_HEIGHT * getHeight()));
//Repaint
repaint();
}
}