-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGUI.java
More file actions
128 lines (114 loc) · 3.26 KB
/
GUI.java
File metadata and controls
128 lines (114 loc) · 3.26 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
import java.awt.event.*; //for ActionListener, ActionEvent
import javax.swing.*; //for JFrame, BoxLayout, JLabel, JTextField, JButton
import java.awt.*;
import java.awt.Color;
import javax.swing.BorderFactory;
import javax.swing.JApplet;
import javax.swing.JLabel;
import javax.swing.border.Border;
public class GUI implements ActionListener
{
private JTextField field;
private JFrame frame;
private JLabel[][] all;
public GUI(Cell[][] cells)
{
int numRows = cells.length;
int numCols = cells[0].length;
all = new JLabel[numRows][numCols];
//make a window
frame = new JFrame();
frame.setTitle("Crossword");
//tell window to place each new component under the previous ones
frame.getContentPane().setLayout(new GridLayout(numRows, numCols));
//Border thingy
Border border = BorderFactory.createLineBorder(Color.black);
//add some text
for (int r = 0; r < numRows; r++)
{
for (int c = 0; c < numCols; c++)
{
if (cells[r][c].getBlack())
{
JLabel j = new JLabel(" ");
j.setOpaque(true);
j.setBackground(Color.BLACK);
all[r][c] = j;
frame.getContentPane().add(j);
}
else
{
if (cells[r][c].getValue().equals("-"))
{
JLabel j = new JLabel(" ");
j.setHorizontalAlignment(JLabel.CENTER);
j.setBorder(border);
//j.setOpaque(true);
//j.setBackground(Color.WHITE);
all[r][c] = j;
frame.getContentPane().add(j);
}
else
{
JLabel j = new JLabel(cells[r][c].getValue());
j.setHorizontalAlignment(JLabel.CENTER);
j.setBorder(border);
//j.setBackground(Color.WHITE);
//j.setOpaque(true);
all[r][c] = j;
frame.getContentPane().add(j);
}
}
}
}
//add a text field
//field = new JTextField(25);
//frame.getContentPane().add(field);
//add a button
//JButton button = new JButton("a button");
//frame.getContentPane().add(button);
//tell button to call this object's actionPerformed method when pressed
//button.addActionListener(this);
//store text in button that can be retrieved in actionPerformed method
//button.setActionCommand("button1");
frame.pack(); //determine best size for window
frame.setVisible(true); //show the window
}
public void update(Cell[][] map)
{
int numRows = map.length;
int numCols = map[0].length;
for (int r = 0; r < numRows; r++)
{
for (int c = 0; c < numCols; c++)
{
if (map[r][c].getBlack())
{}
else
{
if (map[r][c].getValue().equals("-"))
{
all[r][c].setText(" ");
}
else
{
all[r][c].setText(map[r][c].getValue());
}
}
}
}
if (Math.random() < 0.000001)
{
try{ Thread.sleep(10);} catch (Exception e){}
}
}
public void actionPerformed(ActionEvent e)
{
//button was pressed
if (e.getActionCommand().equals("button1"))
{
//we now know which button was pressed
System.out.println("text field contains: " + field.getText());
}
}
}