CoinAnimation.java 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. package model.drawObjects;
  2. import java.awt.*;
  3. import java.awt.geom.Ellipse2D;
  4. import java.awt.geom.Point2D;
  5. /**
  6. * Created by Bilel on 4-6-2015.
  7. *
  8. * CoinAnimation klasse: Weergeeft een balletje (resembleerd een muntje) dat omlaag valt.
  9. * Je hoeft alleen achter elkaar de paint(Graphics2D) methode aan te roepen, deze roep automatisch een 'reposition' aan,
  10. * waardoor je het balletje alleen snel achter elkaar hoeft te repainten.
  11. */
  12. public class CoinAnimation extends DrawObject {
  13. private Ellipse2D coinShape;
  14. private Point2D.Double coinSetPoint;
  15. // Hoevaak de timer gelopen heeft
  16. private static int timerLoops = 0;
  17. public CoinAnimation(int startX, int startY) {
  18. coinSetPoint = new Point2D.Double(startX, startY);
  19. coinShape = new Ellipse2D.Double(startX, startY, 20, 20);
  20. }
  21. // Start
  22. public void start() {
  23. timerLoops = 1;
  24. }
  25. public void draw(Graphics2D g2) {
  26. update(timerLoops); // UPDATE
  27. g2.setColor(new Color(255, 255, 0, (255 - timerLoops * 5))); // GEEL
  28. g2.draw(coinShape); // MUNTJE TEKENEN
  29. g2.fill(coinShape);
  30. }
  31. // Wordt na elke paint aangeroepen
  32. public void update(float factor) {
  33. // Alleen tekenen wanner de animatie ingesteld is om te tekenen (zie: areWeDoneYet()), anders doei.
  34. if (areWeDoneYet()) {
  35. System.out.println("Call initCoin(int startX, startY) first!");
  36. return;
  37. }
  38. // Coin omlaag verschuiven doordat Y * loops omlaag gaat.
  39. // Per frame verschuift het balletje delta 10 op Y-as.
  40. coinShape.setFrame(coinSetPoint.getX(), coinSetPoint.getY() + 10 * timerLoops,
  41. coinShape.getWidth(), coinShape.getHeight());
  42. timerLoops++;
  43. }
  44. // Zijn we al begonnen/klaar?
  45. public boolean areWeDoneYet() {
  46. if (timerLoops > 49 || timerLoops == 0) {
  47. timerLoops = 0;
  48. return true;
  49. }
  50. return false;
  51. }
  52. // Zitten we in de laatste loop?
  53. public boolean isLastLoop() { return (timerLoops == 49) ? true : false; }
  54. }