-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDisplayGUI.java
More file actions
121 lines (115 loc) · 4.37 KB
/
DisplayGUI.java
File metadata and controls
121 lines (115 loc) · 4.37 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
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class DisplayGUI extends JFrame{
private JLabel[][] labels;
private JLabel cycle;
private JPanel top, grid;
/**
* Constructor for DisplayGUI class
* @param world Instance of World passed by reference
*/
public DisplayGUI(World world)
{
super("GridWorld");
labels = new JLabel[world.HEIGHT][world.WIDTH];
cycle = new JLabel();
top = new JPanel();
grid = new JPanel();
Container c = getContentPane();
c.setLayout(new BorderLayout());
top.add(cycle);
grid.setLayout(new GridLayout(world.HEIGHT, world.WIDTH));
for(int i=0; i<labels.length; i++)
{
for(int j=0; j<labels[i].length; j++)
{
labels[i][j] = new JLabel("", JLabel.CENTER);
grid.add(labels[i][j]);
}
}
c.add(top, BorderLayout.NORTH);
c.add(grid, BorderLayout.CENTER);
addKeyListener(new KeyListener() {
@Override
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if(key == KeyEvent.VK_UP) {
Herder.getInstance().moveUp(world.getGrid());
}
else if(key == KeyEvent.VK_DOWN) {
Herder.getInstance().moveDown(world.getGrid());
}
else if(key == KeyEvent.VK_LEFT) {
Herder.getInstance().moveLeft(world.getGrid());
}
else if(key == KeyEvent.VK_RIGHT) {
Herder.getInstance().moveRight(world.getGrid());
}
setLabelsText(world);
}
@Override
public void keyTyped(KeyEvent e) {}
@Override
public void keyReleased(KeyEvent e) {}
});
setSize(800, 800);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
/**
* Sets the text/icon of each JLabel in the GUI to the correct text/icon
* @param world Instance of World passed by reference
*/
public void setLabelsText(World world){
//Resize each image once, set the icons of labels later
int width = 1000/world.WIDTH;
int height = 1000/world.HEIGHT;
//Carnivore resize
ImageIcon carnivore = new ImageIcon(getClass().getResource("/icons/Carnivore.png"));
Image smallCarn = carnivore.getImage().getScaledInstance(width, height, Image.SCALE_DEFAULT);
carnivore = new ImageIcon(smallCarn);
//Herbivore resize
ImageIcon herbivore = new ImageIcon(getClass().getResource("/icons/Herbivore.png"));
Image smallHerb = herbivore.getImage().getScaledInstance(width, height, Image.SCALE_DEFAULT);
herbivore = new ImageIcon(smallHerb);
//Plant resize
ImageIcon plant = new ImageIcon(getClass().getResource("/icons/Plant.png"));
Image smallPlant = plant.getImage().getScaledInstance(width, height, Image.SCALE_DEFAULT);
plant = new ImageIcon(smallPlant);
//Herder resize
ImageIcon herder = new ImageIcon(getClass().getResource("/icons/Herder.png"));
Image smallHerder = herder.getImage().getScaledInstance(width, height, Image.SCALE_DEFAULT);
herder = new ImageIcon(smallHerder);
//Set images
for(int i=0; i<labels.length; i++)
{
for(int j=0; j<labels[i].length; j++)
{
if(world.getEntity(i,j) instanceof Carnivore) {
labels[i][j].setIcon(carnivore);
}
else if(world.getEntity(i,j) instanceof Herbivore) {
labels[i][j].setIcon(herbivore);
}
else if(world.getEntity(i,j) instanceof Plant) {
labels[i][j].setIcon(plant);
}
else if(world.getEntity(i,j) instanceof Herder) {
labels[i][j].setIcon(herder);
}
else {
labels[i][j].setIcon(null);
}
}
}
}
/**
* Sets the text for the JLabel that represents the clock cycle
* @param s The current clock cycle
*/
public void setClockCycle(int s){
cycle.setText("Clock cycle: " + s);
}
}