-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCluedoUI.java
More file actions
147 lines (129 loc) · 4.65 KB
/
Copy pathCluedoUI.java
File metadata and controls
147 lines (129 loc) · 4.65 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
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.swing.*;
/**
* This UI combines three main panel concepts together onto one frame in which
* users can interact with.
*
* @Team MAGA
* @Author Gajun Young - 16440714
* @Author Royal Thomas - 16326926
* @Author Richard Otroshchenko - 16353416
*/
public class CluedoUI {
//Size of the frame
private static final int FRAME_WIDTH = 1015;
private static final int FRAME_HEIGHT = 830;
private BoardPanel board;
private InformationPanel info;
private CommandPanel command;
private JFrame frame;
private Random rand = new Random();
//Constructor
public CluedoUI(Players players, Weapons weapons, Players dummies) {
frame = new JFrame();
JPanel mainPanel = new JPanel();
board = new BoardPanel(players, weapons, dummies);
command = new CommandPanel();
info = new InformationPanel();
//Set up the main panel to look good, this panel contains two panels.
mainPanel.add(board);
mainPanel.add(info);
mainPanel.setPreferredSize(new Dimension(1000, 750));
mainPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), "Cluedo"));
mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.X_AXIS));
//Position all the panels into their correct places
frame.add(mainPanel);
frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
frame.setTitle("Cluedo");
frame.add(mainPanel, BorderLayout.LINE_START);
frame.add(command, BorderLayout.PAGE_END);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setResizable(false);
frame.setVisible(true);
}
/**
* Sets the board with weapons[default] and tokens which depends on the user inputs
*/
public void setBoard(Players players, Weapons weapons, Players dummies) {
board.set(players, weapons, dummies);
}
/**
* Shows the dice rolling on-screen
*/
public void drawDice(int roll1, int roll2){
if(roll1!=0) { //If roll is 0, we are hiding the dice and thus no roll animation needs to be shown
for (int i = 0; i < 8; i++) {
board.drawDice(rand.nextInt(6) + 1,rand.nextInt(6) + 1);
display();
try {
Thread.sleep(250);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
board.drawDice(roll1, roll2); //Draws the final result
}
/**
* @return A String that the user types
*/
public String getCommand() {
String input = command.getCommand();
if(input.equalsIgnoreCase("quit")) { //The program ends whenever quit is entered
System.exit(0);
}
return input;
}
/**
* Repaint the board to show updated tokens
*/
public void display() {
board.repaint();
}
/**
* Takes a string in which it gets updated onto the information panel
*
* @param string contains information of a tokens turn
*/
public void displayString(String string) {
info.updateContent(string);
}
/**
* Method to clear information panel
*/
public void clearContent(){
info.clearContent();
}
/**
* Finds the path of an image
* @param path
* @return
* @throws IOException
*/
public static JLabel imageToLabel(String path) throws IOException {
BufferedImage myPicture = ImageIO.read(CluedoUI.class.getClassLoader().getResourceAsStream(path));
JLabel picLabel = new JLabel(new ImageIcon(myPicture));
return picLabel;
}
/**
* Method that converts an image into a jlabel object to easily place onto the panel
*
* @param path Location to the image
* @param width sets the images width
* @param height sets the image's height
* @return A JLabel that contains the image
* @throws IOException When provided a path that doesn't exist
*/
public static JLabel imageToResizedLabel(String path, int width, int height) throws IOException {
BufferedImage myPicture = ImageIO.read(CluedoUI.class.getClassLoader().getResourceAsStream(path)); // Reads the file
Image resizedImage = myPicture.getScaledInstance(width, height,
myPicture.SCALE_SMOOTH); // Resize it so that it looks better.
JLabel picLabel = new JLabel(new ImageIcon(resizedImage)); // Convert to JLabel object
return picLabel; // Return
}
}