-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBlockDisplay.java
176 lines (157 loc) · 4.49 KB
/
BlockDisplay.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
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/**
* Used to display the contents of a game board
*
* @author Jin Kim
* @version 8 December 2015
*/
public class BlockDisplay implements KeyListener
{
private static final Color BACKGROUND = Color.BLACK;
private MyBoundedGrid<Block> board;
private JPanel[][] grid;
private JFrame frame;
private ArrowListener listener;
// Constructs a new display for displaying the given board
/**
* Constructs a new display for displaying the given board.
*
* @param board grid where the blocks will be shown.
*/
public BlockDisplay(MyBoundedGrid<Block> board)
{
this.board = board;
grid = new JPanel[board.getNumRows()][board.getNumCols()];
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
createAndShowGUI();
}
});
//Wait until display has been drawn
try
{
while (frame == null || !frame.isVisible())
Thread.sleep(1);
}
catch(InterruptedException e)
{
e.printStackTrace();
System.exit(1);
}
}
/**
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event-dispatching thread.
*/
private void createAndShowGUI()
{
//Create and set up the window.
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(new GridLayout(board.getNumRows(), board.getNumCols()));
frame.addKeyListener(this);
//Create each square component.
for (int row = 0; row < grid.length; row++)
for (int col = 0; col < grid[row].length; col++)
{
grid[row][col] = new JPanel();
grid[row][col].setBackground(BACKGROUND);
grid[row][col].setPreferredSize(new Dimension(20, 20));
frame.getContentPane().add(grid[row][col]);
}
//Show the board
showBlocks();
//Display the window.
frame.pack();
frame.setVisible(true);
}
//Redraws the board to include the pieces and border colors.
/**
* Redraws the board to include the pieces and border colors.
*/
public void showBlocks()
{
for (int row = 0; row < grid.length; row++)
for (int col = 0; col < grid[row].length; col++)
{
Location loc = new Location(row, col);
Block square = board.get(loc);
if (square == null)
{
grid[row][col].setBackground(BACKGROUND);
grid[row][col].setBorder(null);
}
else
{
grid[row][col].setBackground(square.getColor());
grid[row][col].setBorder(BorderFactory.createLineBorder(BACKGROUND));
}
}
}
// Sets the title of the window.
/**
* sets the title of the window.
*
* @param title title of the window.
*/
public void setTitle(String title)
{
frame.setTitle(title);
}
/**
* key typed.
*
* @param e key typed by user.
*/
public void keyTyped(KeyEvent e)
{
}
/**
* key released.
*
* @param e key released by user.
*/
public void keyReleased(KeyEvent e)
{
}
/**
* Key pressed.
*
* @param e key released by user.
*/
public void keyPressed(KeyEvent e)
{
if (listener == null)
return;
int code = e.getKeyCode();
if (code == KeyEvent.VK_LEFT)
listener.leftPressed();
else if (code == KeyEvent.VK_RIGHT)
listener.rightPressed();
else if (code == KeyEvent.VK_DOWN)
listener.downPressed();
else if (code == KeyEvent.VK_UP)
listener.upPressed();
}
/**
* Sets ArrowListener to a new value.
*
* @param newListener new value of ArrowListener.
*/
public void setArrowListener(ArrowListener newListener)
{
this.listener = newListener;
}
public void closeWindow()
{
frame.setVisible(false); //you can't see me!
frame.dispose();
}
}