comparison game/Piece.java @ 58:cde170f2f980

Clean up the piece rendering.
author Matti Hamalainen <ccr@tnsp.org>
date Wed, 23 Feb 2011 21:43:07 +0200
parents cc7943cd7f2d
children e9fab3c96043
comparison
equal deleted inserted replaced
57:1435e9d7fd1a 58:cde170f2f980
236 throbTime = (time % 100) / 100.0f; 236 throbTime = (time % 100) / 100.0f;
237 } 237 }
238 238
239 public void paint(Graphics2D g, float x, float y, float dim) 239 public void paint(Graphics2D g, float x, float y, float dim)
240 { 240 {
241 AffineTransform save = g.getTransform();
242
241 // Transform drawing by current angle 243 // Transform drawing by current angle
242 g.rotate(currAngle, x + dim / 2.0f, y + dim / 2.0f); 244 g.rotate(currAngle, x + dim / 2.0f, y + dim / 2.0f);
243 245
244 // Color piece by type 246 // Color piece by type
245 switch (type) { 247 switch (type) {
246 case LOCKED: g.setPaint(Color.green); break; 248 case LOCKED: g.setPaint(new Color(0.3f, 0.8f, 0.3f, 0.35f)); break;
247 case ACTIVE: g.setPaint(Color.red); break; 249 case ACTIVE: g.setPaint(new Color(0.9f, 0.3f, 0.3f, 0.35f)); break;
248 case START: g.setPaint(Color.orange); break; 250 case START: g.setPaint(new Color(1.0f, 0.8f, 0.0f, 0.95f)); break;
249 } 251 }
250 252
251 g.fill(new RoundRectangle2D.Float(x, y, dim, dim, dim / 10, dim / 10)); 253 g.fill(new RoundRectangle2D.Float(x, y, dim, dim, dim / 10, dim / 10));
252 254
253 // Start pieces (center piece) do not have border, etc. 255 // Start pieces (center piece) do not have border, etc.
254 if (type == PieceType.START) 256 if (type != PieceType.START)
255 return; 257 {
256 258 // Active piece has a throbbing "ghost" border
257 // Active piece has a throbbing "ghost" border 259 if (type == PieceType.ACTIVE)
258 if (type == PieceType.ACTIVE) 260 {
259 { 261 float offs1 = throbTime * 10.0f,
260 float offs1 = throbTime * 10.0f, 262 offs2 = throbTime * 20.0f;
261 offs2 = throbTime * 20.0f; 263
262 264 g.setPaint(new Color(0.0f, 0.0f, 0.0f, (float) (1.0f - throbTime) ));
263 g.setPaint(new Color(0.0f, 0.0f, 0.0f, (float) (1.0f - throbTime) )); 265 g.setStroke(new BasicStroke(2.0f + throbTime * 2.0f));
264 g.setStroke(new BasicStroke(2.0f + throbTime * 2.0f)); 266 g.draw(new RoundRectangle2D.Float(
265 g.draw(new RoundRectangle2D.Float( 267 x - offs1, y - offs1,
266 x - offs1, y - offs1, 268 dim + offs2, dim + offs2,
267 dim + offs2, dim + offs2, 269 dim / 10, dim / 10));
268 dim / 10, dim / 10)); 270 }
269 } 271
270 272 // Draw piece border
271 // Draw the connections 273 g.setPaint(new Color(0.0f, 0.0f, 0.0f, 0.6f));
272 g.setStroke(new BasicStroke(5.0f)); 274 g.setStroke(new BasicStroke(5.0f));
273 CubicCurve2D c = new CubicCurve2D.Float(); 275 g.draw(new RoundRectangle2D.Float(x, y, dim, dim, dim / 10, dim / 10));
274 boolean[] drawn = new boolean[numConnections]; 276
275 for (int i = 0; i < numConnections; i++) 277 // Draw the connections
276 if (!drawn[i]) 278 g.setStroke(new BasicStroke(5.5f));
277 { 279 CubicCurve2D curve = new CubicCurve2D.Float();
278 Point2D start, cp1, cp2, end; 280 boolean[] drawn = new boolean[numConnections];
279 boolean isActive = states[i] || states[connections[i]]; 281 for (int i = 0; i < numConnections; i++)
280 282 if (!drawn[i])
281 g.setPaint(isActive ? Color.white : Color.black); 283 {
282 284 Point2D start, cp1, cp2, end;
283 start = getPointCoords(x, y, dim, i); 285 boolean isActive = states[i] || states[connections[i]];
284 end = getPointCoords(x, y, dim, connections[i]); 286
285 287 g.setPaint(isActive ? Color.white : Color.black);
286 cp1 = getPointCoords(x, y, dim, i + 8); 288
287 cp2 = getPointCoords(x, y, dim, connections[i] + 8); 289 start = getPointCoords(x, y, dim, i);
288 290 end = getPointCoords(x, y, dim, connections[i]);
289 c.setCurve(start, cp1, cp2, end); 291 cp1 = getPointCoords(x, y, dim, i + 8);
290 g.draw(c); 292 cp2 = getPointCoords(x, y, dim, connections[i] + 8);
291 293
292 // Mark connection drawn, so we don't overdraw 294 curve.setCurve(start, cp1, cp2, end);
293 drawn[i] = true; 295 g.draw(curve);
294 drawn[connections[i]] = true; 296
295 } 297 // Mark connection drawn, so we don't overdraw
296 298 drawn[i] = true;
297 // Draw piece border 299 drawn[connections[i]] = true;
298 g.setPaint(Color.black); 300 }
299 g.setStroke(new BasicStroke(5.0f)); 301
300 g.draw(new RoundRectangle2D.Float(x, y, dim, dim, dim / 10, dim / 10)); 302 } // !PieceType.START
303
304 g.setTransform(save);
301 } 305 }
302 } 306 }