Objektorientierte Programmierung WS 2013/2014 - Datei Ablenker.java
import java.awt.*;
class Ablenker implements PBElement { // Ablenker, der die Kugel zuückwirft
// Größenangaben
final static int radius = 13;
// die Position
private Rectangle location;
// Konstruktor
public Ablenker(int x, int y) {
location = new Rectangle(x - radius, y - radius, 2 * radius, 2 * radius);
}
// Methoden des Interfaces
public boolean intersects(Ball b) // Ball b beruehrt das Hinderniss
{
return b.region().intersects(location);
}
public void paint(Graphics g) // Zeichne das Hinderniss
{
g.setColor(Color.green);
g.drawOval(location.x, location.y, 2 * radius, 2 * radius);
g.fillOval(location.x + 5, location.y + 5, 2 * radius - 10,
2 * radius - 10);
}
public void hit(Ball b) // reagiere auf die Beruehrung von b
{ // wenn der Ball abwaerts faellt, wird er aufwaerts gefedert
b.setMotion(-b.dx, -b.dy);
// raus aus dem Umkreis des Ablenkers
while (location.intersects(b.region())) {
b.move();
}
}
}
Generiert mit Camelot | Probleme mit Camelot? | Geändert am: 06.01.2014


