-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBlockDisplay.java
executable file
·193 lines (174 loc) · 5.5 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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/**
* BlockDisplay is used to display the contents of the
* game board.
*
* @author Dave Feinberg
* @author Richard Page
* @author Susan King Added documentation
* @version May 13, 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.
*
* @param brd the grid on which the game is to be played
*/
public BlockDisplay(MyBoundedGrid<Block> brd)
{
this.board = brd;
grid = new JPanel[board.getNumRows()][board.getNumCols()];
/*
* Schedules a job for the event-dispatching thread, which
* creates and shows this application's GUI.
*/
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
createAndShowGUI();
}
});
/*
* Waits until display has been drawn.
*/
try
{
while (frame == null || !frame.isVisible())
Thread.sleep(1);
}
catch(InterruptedException e)
{
e.printStackTrace();
System.exit(1);
}
}
/**
* Creates the GUI and shows it. For thread safety,
* this method should be invoked from the
* event-dispatching thread.
*/
private void createAndShowGUI()
{
// Creates and sets up the window.
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(new GridLayout(board.getNumRows(), board.getNumCols()));
frame.addKeyListener(this);
// Creates 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));
//JLabel textLabel = new JLabel("" + 123);
//frame.getContentPane().add(textLabel,BorderLayout.CENTER);
//JButton button = new JButton(""+123);
//grid[row][col].add(button);
frame.getContentPane().add(grid[row][col]);
}
// Shows the board.
showBlocks();
// Displays the window.
frame.pack();
frame.setVisible(true);
}
/**
* 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)
{
//JButton button = new JButton("");
//button.setBackground(BACKGROUND);
//button.setBorder(null);
//grid[row][col].add(button);
grid[row][col].setBackground(BACKGROUND);
grid[row][col].setBorder(null);
}
else
{
//JButton button = new JButton(""+123);
//grid[row][col].add(button);
grid[row][col].setBackground(square.getColor());
grid[row][col].setBorder(BorderFactory.createLineBorder(BACKGROUND));
}
}
}
/**
* Sets the title of the window.
*
* @param title the information to be placed at the
* top of the window
*/
public void setTitle(String title)
{
frame.setTitle(title);
}
/**
* Creates a skeleton method to respond when a key is typed in.
* This event occurs when a key press is followed by a key release.
*
* @param e an event which indicates that a keystroke occurred
* in a component (such as the grid)
*/
public void keyTyped(KeyEvent e)
{
}
/**
* Creates a skeleton method to respond when a
* keyboard key is released.
*
* @param e an event which indicates that a keystroke occurred
* in a component (such as the grid)
*/
public void keyReleased(KeyEvent e)
{
}
/**
* Sets up the action when a key is pressed
* on the keyboard.
*
* @param e an event which indicates that a keystroke occurred
* in a component (such as the grid)
*/
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();
}
/**
* Establishes the class that is the ArrowListener.
*
* @param lstener the class that is assigned the task of
* being the ArrowListener
*/
public void setArrowListener(ArrowListener lstener)
{
this.listener = lstener;
}
}