Mercurial > hg > ristipolku
comparison game/Engine.java @ 134:4c0dec72e2f0
Whitespace cosmetic cleanup.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Thu, 24 Nov 2011 21:51:54 +0200 |
parents | 881deac2daf8 |
children | a33fdb1de11c |
comparison
equal
deleted
inserted
replaced
133:881deac2daf8 | 134:4c0dec72e2f0 |
---|---|
27 | 27 |
28 IDMPoint currPos, currOffs; | 28 IDMPoint currPos, currOffs; |
29 Paint textPaint; | 29 Paint textPaint; |
30 Font textFont; | 30 Font textFont; |
31 FontMetrics textMetrics; | 31 FontMetrics textMetrics; |
32 | 32 |
33 | 33 |
34 public AboutBox() | 34 public AboutBox() |
35 { | 35 { |
36 try { | 36 try { |
37 ResourceLoader res = new ResourceLoader("graphics/girl.jpg"); | 37 ResourceLoader res = new ResourceLoader("graphics/girl.jpg"); |
40 catch (IOException e) | 40 catch (IOException e) |
41 { | 41 { |
42 } | 42 } |
43 | 43 |
44 aboutSecret = false; | 44 aboutSecret = false; |
45 | 45 |
46 setPos(150f, 150f); | 46 setPos(150f, 150f); |
47 setSize(675f, 420f); | 47 setSize(675f, 420f); |
48 } | 48 } |
49 | 49 |
50 public void setTextFont(Font font, FontMetrics metrics) | 50 public void setTextFont(Font font, FontMetrics metrics) |
51 { | 51 { |
52 textFont = font; | 52 textFont = font; |
53 textMetrics = metrics; | 53 textMetrics = metrics; |
54 } | 54 } |
55 | 55 |
56 public void setTextPaint(Paint paint) | 56 public void setTextPaint(Paint paint) |
57 { | 57 { |
58 textPaint = paint; | 58 textPaint = paint; |
59 } | 59 } |
60 | 60 |
61 public void setCurrPos(IDMPoint npos) | 61 public void setCurrPos(IDMPoint npos) |
62 { | 62 { |
63 currPos = npos; | 63 currPos = npos; |
64 currOffs = new IDMPoint(0, 0); | 64 currOffs = new IDMPoint(0, 0); |
65 } | 65 } |
66 | 66 |
67 public void setCurrPos(float x, float y) | 67 public void setCurrPos(float x, float y) |
68 { | 68 { |
69 setCurrPos(new IDMPoint(x, y)); | 69 setCurrPos(new IDMPoint(x, y)); |
70 } | 70 } |
71 | 71 |
72 public void drawString(Graphics2D g, String text) | 72 public void drawString(Graphics2D g, String text) |
73 { | 73 { |
74 Paint savePaint = g.getPaint(); | 74 Paint savePaint = g.getPaint(); |
75 g.setPaint(textPaint); | 75 g.setPaint(textPaint); |
76 g.setFont(textFont); | 76 g.setFont(textFont); |
77 | 77 |
78 int i = 0; | 78 int i = 0; |
79 while (i < text.length()) | 79 while (i < text.length()) |
80 { | 80 { |
81 int p = text.indexOf("\n", i); | 81 int p = text.indexOf("\n", i); |
82 boolean linefeed; | 82 boolean linefeed; |
93 i += str.length(); | 93 i += str.length(); |
94 linefeed = false; | 94 linefeed = false; |
95 } | 95 } |
96 | 96 |
97 g.drawString(str, currPos.x + currOffs.x, currPos.y + currOffs.y); | 97 g.drawString(str, currPos.x + currOffs.x, currPos.y + currOffs.y); |
98 | 98 |
99 if (linefeed) | 99 if (linefeed) |
100 { | 100 { |
101 currOffs.x = 0; | 101 currOffs.x = 0; |
102 currOffs.y += textMetrics.getHeight(); | 102 currOffs.y += textMetrics.getHeight(); |
103 } | 103 } |
118 g.setPaint(new Color(0.0f, 0.0f, 0.0f, 0.7f)); | 118 g.setPaint(new Color(0.0f, 0.0f, 0.0f, 0.7f)); |
119 g.fill(new RoundRectangle2D.Float(x, y, w, h, 10, 10)); | 119 g.fill(new RoundRectangle2D.Float(x, y, w, h, 10, 10)); |
120 | 120 |
121 setTextFont(G.fonts[3], G.metrics[3]); | 121 setTextFont(G.fonts[3], G.metrics[3]); |
122 setTextPaint(Color.white); | 122 setTextPaint(Color.white); |
123 | 123 |
124 setCurrPos(x + 20, y + 30); | 124 setCurrPos(x + 20, y + 30); |
125 drawString(g, "RistiPolku (CrossPaths) v"+ G.version +"\n"); | 125 drawString(g, "RistiPolku (CrossPaths) v"+ G.version +"\n"); |
126 | 126 |
127 setTextFont(G.fonts[1], G.metrics[1]); | 127 setTextFont(G.fonts[1], G.metrics[1]); |
128 if (aboutSecret) | 128 if (aboutSecret) |
129 { | 129 { |
130 g.drawImage(aboutImg, x + 20, y + 55, | 130 g.drawImage(aboutImg, x + 20, y + 55, |
131 aboutImg.getWidth(), aboutImg.getHeight(), null); | 131 aboutImg.getWidth(), aboutImg.getHeight(), null); |
132 | 132 |
133 setCurrPos(x + 225, y + 75); | 133 setCurrPos(x + 225, y + 75); |
134 drawString(g, | 134 drawString(g, |
135 "Dedicated to my\n" + | 135 "Dedicated to my\n" + |
136 "favorite woman\n" + | 136 "favorite woman\n" + |
137 "in the world."); | 137 "in the world."); |
138 | 138 |
139 setCurrPos(x + 370, y + 175); | 139 setCurrPos(x + 370, y + 175); |
140 drawString(g, "- Matti"); | 140 drawString(g, "- Matti"); |
141 } | 141 } |
142 else | 142 else |
143 { | 143 { |
151 | 151 |
152 setTextPaint(Color.red); | 152 setTextPaint(Color.red); |
153 drawString(g, "Controls:\n"); | 153 drawString(g, "Controls:\n"); |
154 | 154 |
155 IDMPoint old = currOffs.copy(); | 155 IDMPoint old = currOffs.copy(); |
156 | 156 |
157 setTextPaint(Color.white); | 157 setTextPaint(Color.white); |
158 drawString(g, | 158 drawString(g, |
159 "Arrow keys / mouse wheel\n"+ | 159 "Arrow keys / mouse wheel\n"+ |
160 "Enter / mouse click\n"+ | 160 "Enter / mouse click\n"+ |
161 "Space bar\n"); | 161 "Space bar\n"); |
164 currOffs.y = old.y; | 164 currOffs.y = old.y; |
165 drawString(g, | 165 drawString(g, |
166 "- Rotate piece\n" + | 166 "- Rotate piece\n" + |
167 "- Place/lock piece\n" + | 167 "- Place/lock piece\n" + |
168 "- Swap piece\n"); | 168 "- Swap piece\n"); |
169 | 169 |
170 currPos.x -= 330; | 170 currPos.x -= 330; |
171 setTextPaint(Color.green); | 171 setTextPaint(Color.green); |
172 drawString(g, | 172 drawString(g, |
173 "\nObjective: Create a path long as possible by rotating\n"+ | 173 "\nObjective: Create a path long as possible by rotating\n"+ |
174 "and placing pieces. More points will be awarded for\n"+ | 174 "and placing pieces. More points will be awarded for\n"+ |
188 clicked(); | 188 clicked(); |
189 aboutSecret = false; | 189 aboutSecret = false; |
190 } | 190 } |
191 return true; | 191 return true; |
192 } | 192 } |
193 | 193 |
194 public void clicked() | 194 public void clicked() |
195 { | 195 { |
196 parent.remove(this); | 196 parent.remove(this); |
197 } | 197 } |
198 } | 198 } |
202 { | 202 { |
203 static final int boardSize = 9; | 203 static final int boardSize = 9; |
204 static final int boardMiddle = 4; | 204 static final int boardMiddle = 4; |
205 Piece[][] board; | 205 Piece[][] board; |
206 float pscale, ptime; | 206 float pscale, ptime; |
207 | 207 |
208 public boolean flagGameOver; | 208 public boolean flagGameOver; |
209 int gameScore; | 209 int gameScore; |
210 | 210 |
211 Piece currPiece, nextPiece; | 211 Piece currPiece, nextPiece; |
212 int currX, currY, currPoint; | 212 int currX, currY, currPoint; |
213 | 213 |
214 Sound sndPlaced; | 214 Sound sndPlaced; |
215 | 215 |
216 private final ReentrantReadWriteLock pointLock = new ReentrantReadWriteLock(); | 216 private final ReentrantReadWriteLock pointLock = new ReentrantReadWriteLock(); |
217 private ArrayList<AnimatedPointElement> pointElems; | 217 private ArrayList<AnimatedPointElement> pointElems; |
218 | 218 |
219 public GameBoard(IDMPoint pos, float ps) | 219 public GameBoard(IDMPoint pos, float ps) |
220 { | 220 { |
221 super(pos); | 221 super(pos); |
222 pscale = ps; | 222 pscale = ps; |
223 | 223 |
225 | 225 |
226 pointElems = new ArrayList<AnimatedPointElement>(); | 226 pointElems = new ArrayList<AnimatedPointElement>(); |
227 | 227 |
228 startNewGame(); | 228 startNewGame(); |
229 } | 229 } |
230 | 230 |
231 public void startNewGame() | 231 public void startNewGame() |
232 { | 232 { |
233 | 233 |
234 board = new Piece[boardSize][boardSize]; | 234 board = new Piece[boardSize][boardSize]; |
235 board[boardMiddle][boardMiddle] = new Piece(PieceType.START); | 235 board[boardMiddle][boardMiddle] = new Piece(PieceType.START); |
236 | 236 |
237 currX = boardMiddle; | 237 currX = boardMiddle; |
238 currY = boardMiddle; | 238 currY = boardMiddle; |
252 g.setStroke(new BasicStroke(5.0f)); | 252 g.setStroke(new BasicStroke(5.0f)); |
253 g.draw(new RoundRectangle2D.Float(getScaledX(), getScaledY(), | 253 g.draw(new RoundRectangle2D.Float(getScaledX(), getScaledY(), |
254 boardSize * pscale, boardSize * pscale, | 254 boardSize * pscale, boardSize * pscale, |
255 pscale / 5, pscale / 5)); | 255 pscale / 5, pscale / 5)); |
256 } | 256 } |
257 | 257 |
258 public void paint(Graphics2D g) | 258 public void paint(Graphics2D g) |
259 { | 259 { |
260 for (int y = 0; y < boardSize; y++) | 260 for (int y = 0; y < boardSize; y++) |
261 for (int x = 0; x < boardSize; x++) | 261 for (int x = 0; x < boardSize; x++) |
262 if (board[x][y] != null) | 262 if (board[x][y] != null) |
278 } | 278 } |
279 finally | 279 finally |
280 { | 280 { |
281 read.unlock(); | 281 read.unlock(); |
282 } | 282 } |
283 | 283 |
284 | 284 |
285 if (!flagGameOver) | 285 if (!flagGameOver) |
286 { | 286 { |
287 if (nextPiece != null) | 287 if (nextPiece != null) |
288 { | 288 { |
304 | 304 |
305 double f = Math.sin(ptime * 0.1) * 4.0; | 305 double f = Math.sin(ptime * 0.1) * 4.0; |
306 g.setPaint(Color.white); | 306 g.setPaint(Color.white); |
307 g.drawString(text, (G.screenDim.width - textWidth) / 2 + (float) f, G.screenDim.height / 2 + (float) f); | 307 g.drawString(text, (G.screenDim.width - textWidth) / 2 + (float) f, G.screenDim.height / 2 + (float) f); |
308 } | 308 } |
309 | 309 |
310 // Score | 310 // Score |
311 g.setFont(G.fonts[2]); | 311 g.setFont(G.fonts[2]); |
312 g.setPaint(Color.white); | 312 g.setPaint(Color.white); |
313 g.drawString(""+ String.format("%05d", gameScore), G.screenDim.width - 230, 220); | 313 g.drawString(""+ String.format("%05d", gameScore), G.screenDim.width - 230, 220); |
314 | 314 |
315 } | 315 } |
316 | 316 |
317 public boolean contains(float x, float y) | 317 public boolean contains(float x, float y) |
318 { | 318 { |
319 return (x >= getScaledX() && | 319 return (x >= getScaledX() && |
320 y >= getScaledY() && | 320 y >= getScaledY() && |
321 x < getScaledX() + boardSize * pscale && | 321 x < getScaledX() + boardSize * pscale && |
322 y < getScaledY() + boardSize * pscale); | 322 y < getScaledY() + boardSize * pscale); |
323 } | 323 } |
324 | 324 |
325 public void animate(float time) | 325 public void animate(float time) |
326 { | 326 { |
327 ptime = time; | 327 ptime = time; |
328 for (int y = 0; y < boardSize; y++) | 328 for (int y = 0; y < boardSize; y++) |
329 for (int x = 0; x < boardSize; x++) | 329 for (int x = 0; x < boardSize; x++) |
330 if (board[x][y] != null) | 330 if (board[x][y] != null) |
331 { | 331 { |
332 board[x][y].animate(time); | 332 board[x][y].animate(time); |
333 } | 333 } |
334 | 334 |
335 Lock write = pointLock.writeLock(); | 335 Lock write = pointLock.writeLock(); |
336 write.lock(); | 336 write.lock(); |
337 try | 337 try |
338 { | 338 { |
339 ArrayList<AnimatedPointElement> tmp = new ArrayList<AnimatedPointElement>(); | 339 ArrayList<AnimatedPointElement> tmp = new ArrayList<AnimatedPointElement>(); |
342 { | 342 { |
343 elem.animate(time); | 343 elem.animate(time); |
344 if (elem.active) | 344 if (elem.active) |
345 tmp.add(elem); | 345 tmp.add(elem); |
346 } | 346 } |
347 | 347 |
348 pointElems = tmp; | 348 pointElems = tmp; |
349 } | 349 } |
350 finally | 350 finally |
351 { | 351 { |
352 write.unlock(); | 352 write.unlock(); |
353 } | 353 } |
354 } | 354 } |
355 | 355 |
356 public void pieceRotate(Piece.RotateDir dir) | 356 public void pieceRotate(Piece.RotateDir dir) |
357 { | 357 { |
358 if (currPiece != null && !flagGameOver) | 358 if (currPiece != null && !flagGameOver) |
359 { | 359 { |
360 currPiece.rotate(dir); | 360 currPiece.rotate(dir); |
385 { | 385 { |
386 currPiece = nextPiece; | 386 currPiece = nextPiece; |
387 currPiece.changed(); | 387 currPiece.changed(); |
388 nextPiece = new Piece(PieceType.ACTIVE); | 388 nextPiece = new Piece(PieceType.ACTIVE); |
389 } | 389 } |
390 | 390 |
391 public void pieceSwapCurrent() | 391 public void pieceSwapCurrent() |
392 { | 392 { |
393 if (!flagGameOver) | 393 if (!flagGameOver) |
394 { | 394 { |
395 Piece tmp = currPiece; | 395 Piece tmp = currPiece; |
398 board[currX][currY] = currPiece; | 398 board[currX][currY] = currPiece; |
399 currPiece.changed(); | 399 currPiece.changed(); |
400 nextPiece.changed(); | 400 nextPiece.changed(); |
401 } | 401 } |
402 } | 402 } |
403 | 403 |
404 // Check one piece, set connections, find the new placement | 404 // Check one piece, set connections, find the new placement |
405 // based on piece rotations etc. | 405 // based on piece rotations etc. |
406 private boolean pieceCheck(Piece piece) | 406 private boolean pieceCheck(Piece piece) |
407 { | 407 { |
408 if (piece == null) | 408 if (piece == null) |
457 | 457 |
458 if (currPiece != null) | 458 if (currPiece != null) |
459 { | 459 { |
460 G.smgr.play(sndPlaced); | 460 G.smgr.play(sndPlaced); |
461 } | 461 } |
462 | 462 |
463 while (!finished) | 463 while (!finished) |
464 { | 464 { |
465 if (currX >= 0 && currX < boardSize && currY >= 0 && currY < boardSize) | 465 if (currX >= 0 && currX < boardSize && currY >= 0 && currY < boardSize) |
466 { | 466 { |
467 connections++; | 467 connections++; |
468 finished = pieceCheck(board[currX][currY]); | 468 finished = pieceCheck(board[currX][currY]); |
469 | 469 |
470 if (!finished) | 470 if (!finished) |
471 { | 471 { |
472 Lock write = pointLock.writeLock(); | 472 Lock write = pointLock.writeLock(); |
473 write.lock(); | 473 write.lock(); |
474 try | 474 try |
490 // Outside of the board, game over | 490 // Outside of the board, game over |
491 finished = true; | 491 finished = true; |
492 flagGameOver = true; | 492 flagGameOver = true; |
493 } | 493 } |
494 } | 494 } |
495 | 495 |
496 // Compute and add score | 496 // Compute and add score |
497 gameScore += connections * connections; | 497 gameScore += connections * connections; |
498 | 498 |
499 // If game over, clear the game | 499 // If game over, clear the game |
500 if (flagGameOver) | 500 if (flagGameOver) |
501 { | 501 { |
502 currPiece = null; | 502 currPiece = null; |
503 } | 503 } |
512 else | 512 else |
513 pieceRotate(Piece.RotateDir.RIGHT); | 513 pieceRotate(Piece.RotateDir.RIGHT); |
514 | 514 |
515 return true; | 515 return true; |
516 } | 516 } |
517 | 517 |
518 public void clicked() | 518 public void clicked() |
519 { | 519 { |
520 if (!flagGameOver) | 520 if (!flagGameOver) |
521 pieceFinishTurn(); | 521 pieceFinishTurn(); |
522 } | 522 } |
564 | 564 |
565 public Engine() | 565 public Engine() |
566 { | 566 { |
567 // Initialize globals | 567 // Initialize globals |
568 System.out.print("Engine() constructor\n"); | 568 System.out.print("Engine() constructor\n"); |
569 | 569 |
570 // Sound system | 570 // Sound system |
571 G.smgr = new SoundManager(new AudioFormat(22050, 16, 1, true, false), 1); | 571 G.smgr = new SoundManager(new AudioFormat(22050, 16, 1, true, false), 1); |
572 | 572 |
573 // Load resources | 573 // Load resources |
574 try | 574 try |
575 { | 575 { |
576 ResourceLoader res = new ResourceLoader("graphics/board.jpg"); | 576 ResourceLoader res = new ResourceLoader("graphics/board.jpg"); |
577 G.lautaBG = ImageIO.read(res.getStream()); | 577 G.lautaBG = ImageIO.read(res.getStream()); |
578 | 578 |
579 try { | 579 try { |
580 res = new ResourceLoader("graphics/font.ttf"); | 580 res = new ResourceLoader("graphics/font.ttf"); |
581 | 581 |
582 G.fonts = new Font[G.numFonts]; | 582 G.fonts = new Font[G.numFonts]; |
583 G.fonts[0] = Font.createFont(Font.TRUETYPE_FONT, res.getStream()); | 583 G.fonts[0] = Font.createFont(Font.TRUETYPE_FONT, res.getStream()); |
584 G.fonts[1] = G.fonts[0].deriveFont(24f); | 584 G.fonts[1] = G.fonts[0].deriveFont(24f); |
585 G.fonts[2] = G.fonts[0].deriveFont(64f); | 585 G.fonts[2] = G.fonts[0].deriveFont(64f); |
586 G.fonts[3] = G.fonts[0].deriveFont(32f); | 586 G.fonts[3] = G.fonts[0].deriveFont(32f); |
587 } | 587 } |
588 catch (FontFormatException e) | 588 catch (FontFormatException e) |
589 { | 589 { |
590 System.out.print("Could not initialize fonts.\n"); | 590 System.out.print("Could not initialize fonts.\n"); |
591 } | 591 } |
592 | 592 |
593 res = new ResourceLoader("sounds/gamemusic.wav"); | 593 res = new ResourceLoader("sounds/gamemusic.wav"); |
594 musa = res.getStream(); | 594 musa = res.getStream(); |
595 } | 595 } |
596 catch (IOException e) | 596 catch (IOException e) |
597 { | 597 { |
612 widgets.add(new BtnSwapPiece(767f, 450f)); | 612 widgets.add(new BtnSwapPiece(767f, 450f)); |
613 widgets.add(new BtnAbout (767f, 550f)); | 613 widgets.add(new BtnAbout (767f, 550f)); |
614 widgets.add(new BtnNewGame (767f, 630f)); | 614 widgets.add(new BtnNewGame (767f, 630f)); |
615 | 615 |
616 aboutBox = new AboutBox(); | 616 aboutBox = new AboutBox(); |
617 | 617 |
618 // Game | 618 // Game |
619 startNewGame(); | 619 startNewGame(); |
620 | 620 |
621 // Initialize event listeners | 621 // Initialize event listeners |
622 addKeyListener(this); | 622 addKeyListener(this); |
623 addMouseListener(this); | 623 addMouseListener(this); |
624 addMouseWheelListener(this); | 624 addMouseWheelListener(this); |
625 | 625 |
645 | 645 |
646 public void paintComponent(Graphics g) | 646 public void paintComponent(Graphics g) |
647 { | 647 { |
648 Graphics2D g2 = (Graphics2D) g; | 648 Graphics2D g2 = (Graphics2D) g; |
649 boolean scaleChanged = false; | 649 boolean scaleChanged = false; |
650 | 650 |
651 // Rescale if parent component size has changed | 651 // Rescale if parent component size has changed |
652 Dimension dim = getSize(); | 652 Dimension dim = getSize(); |
653 if (G.screenDim == null || !dim.equals(G.screenDim)) | 653 if (G.screenDim == null || !dim.equals(G.screenDim)) |
654 { | 654 { |
655 float dw = dim.width / 1024.0f, | 655 float dw = dim.width / 1024.0f, |
656 dh = dim.height / 768.0f; | 656 dh = dim.height / 768.0f; |
657 | 657 |
658 // Rescale IDM GUI widgets | 658 // Rescale IDM GUI widgets |
659 widgets.setScale(dw, dh); | 659 widgets.setScale(dw, dh); |
660 G.screenDim = dim; | 660 G.screenDim = dim; |
661 | 661 |
662 // Rescale background image | 662 // Rescale background image |
663 G.lautaBGScaled = new BufferedImage(dim.width, dim.height, BufferedImage.TYPE_INT_RGB); | 663 G.lautaBGScaled = new BufferedImage(dim.width, dim.height, BufferedImage.TYPE_INT_RGB); |
664 Graphics2D gimg = G.lautaBGScaled.createGraphics(); | 664 Graphics2D gimg = G.lautaBGScaled.createGraphics(); |
665 gimg.setRenderingHint(RenderingHints.KEY_INTERPOLATION, | 665 gimg.setRenderingHint(RenderingHints.KEY_INTERPOLATION, |
666 RenderingHints.VALUE_INTERPOLATION_BICUBIC); | 666 RenderingHints.VALUE_INTERPOLATION_BICUBIC); |
667 | 667 |
668 gimg.drawImage(G.lautaBG, 0, 0, dim.width, dim.height, null); | 668 gimg.drawImage(G.lautaBG, 0, 0, dim.width, dim.height, null); |
669 lauta.paintBackPlate(gimg); | 669 lauta.paintBackPlate(gimg); |
670 | 670 |
671 // Rescale fonts | 671 // Rescale fonts |
672 G.fonts[1] = G.fonts[0].deriveFont(24f * dw); | 672 G.fonts[1] = G.fonts[0].deriveFont(24f * dw); |
673 G.fonts[2] = G.fonts[0].deriveFont(64f * dw); | 673 G.fonts[2] = G.fonts[0].deriveFont(64f * dw); |
674 G.fonts[3] = G.fonts[0].deriveFont(32f * dw); | 674 G.fonts[3] = G.fonts[0].deriveFont(32f * dw); |
675 | 675 |
676 System.out.print("scale changed\n"); | 676 System.out.print("scale changed\n"); |
677 scaleChanged = true; | 677 scaleChanged = true; |
678 } | 678 } |
679 | 679 |
680 // Get font metrics against current Graphics2D context | 680 // Get font metrics against current Graphics2D context |
681 if (G.metrics == null || scaleChanged) | 681 if (G.metrics == null || scaleChanged) |
682 { | 682 { |
683 G.metrics = new FontMetrics[G.numFonts]; | 683 G.metrics = new FontMetrics[G.numFonts]; |
684 for (int i = 0; i < G.numFonts; i++) | 684 for (int i = 0; i < G.numFonts; i++) |
685 G.metrics[i] = g2.getFontMetrics(G.fonts[i]); | 685 G.metrics[i] = g2.getFontMetrics(G.fonts[i]); |
686 } | 686 } |
687 | 687 |
688 // Draw background image, pieces, widgets | 688 // Draw background image, pieces, widgets |
689 g2.drawImage(G.lautaBGScaled, 0, 0, null); | 689 g2.drawImage(G.lautaBGScaled, 0, 0, null); |
690 | 690 |
691 // Use antialiasing when rendering the game elements | 691 // Use antialiasing when rendering the game elements |
692 g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, | 692 g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, |
700 | 700 |
701 // Frames per second counter | 701 // Frames per second counter |
702 g2.setFont(G.fonts[1]); | 702 g2.setFont(G.fonts[1]); |
703 long currTime = new Date().getTime(); | 703 long currTime = new Date().getTime(); |
704 g2.drawString("fps = "+ ((gameFrames * 1000) / (currTime - startTime)), G.screenDim.width - 120, 20); | 704 g2.drawString("fps = "+ ((gameFrames * 1000) / (currTime - startTime)), G.screenDim.width - 120, 20); |
705 | 705 |
706 gameFrames++; | 706 gameFrames++; |
707 } | 707 } |
708 | 708 |
709 public void startThreads() | 709 public void startThreads() |
710 { | 710 { |
714 animThread = new Thread(this); | 714 animThread = new Thread(this); |
715 animEnable = true; | 715 animEnable = true; |
716 animThread.start(); | 716 animThread.start(); |
717 } | 717 } |
718 } | 718 } |
719 | 719 |
720 public void stopThreads() | 720 public void stopThreads() |
721 { | 721 { |
722 System.out.print("stopThreads()\n"); | 722 System.out.print("stopThreads()\n"); |
723 | 723 |
724 // Stop animations | 724 // Stop animations |
725 if (animThread != null) | 725 if (animThread != null) |
726 { | 726 { |
727 animThread.interrupt(); | 727 animThread.interrupt(); |
728 animEnable = false; | 728 animEnable = false; |
772 lauta.mouseWheelMoved(e); | 772 lauta.mouseWheelMoved(e); |
773 } | 773 } |
774 | 774 |
775 public void keyTyped(KeyEvent e) { } | 775 public void keyTyped(KeyEvent e) { } |
776 public void keyReleased(KeyEvent e) { } | 776 public void keyReleased(KeyEvent e) { } |
777 | 777 |
778 public void keyPressed(KeyEvent e) | 778 public void keyPressed(KeyEvent e) |
779 { | 779 { |
780 // Handle keyboard input | 780 // Handle keyboard input |
781 if (widgets.containsObject(aboutBox)) | 781 if (widgets.containsObject(aboutBox)) |
782 aboutBox.keyPressed(e); | 782 aboutBox.keyPressed(e); |
783 else | 783 else |
784 widgets.keyPressed(e); | 784 widgets.keyPressed(e); |
785 } | 785 } |
786 | 786 |
787 public void run() | 787 public void run() |
788 { | 788 { |
789 while (animEnable) | 789 while (animEnable) |
790 { | 790 { |
791 // Progress game animation clock | 791 // Progress game animation clock |
793 | 793 |
794 // Animate components | 794 // Animate components |
795 lauta.animate(gameClock); | 795 lauta.animate(gameClock); |
796 if (lauta.nextPiece != null) | 796 if (lauta.nextPiece != null) |
797 lauta.nextPiece.animate(gameClock); | 797 lauta.nextPiece.animate(gameClock); |
798 | 798 |
799 // Repaint with a frame limiter | 799 // Repaint with a frame limiter |
800 if (gameClock % 4 == 1) | 800 if (gameClock % 4 == 1) |
801 repaint(); | 801 repaint(); |
802 | 802 |
803 // Sleep for a moment | 803 // Sleep for a moment |
804 try { | 804 try { |
805 Thread.sleep(10); | 805 Thread.sleep(10); |
806 } | 806 } |
807 catch (InterruptedException x) { | 807 catch (InterruptedException x) { |
808 } | 808 } |
809 } | 809 } |
810 } | 810 } |
811 | 811 |
812 class BtnNewGame extends IDMButton | 812 class BtnNewGame extends IDMButton |
813 { | 813 { |
814 public BtnNewGame(float x, float y) | 814 public BtnNewGame(float x, float y) |
815 { | 815 { |
816 super(x, y, KeyEvent.VK_ESCAPE, G.fonts[1], "New Game"); | 816 super(x, y, KeyEvent.VK_ESCAPE, G.fonts[1], "New Game"); |