14.6 Alles was rund ist
Die Graphics-Klasse stellt vier Methoden zum Zeichnen von Ovalen und Kreisbögen bereit. Gefüllte und nicht gefüllte Ellipsen sind immer in ein Rechteck eingepasst.
abstract class java.awt.Graphics
|
|
abstract drawOval( int x, int y, intwidth, int height )
Zeichnet ein Oval in der Vordergrundfarbe, welches die Ausmaße eines Rechtecks hat. Das Oval hat eine Größe von (width + 1) Pixel in der Breite und (height + 1) Pixel in der Höhe. |
|
abstract fillOval( int x, int y, int width, int height )
Wie drawOval(), nur gefüllt |
|
abstract void drawArc( int x, int y, int width, int height, int startAngle, int arcAngle )
Zeichnet einen Kreisbogen. Null Grad liegt in der 3-Uhr-Position. Bei einem Aufruf mit den Winkelparametern 0, 270 wird ein Kreisbogen gezeichnet, bei dem 90 Grad im unteren rechten Bereich nicht gezeichnet sind. |
|
abstract void fillArc( int x, int y, int width, int height, int startAngle, int arcAngle )
Wie drawArc(), nur gefüllt |
Eine Kreis- und Ellipsen-Klasse
Bei der Methode drawOval() müssen wir immer daran denken, dass die Ellipse oder im Spezialfall der Kreis in ein Rechteck mit Startkoordinaten und mit Breite und Höhe gezeichnet wird. Dies ist nicht immer die natürliche Vorstellung von einer Ellipse beziehungsweise eines Kreises. Daher packen wir das Ganze in eine Klasse Ellipse und geben ihr eine draw()-Methode.
Listing 14.7 Ellipse.java
import java.awt.*;
public class Ellipse
{
public Ellipse( int x, int y, int r )
{
this.x = x; this.y = y; this.rx = r; this.ry = r;
// oder OOP-schöner this( x, y, r, r );
}
public Ellipse( int x, int y, int rx, int ry )
{
this.x = x; this.y = y; this.rx = rx; this.ry = ry;
}
public void draw( Graphics g )
{
g.drawOval( x-rx, y-ry, rx+rx, ry+ry );
}
private int x, y, rx, ry;
}
|