FH-Hof Java2D Richard Göbel. FH-Hof Java2D - Funktionen Java2D unterstützt: das Zeichnen von...
-
Upload
gerrit-rediske -
Category
Documents
-
view
107 -
download
0
Transcript of FH-Hof Java2D Richard Göbel. FH-Hof Java2D - Funktionen Java2D unterstützt: das Zeichnen von...
![Page 1: FH-Hof Java2D Richard Göbel. FH-Hof Java2D - Funktionen Java2D unterstützt: das Zeichnen von Grafiken die Darstellung von Texten die Darstellung von Bildern.](https://reader036.fdocument.pub/reader036/viewer/2022062417/55204d6449795902118ba576/html5/thumbnails/1.jpg)
FH-Hof
Java2D
Richard Göbel
![Page 2: FH-Hof Java2D Richard Göbel. FH-Hof Java2D - Funktionen Java2D unterstützt: das Zeichnen von Grafiken die Darstellung von Texten die Darstellung von Bildern.](https://reader036.fdocument.pub/reader036/viewer/2022062417/55204d6449795902118ba576/html5/thumbnails/2.jpg)
FH-Hof
Java2D - Funktionen
Java2D unterstützt:
das Zeichnen von Grafiken
die Darstellung von Texten
die Darstellung von Bildern
Java2D bildet die Basis für die Definition
eigener Komponenten
Java2D unterstützt das Drucken
![Page 3: FH-Hof Java2D Richard Göbel. FH-Hof Java2D - Funktionen Java2D unterstützt: das Zeichnen von Grafiken die Darstellung von Texten die Darstellung von Bildern.](https://reader036.fdocument.pub/reader036/viewer/2022062417/55204d6449795902118ba576/html5/thumbnails/3.jpg)
FH-Hof
Java2D - Ansatz
Definition einer Unterklasse für eine
Komponente ohne Inhalt, zum Beispiel:
Canvas (AWT)
JPanel (Swing)
JComponent (Swing)
Definition der Method „paint“ in dieser
Unterklasse
Die Methode „paint“ wird automatisch bei dem
(Neu-) Zeichnen der Grafik aufgerufen
![Page 4: FH-Hof Java2D Richard Göbel. FH-Hof Java2D - Funktionen Java2D unterstützt: das Zeichnen von Grafiken die Darstellung von Texten die Darstellung von Bildern.](https://reader036.fdocument.pub/reader036/viewer/2022062417/55204d6449795902118ba576/html5/thumbnails/4.jpg)
FH-Hof
Aufbau einer Klasse für eine Grafik
public class <className> extends JComponent
{
// Attribute mit den zu zeichnenden Daten
. . .
public void paint (Graphics g)
{
Graphics2D g2 = (Graphics2D) g;
// ab hier wird gezeichnet
. . .
}
. . .
}
![Page 5: FH-Hof Java2D Richard Göbel. FH-Hof Java2D - Funktionen Java2D unterstützt: das Zeichnen von Grafiken die Darstellung von Texten die Darstellung von Bildern.](https://reader036.fdocument.pub/reader036/viewer/2022062417/55204d6449795902118ba576/html5/thumbnails/5.jpg)
FH-Hof
Inhalt der Methode „paint“
Zeichenmethoden für Graphics2D:
draw Zeichnen grafischer Objekte
fill Füllen grafischer Objekte
Grafische Objekte
Rectangle2D
Ellipse2D
. . .
Kontext für das Zeichnen
Klasse „Stroke“ ist der Zeichenstift: Breite, Höhe,
etc.
Klasse „Paint“ ist die Farbe
. . .
![Page 6: FH-Hof Java2D Richard Göbel. FH-Hof Java2D - Funktionen Java2D unterstützt: das Zeichnen von Grafiken die Darstellung von Texten die Darstellung von Bildern.](https://reader036.fdocument.pub/reader036/viewer/2022062417/55204d6449795902118ba576/html5/thumbnails/6.jpg)
FH-Hof
Koordinatensystem
100
200
100
200
![Page 7: FH-Hof Java2D Richard Göbel. FH-Hof Java2D - Funktionen Java2D unterstützt: das Zeichnen von Grafiken die Darstellung von Texten die Darstellung von Bildern.](https://reader036.fdocument.pub/reader036/viewer/2022062417/55204d6449795902118ba576/html5/thumbnails/7.jpg)
FH-Hof
Ausgabe eines Rechtecks
public class myRectangle extends JComponent
{
Rectangle2D.Float s = new Rectangle2D.Float(10,10,100,200);
public void paint (Graphics g)
{
Graphics2D g2 = (Graphics2D) g;
g2.setStroke(new BasicStroke(1));
g2.draw(s);
}
}
![Page 8: FH-Hof Java2D Richard Göbel. FH-Hof Java2D - Funktionen Java2D unterstützt: das Zeichnen von Grafiken die Darstellung von Texten die Darstellung von Bildern.](https://reader036.fdocument.pub/reader036/viewer/2022062417/55204d6449795902118ba576/html5/thumbnails/8.jpg)
FH-Hof
Einfache Graphische Objekte: Klassen
Linie: Line2D, Line2D.Float, Line2D.Double
Quadratische Kurve: QuadCurve2D.Float,
QuadCurve2D.Double
Kubische Kurve: CubicCurve2D.Float,
CubicCurve2D.Double
Rechtecke: Rectangle2D.Float,
Rectangle2D.Double
Ellipse: Ellipse2D.Float, Ellipse2D.Double
Segment einer Ellipse: Arc2D.Float, Arc2D.Double
![Page 9: FH-Hof Java2D Richard Göbel. FH-Hof Java2D - Funktionen Java2D unterstützt: das Zeichnen von Grafiken die Darstellung von Texten die Darstellung von Bildern.](https://reader036.fdocument.pub/reader036/viewer/2022062417/55204d6449795902118ba576/html5/thumbnails/9.jpg)
FH-Hof
Einfache Grafische Objekte: Darstellung Teil 1
Line2D.Float(float x1, float y1, float x2, float y2)
QuadCurve2D.Float(float x1, float y1, float cx, float cy, float x2, float y2)
(x1, y1)
(x2, y2)
CubicCurve2D.Float(float x1, float y1,
float cx1, float cy1,
float cx2, float cy2,
float x2, float y2)
(x1, y1) (x2, y2)
(cx1, cy1)
(cx2, cy2)
(cx1, cy1)
(x1, y1)
(x2, y2)
![Page 10: FH-Hof Java2D Richard Göbel. FH-Hof Java2D - Funktionen Java2D unterstützt: das Zeichnen von Grafiken die Darstellung von Texten die Darstellung von Bildern.](https://reader036.fdocument.pub/reader036/viewer/2022062417/55204d6449795902118ba576/html5/thumbnails/10.jpg)
FH-Hof
Einfache Grafische Objekte: Darstellung Teil 2
Rectangle2D.Float(float x, float y,
float w, float h)
Ellipse2D.Float(float x, float y, float w, float h)
(x, y)
w
h
(x, y)
w
h
![Page 11: FH-Hof Java2D Richard Göbel. FH-Hof Java2D - Funktionen Java2D unterstützt: das Zeichnen von Grafiken die Darstellung von Texten die Darstellung von Bildern.](https://reader036.fdocument.pub/reader036/viewer/2022062417/55204d6449795902118ba576/html5/thumbnails/11.jpg)
FH-Hof
Einfache Grafische Objekte: Darstellung Teil 3
Arc2D.Float(float x, float y, float w, float h, float s, float e, int type)
(x, y)
w
hse
type = CHORD type = OPENtype = PIE
![Page 12: FH-Hof Java2D Richard Göbel. FH-Hof Java2D - Funktionen Java2D unterstützt: das Zeichnen von Grafiken die Darstellung von Texten die Darstellung von Bildern.](https://reader036.fdocument.pub/reader036/viewer/2022062417/55204d6449795902118ba576/html5/thumbnails/12.jpg)
FH-Hof
Kombination von Objekten: GeneralPath
p = new GeneralPath();
p.moveTo(x1,y1);
p.lineTo(x2,y2);
p.quadTo(cx, cy,
x3,y3);
p.lineTo(x4,y4);
(x1, y1)
(x2, y2)
(x3, y3)
(x4, y4)
(cx, cy)
![Page 13: FH-Hof Java2D Richard Göbel. FH-Hof Java2D - Funktionen Java2D unterstützt: das Zeichnen von Grafiken die Darstellung von Texten die Darstellung von Bildern.](https://reader036.fdocument.pub/reader036/viewer/2022062417/55204d6449795902118ba576/html5/thumbnails/13.jpg)
FH-Hof
Kombination von Objekten: AREA
a1 = new Area(new Rectangle2D.Float( . . . )
a2 = new Area(new Rectangle2D.Float( . . . )
a1.add(a2) a1.exclusiveOr(a2)
a1.intersect(a2) a1.subtract(a2)
![Page 14: FH-Hof Java2D Richard Göbel. FH-Hof Java2D - Funktionen Java2D unterstützt: das Zeichnen von Grafiken die Darstellung von Texten die Darstellung von Bildern.](https://reader036.fdocument.pub/reader036/viewer/2022062417/55204d6449795902118ba576/html5/thumbnails/14.jpg)
FH-Hof
Bestandteile des Kontext
Zeichenstift: Stroke
Füllfarbe: Paint
Transformationen: AffineTransform
Weitere Attribute:
Hintergrund
Rendering Hints
Clip-Bereich
![Page 15: FH-Hof Java2D Richard Göbel. FH-Hof Java2D - Funktionen Java2D unterstützt: das Zeichnen von Grafiken die Darstellung von Texten die Darstellung von Bildern.](https://reader036.fdocument.pub/reader036/viewer/2022062417/55204d6449795902118ba576/html5/thumbnails/15.jpg)
FH-Hof
Zeichenstift
Basis: Interface Stroke
Implementierende Klasse: BasicStroke
Parameter für die Klasse BasicStroke:
Breite
Gestaltung von Ecken
Muster für gestrichelte Linien
Setzen des Zeichenstifts
<graphics2d>.setStroke(new BasicStroke(. . .))
![Page 16: FH-Hof Java2D Richard Göbel. FH-Hof Java2D - Funktionen Java2D unterstützt: das Zeichnen von Grafiken die Darstellung von Texten die Darstellung von Bildern.](https://reader036.fdocument.pub/reader036/viewer/2022062417/55204d6449795902118ba576/html5/thumbnails/16.jpg)
FH-Hof
Konstruktoren für die Klasse BasicStroke
BasicStroke( float width)
BasicStroke( float width,
int cap,
int join)
BasicStroke( float width,
int cap,
int join,
float miterlimit,
float[] dash,
float dash_phase)
![Page 17: FH-Hof Java2D Richard Göbel. FH-Hof Java2D - Funktionen Java2D unterstützt: das Zeichnen von Grafiken die Darstellung von Texten die Darstellung von Bildern.](https://reader036.fdocument.pub/reader036/viewer/2022062417/55204d6449795902118ba576/html5/thumbnails/17.jpg)
FH-Hof
Ende einer Linie: Parameter „cap“
CAP_BUTT
CAP_SQUARE
CAP_BUTT
![Page 18: FH-Hof Java2D Richard Göbel. FH-Hof Java2D - Funktionen Java2D unterstützt: das Zeichnen von Grafiken die Darstellung von Texten die Darstellung von Bildern.](https://reader036.fdocument.pub/reader036/viewer/2022062417/55204d6449795902118ba576/html5/thumbnails/18.jpg)
FH-Hof
Ende einer Linie: Parameter „join“
JOIN_BEVEL
JOIN_MITER
JOIN_ROUND
![Page 19: FH-Hof Java2D Richard Göbel. FH-Hof Java2D - Funktionen Java2D unterstützt: das Zeichnen von Grafiken die Darstellung von Texten die Darstellung von Bildern.](https://reader036.fdocument.pub/reader036/viewer/2022062417/55204d6449795902118ba576/html5/thumbnails/19.jpg)
FH-Hof
Muster für gestrichelte Linien: Parameter dash
Für den Parameter „dash“ wird ein Array für
den Datentyp „float“ erwartet
Das Array muss eine gerade Anzahl von
Elementen enthalten
Die Länge der Liniensegmente wird mit den
geraden Elementen definiert
Die Länge der Zwischenräume wird mit den
ungeraden Elementen definiert
Der Startpunkt innerhalb des Musters wird mit
Hilfe des Parameters „dashphase“ festgelegt,
![Page 20: FH-Hof Java2D Richard Göbel. FH-Hof Java2D - Funktionen Java2D unterstützt: das Zeichnen von Grafiken die Darstellung von Texten die Darstellung von Bildern.](https://reader036.fdocument.pub/reader036/viewer/2022062417/55204d6449795902118ba576/html5/thumbnails/20.jpg)
FH-Hof
Beispiel für eine gestrichelte Linie
float[] pattern = { 1, 19, 10, 10 }
![Page 21: FH-Hof Java2D Richard Göbel. FH-Hof Java2D - Funktionen Java2D unterstützt: das Zeichnen von Grafiken die Darstellung von Texten die Darstellung von Bildern.](https://reader036.fdocument.pub/reader036/viewer/2022062417/55204d6449795902118ba576/html5/thumbnails/21.jpg)
FH-Hof
Füllfarbe: Optionen
Einheitliche Farbe mit der Klasse „Color“, z.B.:
Color(float r, float g, float b)
Farbverlauf mit der Klasse „GradientPaint“, z.B.:
GradientPaint(float x1, float y1, Color col1,
float x2, float y2, Color col2,
boolean cyclic)
Textur mit der Klasse „TexturePaint“, z.B.:
TexturePaint(BufferedImage txtr,
Rectangle2d anchor)
Setzen der Füllfarbe:
<graphics2d>.setPaint( . . . )
![Page 22: FH-Hof Java2D Richard Göbel. FH-Hof Java2D - Funktionen Java2D unterstützt: das Zeichnen von Grafiken die Darstellung von Texten die Darstellung von Bildern.](https://reader036.fdocument.pub/reader036/viewer/2022062417/55204d6449795902118ba576/html5/thumbnails/22.jpg)
FH-Hof
Beispiele für Füllfarbe
new Color(1f,1f,0f) new TexturePaint(...)
new GradientPaint(30, 40, new Color(1f,0,0), 60, 70, new Color(0,1f,0), ...)
cyclic = false cyclic = true
![Page 23: FH-Hof Java2D Richard Göbel. FH-Hof Java2D - Funktionen Java2D unterstützt: das Zeichnen von Grafiken die Darstellung von Texten die Darstellung von Bildern.](https://reader036.fdocument.pub/reader036/viewer/2022062417/55204d6449795902118ba576/html5/thumbnails/23.jpg)
FH-Hof
Transformationen
Auf alle Punkte eines zu zeichnenden Objekts
wird zunächst eine Transformation angewendet
Mit diesem Ansatz lassen sich verschiedene
Ausgabemedien unterstützen:
Bildschirm
Drucker
Palm
. . .
Die Transformation wird mit Hilfe einer 3 3
Matrix als Objekt der Klasse
„AffineTransformation“ dargestellt
![Page 24: FH-Hof Java2D Richard Göbel. FH-Hof Java2D - Funktionen Java2D unterstützt: das Zeichnen von Grafiken die Darstellung von Texten die Darstellung von Bildern.](https://reader036.fdocument.pub/reader036/viewer/2022062417/55204d6449795902118ba576/html5/thumbnails/24.jpg)
FH-Hof
Methoden von Graphics2D für Transformationen
Änderung der aktuellen Transformation
rotate(double theta, double x, double y)
scale(double sx, double sy)
shear(double shx, double shy)
translate(double tx, double ty)
Neue Transformation setzen
setTransform(AffineTransform Tx)
![Page 25: FH-Hof Java2D Richard Göbel. FH-Hof Java2D - Funktionen Java2D unterstützt: das Zeichnen von Grafiken die Darstellung von Texten die Darstellung von Bildern.](https://reader036.fdocument.pub/reader036/viewer/2022062417/55204d6449795902118ba576/html5/thumbnails/25.jpg)
FH-Hof
Statische Funktionen von AffineTransformation
getRotateInstance(double theta, double x,
double y)
getScaleInstance(double sx, double sy)
getShearInstance(double shx, double shy)
getTranslateInstance(double tx, double ty)