- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBlockDisplay.java
More file actions
Latest commit
176 lines (157 loc) · 4.49 KB
/
Copy pathBlockDisplay.java
File metadata and controls
176 lines (157 loc) · 4.49 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
importjava.awt.*;
importjava.awt.event.*;
importjavax.swing.*;
/**
* Used to display the contents of a game board
*
* @author Jin Kim
* @version 8 December 2015
*/
publicclassBlockDisplayimplementsKeyListener
{
privatestaticfinalColorBACKGROUND = Color.BLACK;
privateMyBoundedGrid<Block> board;
privateJPanel[][] grid;
privateJFrameframe;
privateArrowListenerlistener;
// 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.
*/
publicBlockDisplay(MyBoundedGrid<Block> board)
{
this.board = board;
grid = newJPanel[board.getNumRows()][board.getNumCols()];
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
SwingUtilities.invokeLater(newRunnable()
{
publicvoidrun()
{
createAndShowGUI();
}
});
//Wait until display has been drawn
try
{
while (frame == null || !frame.isVisible())
Thread.sleep(1);
}
catch(InterruptedExceptione)
{
e.printStackTrace();
System.exit(1);
}
}
/**
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event-dispatching thread.
*/
privatevoidcreateAndShowGUI()
{
//Create and set up the window.
frame = newJFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(newGridLayout(board.getNumRows(), board.getNumCols()));
frame.addKeyListener(this);
//Create each square component.
for (introw = 0; row < grid.length; row++)
for (intcol = 0; col < grid[row].length; col++)
{
grid[row][col] = newJPanel();
grid[row][col].setBackground(BACKGROUND);
grid[row][col].setPreferredSize(newDimension(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.
*/
publicvoidshowBlocks()
{
for (introw = 0; row < grid.length; row++)
for (intcol = 0; col < grid[row].length; col++)
{
Locationloc = newLocation(row, col);
Blocksquare = 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.
*/
publicvoidsetTitle(Stringtitle)
{
frame.setTitle(title);
}
/**
* key typed.
*
* @param e key typed by user.
*/
publicvoidkeyTyped(KeyEvente)
{
}
/**
* key released.
*
* @param e key released by user.
*/
publicvoidkeyReleased(KeyEvente)
{
}
/**
* Key pressed.
*
* @param e key released by user.
*/
publicvoidkeyPressed(KeyEvente)
{
if (listener == null)
return;
intcode = e.getKeyCode();
if (code == KeyEvent.VK_LEFT)
listener.leftPressed();
elseif (code == KeyEvent.VK_RIGHT)
listener.rightPressed();
elseif (code == KeyEvent.VK_DOWN)
listener.downPressed();
elseif (code == KeyEvent.VK_UP)
listener.upPressed();
}
/**
* Sets ArrowListener to a new value.
*
* @param newListener new value of ArrowListener.
*/
publicvoidsetArrowListener(ArrowListenernewListener)
{
this.listener = newListener;
}
publicvoidcloseWindow()
{
frame.setVisible(false); //you can't see me!
frame.dispose();
}
}