-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathCardGUI.java
126 lines (98 loc) · 2.55 KB
/
CardGUI.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
import java.awt.*;
import javax.swing.*;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
public class CardGUI extends JPanel {
JLabel [][] moves = new JLabel[5][5];
JLabel name;
JPanel movePanel;
int color;
GridBagLayout gridbag = new GridBagLayout();
GridBagConstraints gbc = new GridBagConstraints();
public CardGUI(CardGUI cg) {
this(cg, cg.getColor());
}
public CardGUI(CardGUI cg, int color) {
init();
this.color = color;
set(cg);
}
public CardGUI(Card c, int color) {
init();
this.color = color;
name.setText(c.getName());
for(int[] m : c.getMoves()) {
int x = 2+m[0]*color*-1, y = 2+m[1]*color;
moves[y][x].setBackground(Color.YELLOW);
}
gbc = new GridBagConstraints();
gbc.gridwidth = 2;
gbc.gridheight = 1;
gridbag.setConstraints(movePanel,gbc);
add(movePanel);
}
private void init() {
setBackground(Color.WHITE);
gridbag = new GridBagLayout();
gbc = new GridBagConstraints();
name = new JLabel("");
setLayout(gridbag);
movePanel = new JPanel();
movePanel.setLayout(new GridLayout(5,5));
gbc = new GridBagConstraints();
gbc.gridwidth = 1;
gbc.gridheight = 1;
gridbag.setConstraints(name,gbc);
add(name);
for(int y = 0; y < 5; y++)
for(int x = 0; x < 5; x++) {
int width = 15, height = 15;
moves[y][x] = new JLabel(" ");
moves[y][x].setBackground(Color.WHITE);
moves[y][x].setOpaque(true);
moves[y][x].setBorder(BorderFactory.createLineBorder(Color.BLACK));
moves[y][x].setMinimumSize(new Dimension(width, height));
moves[y][x].setPreferredSize(new Dimension(width, height));
moves[y][x].setMaximumSize(new Dimension(width, height));
movePanel.add(moves[y][x]);
}
moves[2][2].setBackground(Color.BLACK);
}
public void setText(String n) {
name.setText(n);
}
public void setMoves(JLabel [][] mp) {
for(int y = 0; y < 5; y++)
for(int x = 0; x < 5; x++)
moves[y][x].setBackground(mp[y][x].getBackground());
}
public void swap(JLabel [][] mp) {
for(int y = 0; y < 5; y++)
for(int x = 0; x < 5; x++)
moves[y][x].setBackground(mp[4-y][4-x].getBackground());
}
public JLabel[][] getMoves() {
return moves;
}
public String getText() {
return name.getText();
}
public void set(CardGUI cg, int color) {
set(cg);
this.color = color;
}
public void set(CardGUI cg) {
setText(cg.getText());
if(color != cg.getColor())
swap(cg.getMoves());
else
setMoves(cg.getMoves());
color = cg.getColor();
}
public int getColor() {
return color;
}
public String getName() {
return name.getText();
}
}