GraphicsApp API
Eine überarbeitete und angepasste Variante der originalen GraphicsApp-Umgebung
Line.java
gehe zur Dokumentation dieser Datei
1package de.ur.mi.oop.graphics;
2
3import de.ur.mi.oop.app.GraphicsApp;
4import de.ur.mi.oop.colors.Color;
5
6/**
7 * Die Klasse Line ist ein grafisches Objekt, das eine Linie darstellt.
8 */
9public class Line extends GraphicsObject {
10 private float xEndPoint;
11 private float yEndPoint;
12
13 /**
14 * Konstruiert eine neue Linie von startX und startY nach endX und endY.
15 *
16 * @param startX Die x-Position des ersten Punktes
17 * @param startY Die y-Position des ersten Punktes
18 * @param endX Die x-Position des Endpunktes
19 * @param endY Die y-Position des Endpunktes
20 * @param color Die Strichfarbe für die Linie
21 */
22 public Line(float startX, float startY, float endX, float endY, Color color) {
23 super(startX, startY, color);
24 this.xEndPoint = endX;
25 this.yEndPoint = endY;
28 this.setColor(color);
29 }
30
31 /**
32 * Konstruiert eine neue Linie von startX und startY nach endX und endY.
33 *
34 * @param startX Die x-Position des ersten Punktes
35 * @param startY Die y-Position des ersten Punktes
36 * @param endX Die x-Position des Endpunktes
37 * @param endY Die y-Position des Endpunktes
38 * @param color Die Strichfarbe für die Linie
39 * @param lineWidth Die Strichstärke
40 */
41 public Line(float startX, float startY, float endX, float endY, Color color, float lineWidth) {
42 this(startX, startY, endX, endY, color);
43 this.strokeWeight = lineWidth;
44 }
45
46 /**
47 * Konstruiert eine neue Linie von einem Startpunkt zu einem Endpunkt.
48 *
49 * @param start Start-Koordinatenpunkt
50 * @param end End-Koordinatenpunkt
51 * @param color Die Strichfarbe für die Linie
52 */
53 public Line(Point start, Point end, Color color) {
54 this(start.getXPos(), start.getYPos(), end.getXPos(), end.getYPos(), color);
55 }
56
57 /**
58 * Konstruiert eine neue Linie von einem Startpunkt zu einem Endpunkt.
59 *
60 * @param start Start-Koordinatenpunkt
61 * @param end End-Koordinatenpunkt
62 * @param color Die Strichfarbe für die Linie
63 * @param lineWidth Die Strichstärke
64 */
65 public Line(Point start, Point end, Color color, float lineWidth) {
66 this(start.getXPos(), start.getYPos(), end.getXPos(), end.getYPos(), color, lineWidth);
67 }
68
69 public float getLineWidth() {
70 return strokeWeight;
71 }
72
73 public void setLineWidth(float lineWidth) {
74 this.strokeWeight = lineWidth;
75 }
76
77 /**
78 * Gibt die Länge der Linie, also den Abstand zwischen Start- und Endpunkt, in Pixel zurück
79 *
80 * @return Länge der Linie in Pixel
81 */
82 public float getLength() {
83 float deltaX = Math.abs(getXPos() - xEndPoint);
84 float deltaY = Math.abs(getYPos() - yEndPoint);
85 return (float) Math.sqrt((deltaX * deltaX) + (deltaY * deltaY));
86 }
87
88 public float getStartpointX() {
89 return super.getXPos();
90 }
91
92 public float getStartpointY() {
93 return super.getYPos();
94 }
95
96 public float getEndpointX() {
97 return this.xEndPoint;
98 }
99
100 public float getEndpointY() {
101 return this.yEndPoint;
102 }
103
104 public void setStartPoint(float xStartPoint, float yStartPoint) {
105 super.setPosition(xStartPoint, yStartPoint);
106 }
107
108 public void setStartPoint(Point start) {
109 this.setStartPoint(start.getXPos(), start.getYPos());
110 }
111
112 public void setEndPoint(float xEndPoint, float yEndPoint) {
113 this.xEndPoint = xEndPoint;
114 this.yEndPoint = yEndPoint;
115 }
116
117 public void setEndPoint(Point end) {
118 this.setEndPoint(end.getXPos(), end.getYPos());
119 }
120
121 public void setColor(Color newColor) {
122 super.setColor(newColor);
123 this.setBorderColor(newColor);
124 }
125
126 public void move(float dx, float dy) {
127 this.setStartPoint(super.getXPos() + dx, super.getXPos() + dy);
128 this.setEndPoint(this.xEndPoint + dx, this.yEndPoint + dy);
129 }
130
131 public void setPosition(float x, float y) {
132 if (x < super.getXPos()) {
133 if (super.getXPos() < this.xEndPoint) {
134 this.xEndPoint = x + (this.xEndPoint - super.getXPos());
135 super.setXPos(x);
136 } else {
137 super.setXPos(x + (super.getXPos() - this.xEndPoint));
138 this.xEndPoint = x;
139 }
140 } else if (super.getXPos() < this.xEndPoint) {
141 this.xEndPoint = x + (this.xEndPoint - super.getXPos());
142 super.setXPos(x);
143 } else {
144 super.setXPos(x + (super.getXPos() - this.xEndPoint));
145 this.xEndPoint = x;
146 }
147
148 if (y < super.getYPos()) {
149 if (super.getYPos() < this.yEndPoint) {
150 this.yEndPoint = y + (this.yEndPoint - super.getYPos());
151 super.setYPos(y);
152 } else {
153 super.setYPos(y + (super.getYPos() - this.yEndPoint));
154 this.yEndPoint = y;
155 }
156 } else if (super.getYPos() < this.yEndPoint) {
157 this.yEndPoint = y + (this.yEndPoint - super.getYPos());
158 super.setYPos(y);
159 } else {
160 super.setYPos(y + (super.getYPos() - this.yEndPoint));
161 this.yEndPoint = y;
162 }
163
164 }
165
166 public void setPosition(Point p) {
167 this.setPosition(p.getXPos(), p.getYPos());
168 }
169
170 /**
171 * Setzt sowohl Start- als auch Endpunkt der Linie neu
172 *
173 * @param toStartPointX Die neue x-Position des Startpunkts
174 * @param toStartPointY Die neue y-Position des Startpunktes
175 * @param toEndPointX Die neue x-Position des Endpunkts
176 * @param toEndPointY Die neue y-Position des Endpunktes
177 */
178 public void setStartAndEndPoint(float toStartPointX, float toStartPointY, float toEndPointX, float toEndPointY) {
179 this.setStartPoint(toStartPointX, toStartPointY);
180 this.setEndPoint(toEndPointX, toEndPointY);
181 }
182
183 public float getRightBorder() {
184 return Math.max(super.getXPos(), this.xEndPoint);
185 }
186
187 public float getLeftBorder() {
188 return Math.min(super.getXPos(), this.xEndPoint);
189 }
190
191 public float getTopBorder() {
192 return Math.min(super.getYPos(), this.yEndPoint);
193 }
194
195 public float getBottomBorder() {
196 return Math.max(super.getYPos(), this.yEndPoint);
197 }
198}
Line(float startX, float startY, float endX, float endY, Color color)
Definition: Line.java:22
void setLineWidth(float lineWidth)
Definition: Line.java:73
void setStartAndEndPoint(float toStartPointX, float toStartPointY, float toEndPointX, float toEndPointY)
Definition: Line.java:178
void setStartPoint(Point start)
Definition: Line.java:108
Line(Point start, Point end, Color color, float lineWidth)
Definition: Line.java:65
void setColor(Color newColor)
Definition: Line.java:121
void setStartPoint(float xStartPoint, float yStartPoint)
Definition: Line.java:104
void move(float dx, float dy)
Definition: Line.java:126
void setPosition(Point p)
Definition: Line.java:166
void setEndPoint(float xEndPoint, float yEndPoint)
Definition: Line.java:112
void setPosition(float x, float y)
Definition: Line.java:131
Line(float startX, float startY, float endX, float endY, Color color, float lineWidth)
Definition: Line.java:41
Line(Point start, Point end, Color color)
Definition: Line.java:53
void setEndPoint(Point end)
Definition: Line.java:117