Objektorientierte Programmierung WS 2013/2014 - Datei SpielBall.java
import java.awt.Graphics;
class SpielBall extends Ball implements SpielRolle
{
public static final int LEFTMARGIN = 20;
public static final int RIGHTMARGIN = 20;
public static final int UPPERMARGIN = 40;
public static final int LOWERMARGIN = 20;
private SpielRolle rolle;
private int xlinks, xrechts; // x-Koordinaten des Spielbereiches
private int yoben, yunten; // y-Koordinaten des Spielbereiches
// Konstruktor
SpielBall (int xpos, int ypos, int radius, int xlinks, int yoben,
int xrechts, int yunten)
{
super(xpos, ypos, radius);
this.xlinks = xlinks;
this.xrechts = xrechts;
this.yoben = yoben;
this.yunten = yunten;
}
// Zugriffsmethoden
public SpielRolle getRolle ()
{
return rolle;
}
public void setRolle (SpielRolle r)
{
rolle = r;
}
// Implementieren der Schnittstelle SpielRolle
public void moveSpielBall ()
{ // Delegation an die Rolle
rolle.moveSpielBall();
}
public void paintSpielBall (Graphics g)
{ // Delegation an die Rolle
rolle.paintSpielBall(g);
}
// Ueberschreiben der move-Methode von Ball, um im Frame zu bleiben
public boolean blocked ()
{ // Stoesst der Ball gegen den Rand
return yMotion() < 0 && y() <= yoben + UPPERMARGIN
|| yMotion() > 0 && y() >= (yunten - LOWERMARGIN)
|| xMotion() < 0 && x() <= xlinks + LEFTMARGIN
|| xMotion() > 0 && x() >= (xrechts - RIGHTMARGIN);
}
public void move ()
{ // bei Erreichen des Randes anhalten, sonst bewegen
super.move();
if (blocked()) setMotion(0, 0);
}
}
Generiert mit Camelot | Probleme mit Camelot? | Geändert am: 05.12.2013


