1 package net.sourceforge.simplegamenet.connectaline;
2
3 import java.awt.*;
4 import java.awt.event.MouseEvent;
5 import java.awt.event.MouseListener;
6 import javax.swing.*;
7
8
9 public class CALPlayFieldPanel extends JPanel implements MouseListener,
10 CALPacketCodes,
11 CALPlayStyle {
12 private int cALNumberOfRows;
13 private int cALNumberOfColumns;
14 private int cALPlayStyle;
15 private int cellWidth;
16 private int cellHeight;
17 private ImageIcon squareIcon;
18 private ImageIcon squareColoredIcon;
19 private CALPlayerClient cALPlayerClient;
20 private int[] coordinates = new int[2];
21 private boolean highlight = false;
22
23 public CALPlayFieldPanel(CALPlayerClient cALPlayerClient, CALSettings cALSettings) {
24 this.cALPlayerClient = cALPlayerClient;
25 this.cALNumberOfColumns = cALSettings.getNumberOfColumns();
26 this.cALNumberOfRows = cALSettings.getNumberOfRows();
27 this.cALPlayStyle = cALSettings.getPlayStyle();
28 setOpaque(false);
29 squareIcon = new ImageIcon(getClass().getResource("CALFrame.png"));
30 squareColoredIcon = new ImageIcon(getClass().getResource("CALColors.png"));
31 addMouseListener(this);
32 }
33
34 public void paintComponent(Graphics g) {
35 Dimension size = getSize();
36 this.cellWidth = size.width / (cALNumberOfColumns + 2);
37 this.cellHeight = size.height / (cALNumberOfRows + 2);
38 CALPlayField cALPlayField = cALPlayerClient.getCALPlayField();
39 Integer[] cALParticipantsOrder = cALPlayerClient.getParticipantsOrder();
40 int[] colorIndexArray = cALPlayerClient.getColorIndexArray();
41 for (int i = 0; i < cALNumberOfColumns + 2; i++) {
42 for (int j = 0; j < cALNumberOfRows + 2; j++) {
43 if (i == 0 && j == 0) {
44
45 g.drawImage(squareIcon.getImage(), i * cellWidth, j * cellHeight,
46 (i + 1) * cellWidth, (j + 1) * cellHeight,
47 0, 0, 100, 100, this);
48 } else if (i == 0 && j != 0 && j != cALNumberOfRows + 1) {
49
50 g.drawImage(squareIcon.getImage(), i * cellWidth, j * cellHeight,
51 (i + 1) * cellWidth, (j + 1) * cellHeight,
52 0, 100, 100, 200, this);
53 } else if (i == 0 && j == cALNumberOfRows + 1) {
54
55 g.drawImage(squareIcon.getImage(), i * cellWidth, j * cellHeight,
56 (i + 1) * cellWidth, (j + 1) * cellHeight,
57 0, 200, 100, 300, this);
58 } else if (i != 0 && i != cALNumberOfColumns + 1 && j == 0) {
59
60 g.drawImage(squareIcon.getImage(), i * cellWidth, j * cellHeight,
61 (i + 1) * cellWidth, (j + 1) * cellHeight,
62 100, 0, 200, 100, this);
63 } else if (i != 0 && i != cALNumberOfColumns + 1
64 && j != 0 && j != cALNumberOfRows + 1
65 && cALPlayerClient.isGameStarted()
66 && cALPlayField.isOccupied(i - 1, j - 1)) {
67
68 for (int k = 0; k < cALParticipantsOrder.length; k++) {
69 if (cALParticipantsOrder[k].equals(
70 cALPlayField.getSquarePlayerID(i - 1, j - 1))) {
71 g.drawImage(squareColoredIcon.getImage(), i * cellWidth,
72 j * cellHeight, (i + 1) * cellWidth,
73 (j + 1) * cellHeight, (colorIndexArray[k] + 1) * 100, 0,
74 (colorIndexArray[k] + 2) * 100, 100, this);
75 break;
76 }
77 }
78 } else if (i != 0 && i != cALNumberOfColumns + 1
79 && j != 0 && j != cALNumberOfRows + 1) {
80
81 g.drawImage(squareColoredIcon.getImage(), i * cellWidth, j * cellHeight,
82 (i + 1) * cellWidth, (j + 1) * cellHeight,
83 0, 0, 100, 100, this);
84 } else if (i != 0 && i != cALNumberOfColumns + 1
85 && j == cALNumberOfRows + 1) {
86
87 g.drawImage(squareIcon.getImage(), i * cellWidth, j * cellHeight,
88 (i + 1) * cellWidth, (j + 1) * cellHeight,
89 100, 200, 200, 300, this);
90 } else if (i == cALNumberOfColumns + 1 && j == 0) {
91
92 g.drawImage(squareIcon.getImage(), i * cellWidth, j * cellHeight,
93 (i + 1) * cellWidth, (j + 1) * cellHeight,
94 200, 0, 300, 100, this);
95 } else if (i == cALNumberOfColumns + 1 && j != 0 && j != cALNumberOfRows + 1) {
96
97 g.drawImage(squareIcon.getImage(), i * cellWidth, j * cellHeight,
98 (i + 1) * cellWidth, (j + 1) * cellHeight,
99 200, 100, 300, 200, this);
100 } else if (i == cALNumberOfColumns + 1 && j == cALNumberOfRows + 1) {
101
102 g.drawImage(squareIcon.getImage(), i * cellWidth, j * cellHeight,
103 (i + 1) * cellWidth, (j + 1) * cellHeight,
104 200, 200, 300, 300, this);
105 }
106 }
107 }
108 }
109
110 public int getCellWidth() {
111 return cellWidth;
112 }
113
114 public int getCellHeight() {
115 return cellHeight;
116 }
117
118 public void cALSettingsUpdated(CALSettings cALSettings) {
119 this.cALNumberOfColumns = cALSettings.getNumberOfColumns();
120 this.cALNumberOfRows = cALSettings.getNumberOfRows();
121 this.cALPlayStyle = cALSettings.getPlayStyle();
122 repaint();
123 }
124
125 public void setEnabled(boolean enabled) {
126 super.setEnabled(enabled);
127 }
128
129 public void updateMove() {
130 repaint();
131 }
132
133 /***
134 * Invoked when the mouse button has been clicked (pressed and released) on a component.
135 */
136
137 public void mouseClicked(MouseEvent e) {
138 }
139
140 /***
141 * Invoked when the mouse enters a component.
142 */
143
144 public void mouseEntered(MouseEvent e) {
145 }
146
147 /***
148 * Invoked when the mouse exits a component.
149 */
150 public void mouseExited(MouseEvent e) {
151 }
152
153 /***
154 * Invoked when a mouse button has been pressed on a component.
155 */
156 public void mousePressed(MouseEvent e) {
157 if (isEnabled()) {
158 if (e.getX() > cellWidth && e.getX() < cellWidth * (cALNumberOfColumns + 1)
159 && e.getY() > cellHeight && e.getY() < cellHeight * (cALNumberOfRows + 1)) {
160 int[] coordinates = new int[2];
161 for (int i = 0; i <= cALNumberOfColumns; i++) {
162 if (e.getX() > (i * cellWidth)) {
163 coordinates[0] = i - 1;
164 }
165 }
166 for (int j = 0; j <= cALNumberOfRows; j++) {
167 if (e.getY() > (j * cellHeight)) {
168 coordinates[1] = j - 1;
169 }
170 }
171 CALPlayField cALPlayField = cALPlayerClient.getCALPlayField();
172 switch (cALPlayStyle) {
173 case GRAVITY:
174 if (cALPlayField.isMoveAllowedGravity(coordinates[0], coordinates[1])
175 && cALPlayField != null) {
176 CALPacket cAlPacket = new CALPacket(RESULT_TURN_BY_PLAYER,
177 coordinates);
178 cALPlayerClient.sendCALPacket(cAlPacket);
179 setEnabled(false);
180 }
181 break;
182 case ANY_EMPTY_SPOT:
183 if (cALPlayField.isMoveAllowedAnyEmptySpot(coordinates[0],
184 coordinates[1])
185 && cALPlayField != null) {
186 CALPacket cAlPacket = new CALPacket(RESULT_TURN_BY_PLAYER,
187 coordinates);
188 cALPlayerClient.sendCALPacket(cAlPacket);
189 setEnabled(false);
190 }
191 break;
192 }
193 }
194 }
195 }
196
197 /***
198 * Invoked when a mouse button has been released on a component.
199 */
200 public void mouseReleased(MouseEvent e) {
201 }
202
203 /***
204 * Invoked when a mouse button is pressed on a component and then dragged.
205 * <code>MOUSE_DRAGGED</code> events will continue to be delivered to the component where the
206 * drag originated until the mouse button is released (regardless of whether the mouse position
207 * is within the bounds of the component).
208 * <p/>
209 * Due to platform-dependent Drag&Drop implementations, <code>MOUSE_DRAGGED</code> events may
210 * not be delivered during a native Drag&Drop operation.
211 */
212 public void mouseDragged(MouseEvent e) {
213 }
214
215 /*** Invoked when the mouse cursor has been moved onto a component
216 * but no buttons have been pushed.
217 *
218 */
219 }