summaryrefslogtreecommitdiffstats
path: root/src/jrummikub/view/impl/StonePainter.java
blob: 68ab72c08e9f03f69f96bd7522bf349f1bbd7fdb (plain)
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
package jrummikub.view.impl;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.Stroke;
import java.awt.geom.GeneralPath;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.util.HashMap;
import java.util.Map;

import jrummikub.model.Position;
import jrummikub.model.Stone;
import jrummikub.model.StoneColor;

/**
 * The StonePainter paints stones and converts between pixel and grid
 * coordinates
 */
class StonePainter {
	private static final float ASPECT_RATIO = 0.75f;
	private static final float DEFAULT_WIDTH = 40;
	private static final float TEXT_POS = 0.275f;
	private static final float FACE_WIDTH = 0.475f;
	private static final float CIRCLE_POS = 0.725f;
	private static final float CIRCLE_WIDTH = 0.45f;

	private static final Color BACKGROUND_COLOR = new Color(0.9f, 0.9f, 0.6f);
	private static final Color SELECTED_COLOR = BACKGROUND_COLOR.darker();

	private static final float BRIGHTER_SCALE = 1.15f;

	private Map<StoneColor, Map<Integer, BufferedImage>> defaultStones;
	private Map<StoneColor, Map<Integer, BufferedImage>> selectedStones;

	/**
	 * The width of one pixel in the scale of 1.0
	 */
	public static final float WIDTH_SCALE = 1 / DEFAULT_WIDTH;
	/**
	 * The height of one pixel in the scale of 1.0
	 */
	public static final float HEIGHT_SCALE = ASPECT_RATIO / DEFAULT_WIDTH;

	private float scale;

	private static int even(float f) {
		return 2 * (int) (f / 2);
	}

	private static Color brighter(Color color) {
		int r = (int) (color.getRed() * BRIGHTER_SCALE);
		int g = (int) (color.getRed() * BRIGHTER_SCALE);
		int b = (int) (color.getRed() * BRIGHTER_SCALE);

		return new Color(r > 255 ? 255 : r, g > 255 ? 255 : g, b > 255 ? 255 : b);
	}

	private static Color getColor(StoneColor color) {
		switch (color) {
			case BLACK:
				return new Color(0.15f, 0.15f, 0.15f);
			case BLUE:
				return new Color(0.0f, 0.0f, 1.0f);
			case ORANGE:
				return new Color(1.0f, 0.6f, 0.0f);
			case RED:
				return new Color(0.9f, 0.0f, 0.25f);
		}

		return null;
	}

	/**
	 * Sets the new grid scale
	 * 
	 * @param scale
	 *          the new scale
	 */
	public void setScale(float scale) {
		this.scale = scale;
		prepaint();
	}

	/**
	 * @return the current grid scale
	 */
	public float getScale() {
		return scale;
	}

	/**
	 * @param x
	 *          x position in screen coordinates
	 * @param y
	 *          y position in screen coordinates
	 * @return position in grid coordinates
	 */
	public Position calculatePosition(int x, int y) {
		float width = getStoneWidth();
		float height = getStoneHeight();

		return new Position(x / width, y / height);
	}

	/**
	 * @return the width of a stone in the current scale in pixels
	 */
	public int getStoneWidth() {
		return even(DEFAULT_WIDTH * scale);
	}

	/**
	 * @return the height of a stone in the current scale in pixels
	 */
	public int getStoneHeight() {
		return (int) (DEFAULT_WIDTH * scale / ASPECT_RATIO);
	}

	private BufferedImage prepaintStone(Color fg, Color bg, int value) {
		Rectangle r = new Rectangle(0, 0, getStoneWidth(), getStoneHeight());
		BufferedImage img = new BufferedImage(r.width, r.height,
				BufferedImage.TYPE_INT_RGB);
		Graphics2D g = img.createGraphics();
		g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
				RenderingHints.VALUE_ANTIALIAS_ON);

		paintStoneBackground(g, r, bg);

		if (value == 0) {
			paintJoker(g, r, fg);
		} else {
			paintStoneValue(g, r, fg, value);
		}

		paintCircle(g, r, bg);

