-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommandPanel.java
More file actions
157 lines (135 loc) · 5.05 KB
/
Copy pathCommandPanel.java
File metadata and controls
157 lines (135 loc) · 5.05 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
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.LinkedList;
/**
* A panel that allows users to type information which interacts with the board
*
* @Team MAGA
* @Author Gajun Young - 16440714
* @Author Royal Thomas - 16326926
* @Author Richard Otroshchenko - 16353416
*/
public class CommandPanel extends JPanel {
private static final long serialVersionUID = 1L;
private static final int SIZE = 30;
private static final int FONT_SIZE = 14;
private JTextField commandField = new JTextField("", SIZE);
private final LinkedList<String> commandBuffer = new LinkedList<>();
public static JLabel picLabel;
private static JTextArea availableInputs = new JTextArea("quit", 5, 10);
private static JPanel availableInput;
private static JLabel movesRemaining = new JLabel("");
public CommandPanel() {
JPanel inputPanel = new JPanel(); //Panel that displays input
availableInput = new JPanel(); //Panel that displays available commands that users can use
//A Label that contains the current player icon
try {
picLabel = CluedoUI.imageToLabel("Profiler/default.png");
picLabel.setBorder(BorderFactory.createTitledBorder("Current Player"));
add(picLabel);
}catch(IOException ex){
System.out.println("Unable to load image.");
}
//Beautification
setPreferredSize(new Dimension(1000, 145));
setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
setBorder(new EmptyBorder(0, 10, 0, 0));
//A container that holds the text box and the submit button
inputPanel.setBorder(new EmptyBorder(50, 0, 0, 0));
inputPanel.add(new JLabel("Input Area:"));
//Add a panel to make a list of actions and set its layout to allow for that
JScrollPane scroll = new JScrollPane(availableInputs);
availableInput.setLayout(new BoxLayout(availableInput, BoxLayout.X_AXIS));
availableInput.setPreferredSize(new Dimension(300, 0));
availableInput.setBorder(BorderFactory.createTitledBorder("Available Inputs"));
availableInput.add(scroll);
availableInputs.setMaximumSize(new Dimension(290,200));
availableInputs.setLineWrap(true);
availableInputs.setEditable(false);
add(inputPanel);
add(availableInput);
//An actionListener to listen for user inputs and respond
class AddActionListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
synchronized (commandBuffer) {
commandBuffer.add(commandField.getText());
commandField.setText("");
commandBuffer.notify();
}
}
}
ActionListener listener = new AddActionListener();
commandField.addActionListener(listener);
commandField.setFont(new Font("Times New Roman", Font.PLAIN, FONT_SIZE));
inputPanel.add(commandField);
inputPanel.add(movesRemaining);
//border style
setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), "Command Panel"));
}
//Methods to control the text box in the command panel that lets users know what inputs are available
public static void updateCommands(String[] x){
availableInputs.setText("");
for(String y: x){
availableInputs.append(y + "\n");
}
availableInputs.append("quit");
}
//Used to clear the text box.
public static void updateCommands(){
availableInputs.setText("quit");
}
//Method to update label that shows how many moves the player has left
public static void updateMovesReamining(int x){
if(x == 0){
movesRemaining.setText("You've run out of moves! Enter 'done' to move to next player.");
}else if(x == -1){
movesRemaining.setText("");
}else if(x == -2){
movesRemaining.setText("Choose an exit from the available set in information panel.");
}else if(x == -3) {
movesRemaining.setText("Answering question! Enter 'done' to move to next player.");
} else {
movesRemaining.setText("Moves remaining for current player:" + x);
}
}
/**
* A method that takes in a string of information
*
* @return A string that user typed
*/
public String getCommand() {
String command;
synchronized(commandBuffer) {
while (commandBuffer.isEmpty()) {
try {
commandBuffer.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
command = commandBuffer.pop();
}
return command;
}
/**
* Updates current user image on the command panel
* @param path
*/
public static void updateUserImage(String path) {
try {
BufferedImage myPicture = ImageIO.read(CluedoUI.class.getClassLoader().getResourceAsStream(path));
picLabel.setIcon(new ImageIcon(myPicture));
picLabel.revalidate();
picLabel.repaint();
picLabel.update(picLabel.getGraphics());
}catch(IOException ex){
System.out.println("Unable to load user image.");
}
}
}