-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLightsPanel.java
90 lines (78 loc) · 2.05 KB
/
LightsPanel.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
package berger_may_muehlehner_lights_out;
import java.awt.*;
import javax.swing.*;
/**
* Optische Oberflaeche des Programms
*
* @author benedikt berger
* @version 1.0
*/
public class LightsPanel extends JPanel {
/* Groesse der Spielflaeche */
private static int amount = 5;
private static final long serialVersionUID = 1L;
private JButton[][] lights;
private JButton reset;
private JPanel buttons, bottom;
private LightsController control;
/**
* Konstruktor
*
* @param controller
* Controller mit der Funktion als ActionListener
*/
public LightsPanel(LightsController controller) {
this.control = controller;
buttons = new JPanel();
bottom = new JPanel();
this.setLayout(new BorderLayout());
buttons.setLayout(new GridLayout(amount, amount, 5, 5));
bottom.setLayout(new BorderLayout());
lights = new JButton[amount][amount];
/*
* Lights werden initialisiert
*/
for (int x = 0; x < amount; x++) {
for (int y = 0; y < amount; y++) {
lights[x][y] = new JButton();
lights[x][y].setBackground(Color.BLACK);
lights[x][y].setOpaque(true);
lights[x][y].setBorderPainted(false);
lights[x][y].addActionListener(control);
lights[x][y].setActionCommand(y + " " + x);
buttons.add(lights[x][y], BorderLayout.CENTER);
}
}
reset = new JButton("Reset");
reset.addActionListener(control);
reset.setActionCommand("reset");
bottom.add(reset, BorderLayout.CENTER);
this.add(bottom, BorderLayout.SOUTH);
this.add(buttons, BorderLayout.CENTER);
}
/**
* Ermoeglicht das aktivieren und deaktivieren von bestimmten Lights
*
* @param x
* Koordinate fuer das Array
* @param y
* Koordinate fuer das Array
* @param status
* an oder aus
*/
public void control(int x, int y, boolean status) {
if (status)
lights[y][x].setBackground(Color.YELLOW);
else
lights[y][x].setBackground(Color.BLACK);
}
/**
* Getter fuer Lights
*
* @return lights Array der Buttons
* @return lights
*/
public JButton[][] getLight() {
return lights;
}
}