		return img;
	}

	private Map<Integer, BufferedImage> prepaintColor(Color fg, Color bg) {
		Map<Integer, BufferedImage> images = new HashMap<Integer, BufferedImage>();

		for (int i = 0; i <= 13; ++i) {
			images.put(i, prepaintStone(fg, bg, i));
		}

		return images;
	}

	private void prepaint() {
		defaultStones = new HashMap<StoneColor, Map<Integer, BufferedImage>>();
		selectedStones = new HashMap<StoneColor, Map<Integer, BufferedImage>>();

		Color defaultBackground = BACKGROUND_COLOR;
		Color selectedBackground = SELECTED_COLOR;

		for (StoneColor color : StoneColor.values()) {
			Color defaultFg = getColor(color);
			Color selectedFg = defaultFg.darker();

			defaultStones.put(color, prepaintColor(defaultFg, defaultBackground));
			selectedStones.put(color, prepaintColor(selectedFg, selectedBackground));
		}
	}

	/**
	 * @param scale
	 *          the scaling factor for the grid coordinates
	 */
	StonePainter(float scale) {
		setScale(scale);
	}

	private void paintStoneBackground(Graphics2D g, Rectangle r, Color background) {
		// Paint background
		g.setColor(background);
		g.fillRect(r.x, r.y, r.width, r.height);

		// Paint bevel border
		g.setColor(brighter(brighter(background)));
		g.fillRect(r.x, r.y, 1, r.height);
		g.setColor(brighter(background));
		g.fillRect(r.x + 1, r.y + 1, 1, r.height - 2);

		g.setColor(brighter(brighter(background)));
		g.fillRect(r.x, r.y, r.width, 1);
		g.setColor(brighter(background));
		g.fillRect(r.x + 1, r.y + 1, r.width - 2, 1);

		g.setColor(background.darker().darker());
		g.fillRect(r.x + r.width - 1, r.y, 1, r.height);
		g.setColor(background.darker());
		g.fillRect(r.x + r.width - 2, r.y + 1, 1, r.height - 2);

		g.setColor(background.darker().darker());
		g.fillRect(r.x, r.y + r.height - 1, r.width, 1);
		g.setColor(background.darker());
		g.fillRect(r.x + 1, r.y + r.height - 2, r.width - 2, 1);
	}

	private void paintJokerFace(Graphics2D g, Rectangle r) {
		Stroke oldStroke = g.getStroke();

		g.setStroke(new BasicStroke(2));
		g.drawOval(r.x, r.y, r.width, r.height);

		g.setStroke(new BasicStroke(1));
		GeneralPath path = new GeneralPath();
		// nose
		path.moveTo(r.x + 0.5f * r.width, r.y + 0.45f * r.height);
		path.lineTo(r.x + 0.53f * r.width, r.y + 0.6f * r.height);
		path.lineTo(r.x + 0.47f * r.width, r.y + 0.6f * r.height);
		path.closePath();
		g.fill(path);

		path.reset();
		// mouth, left
		path.moveTo(r.x + 0.23f * r.width, r.y + 0.75f * r.width);
		path.lineTo(r.x + 0.27f * r.width, r.y + 0.65f * r.width);
		// mouth, middle
		path.moveTo(r.x + 0.25f * r.width, r.y + 0.7f * r.width);
		path.lineTo(r.x + 0.5f * r.width, r.y + 0.8f * r.width);
		path.lineTo(r.x + 0.75f * r.width, r.y + 0.7f * r.width);
		// mouth, right
		path.moveTo(r.x + 0.77f * r.width, r.y + 0.75f * r.width);
		path.lineTo(r.x + 0.73f * r.width, r.y + 0.65f * r.width);
		g.draw(path);

		path.reset();
		// left eye
		path.moveTo(r.x + 0.3f * r.width, r.y + 0.41f * r.height);
		path.lineTo(r.x + 0.375f * r.width, r.y + 0.375f * r.height);
		path.lineTo(r.x + 0.3f * r.width, r.y + 0.34f * r.height);
		path.lineTo(r.x + 0.225f * r.width, r.y + 0.375f * r.height);
		path.closePath();
		g.draw(path);

		path.reset();
		// right eye
		path.moveTo(r.x + 0.7f * r.width, r.y + 0.41f * r.height);
		path.lineTo(r.x + 0.625f * r.width, r.y + 0.375f * r.height);
		path.lineTo(r.x + 0.7f * r.width, r.y + 0.34f * r.height);
		path.lineTo(r.x + 0.775f * r.width, r.y + 0.375f * r.height);
		path.closePath();
		g.draw(path);

		g.setStroke(oldStroke);
	}

	private void paintJoker(Graphics2D g, Rectangle r, Color color) {
		int faceSize = even(FACE_WIDTH * r.width);
		int pos = r.y + (int) (TEXT_POS * r.height);

		g.setColor(color);
		paintJokerFace(g, new Rectangle(r.x + r.width / 2 - faceSize / 2, pos
				- faceSize / 2, faceSize, faceSize));
	}

	private void paintStoneValue(Graphics2D g, Rectangle r, Color color, int v) {
		int pos = r.y + (int) (TEXT_POS * r.height);

		g.setFont(new Font("SansSerif", Font.BOLD, r.height / 4));
		FontMetrics fm = g.getFontMetrics();
		String value = Integer.toString(v);
		Rectangle2D stringRect = fm.getStringBounds(value, g);

		g.setColor(color.darker());
		g.drawString(value,
				(int) (r.x + r.width / 2 - stringRect.getWidth() / 2) + 1,
				pos + (fm.getAscent() - fm.getDescent()) / 2 + 1);
		g.setColor(color);
		g.drawString(value, (int) (r.x + r.width / 2 - stringRect.getWidth() / 2),
				pos + (fm.getAscent() - fm.getDescent()) / 2);
	}

	private void paintCircle(Graphics2D g, Rectangle r, Color background) {
		int size = even(r.width * CIRCLE_WIDTH);
		int pos = r.y + (int) (CIRCLE_POS * r.height);

		// Paint circle
		g.setColor(background.darker());
		g.drawArc(r.x + r.width / 2 - size / 2, pos - size / 2, size, size, 50, 170);

		g.setColor(brighter(background));
		g.drawArc((int) (r.x + r.width / 2 - size / 2), pos - size / 2, size, size,
				-130, 170);
	}

	/**
	 * Paints a stone
	 * 
	 * @param g
	 *          the graphics context to paint the stone on
	 * @param stone
	 *          the stone to paint
	 * @param p
	 *          the position of the stone
	 * @param selected
	 *          if selected is true the stone will be painted darker
	 */
	public void paintStone(Graphics2D g, Stone stone, Position p, boolean selected) {
		// Color background = selected ? SELECTED_COLOR : BACKGROUND_COLOR;
		int width = getStoneWidth();
		int height = getStoneHeight();
		int x = (int) (p.getX() * width), y = (int) (p.getY() * height);

		if (stone.isJoker()) {
			g.drawImage(
					(selected ? selectedStones : defaultStones).get(stone.getColor())
							.get(0), x, y, null);
		} else {
			g.drawImage(
					(selected ? selectedStones : defaultStones).get(stone.getColor())
							.get(stone.getValue()), x, y, null);
		}
	}
}