package de.lmu.ifi.tcs; import java.awt.Color; import java.awt.Point; public class Ball { private Point randpunkt; private double pos_x; private double pos_y; private double dx; private double dy; private long letztesupdate; private Color farbe = Color.BLACK; public Ball(Point randpunkt, Point startpunkt, double dx, double dy, Color farbe) { this.randpunkt = randpunkt; this.pos_x = startpunkt.getX(); this.pos_y = startpunkt.getY(); this.dx = dx; this.dy = dy; this.letztesupdate = System.currentTimeMillis(); this.farbe = farbe; } public Ball(Point randpunkt, Point startpunkt, double dx, double dy) { this(randpunkt, startpunkt, dx, dy, Color.BLACK); } /** * Aktualisiert die Position des Balls gemäß der vestrichenen Zeit. */ public void updatePosition() { long aktuellezeit = System.currentTimeMillis(); long verstrichen = aktuellezeit - letztesupdate; pos_x += dx * verstrichen / 1000; pos_y += dy * verstrichen / 1000; letztesupdate = aktuellezeit; if (pos_x<0 || pos_x>randpunkt.x) { dx *= -1; pos_x = (2*randpunkt.x - pos_x) % randpunkt.x; } if (pos_y<0 || pos_y>randpunkt.y) { dy *= -1; pos_y = (2*randpunkt.y - pos_y) % randpunkt.y; } } /** * @return letzte Position des Balles */ public Point holePosition() { return new Point((int) Math.round(pos_x), (int) Math.round(pos_y)); } /** * @return aktuelle Farbe des Balls */ public Color getFarbe() { return farbe; } /** * Wechselt die Farbe des Balls */ public void wechsleFarbe() { if (this.farbe.equals(Color.RED)) { this.farbe = Color.GREEN; } else if (this.farbe.equals(Color.GREEN)) { this.farbe = Color.BLUE; } else if (this.farbe.equals(Color.BLUE)) { this.farbe = Color.ORANGE; } else { this.farbe = Color.RED; } } }