Hallo zusammen!
Ich habe folgendes Problem: Als Aufgabe müssen wir sich bewegende Kreise in einem Applet darstellen. Bis jetzt auch kein Problem, das funktioniert alles tiptop. Die Kreise haben rechts einen Namen z.B. "Kreis:01". Wenn der Kreis an den rechten Rand kommt, verschiebt sich dieser Text aber leider ausserhalb des Applets.
Die Lösung wäre eigentlich einfach, ich müsste nur wissen, wie gross mein ganzes Kreisobjekt (Kreis + Text) ist und dann diese Gröse von windowheight (siehe Code) abziehen. Wie finde ich aber diese Grösser heraus??
Hier mal mein Code für den Kreis:
Ich hoffe, ich konnte mich verständlich ausdrücken...
Besten Dank!
Ich habe folgendes Problem: Als Aufgabe müssen wir sich bewegende Kreise in einem Applet darstellen. Bis jetzt auch kein Problem, das funktioniert alles tiptop. Die Kreise haben rechts einen Namen z.B. "Kreis:01". Wenn der Kreis an den rechten Rand kommt, verschiebt sich dieser Text aber leider ausserhalb des Applets.
Die Lösung wäre eigentlich einfach, ich müsste nur wissen, wie gross mein ganzes Kreisobjekt (Kreis + Text) ist und dann diese Gröse von windowheight (siehe Code) abziehen. Wie finde ich aber diese Grösser heraus??
Hier mal mein Code für den Kreis:
Code:
import java.awt.*;
class Kreis extends Gegenstand implements Runnable {
private static final double PI = 3.14159;
private double radius;
private int windowHeight;
private int windowWidth;
/**
* @param name Name des Balls
* @param color Farbe des Balls
* @param xPos Startposition x-Komponente
* @param yPos Startposition y-Komponente
* @param xGeschw Geschwindigkeit in x-Richtung
* @param yGeschw Geschwindigkeit in y-Richtung
* @param radius Radius des Balls
* @param windowWidth Fensterbreite
* @param windowHeight Fensterhöhe
*/
public Kreis(String name, Color color, int xPos, int yPos, int xGeschw, int yGeschw, int radius, int windowWidth, int windowHeight) {
// perform all initialization for this object
super(name, color, xPos, yPos, yGeschw, xGeschw); // initialize the super-class
this.radius = radius;
this.windowHeight = windowHeight;
this.windowWidth = windowWidth;
// after initialization, start the thread for this object
super.start();
}
public double flaeche() {
return (PI * radius * radius);
}
public void paint(Graphics pGraphics) {
pGraphics.setColor(color);
pGraphics.fillOval(xPosition, yPosition, (int) (2*radius), (int) (2*radius));
pGraphics.drawString(name, (int) (xPosition + 2 * radius + 5), (int) (yPosition + 2 * radius));
}
public void run() {
while(running) { // continue until we are told to stop
// vertical position
if (yGeschwindigkeit < 0) {
if ((yPosition + yGeschwindigkeit) < 0) yGeschwindigkeit = 0 - yGeschwindigkeit;
}
else {
if ((yPosition + yGeschwindigkeit) > windowHeight - 2 * radius) yGeschwindigkeit = 0 - yGeschwindigkeit; //Damit der Ball nicht aus dem Fenster "rutscht" wird 2 * radius abgezogen
}
yPosition += yGeschwindigkeit;
// horizontal position
if (xGeschwindigkeit < 0){
if ((xPosition + xGeschwindigkeit) < 0) xGeschwindigkeit = 0 - xGeschwindigkeit;
}
else {
if ((xPosition + xGeschwindigkeit) > windowWidth - 2 * radius) xGeschwindigkeit = 0 - xGeschwindigkeit; //Damit der Ball nicht aus dem Fenster "rutscht" wird 2 * radius abgezogen
}
xPosition += xGeschwindigkeit;
try { Thread.sleep(50); }
catch(InterruptedException e) {}
}
objectThread = null; // dereference the thread - not really necessary, but a nicety
}
}
Ich hoffe, ich konnte mich verständlich ausdrücken...
Besten Dank!