hammet
himekishi
Hallo lieb Leute von Tutorials.de
Ich habe mir eine Klasse geschrieben, die für mich Pfeile zeichnet.
Es funktioniert soweit alles wie es soll, allerdings sind manche Pfeile dicker als andere, bzw. einzelne Teile sind sogar unterschiedlich dick. Und nun zu meiner Frage: Kommt mir das einfach nur so vor? oder zeichnet JAVA 2D einfach nur ungenau?
Natürlich kann ich auch ienen Fehler im Quellcode haben deswegen poste ich ihn gleich mal mit.
Ich würde mich freue wenn ihr vlt eine Lösung kennt. Und wenn nicht, hab ich Pech gehabt *g*
mfg
hammet
Ich habe mir eine Klasse geschrieben, die für mich Pfeile zeichnet.
Es funktioniert soweit alles wie es soll, allerdings sind manche Pfeile dicker als andere, bzw. einzelne Teile sind sogar unterschiedlich dick. Und nun zu meiner Frage: Kommt mir das einfach nur so vor? oder zeichnet JAVA 2D einfach nur ungenau?
Natürlich kann ich auch ienen Fehler im Quellcode haben deswegen poste ich ihn gleich mal mit.
Code:
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Polygon;
/**
* @TK
*
* Folgendes auswählen, um die Schablone für den erstellten Typenkommentar zu ändern:
* Fenster>Benutzervorgaben>Java>Codegenerierung>Code und Kommentare
*/
public class Arrow {
public void drawArrow(
Graphics g,
int xstart,
int ystart,
int width,
int length,
int peaklength,
char bias,
int expansion) {
Point point1 = new Point(xstart, ystart);
Point point2 = new Point();
Point point3 = new Point();
Point point4 = new Point();
Point point5 = new Point();
Point point6 = new Point();
Point point7 = new Point();
Point point8 = new Point();
Point point9 = new Point();
Point point10 = new Point();
Point point11 = new Point();
Point point12 = new Point();
Point point13 = new Point();
boolean schalter = false;
if (bias == 'r') {
//point2 berechnen
point2.x = point1.x - (int) ((peaklength * Math.cos(Math.toRadians(45.0))) + 0.5);
point2.y = point1.y - (int) ((peaklength * Math.sin(Math.toRadians(45.0))) + 0.5);
//point3 berechnen
point3.x = point2.x - (int) ((width * Math.cos(Math.toRadians(45.0))) + 0.5);
point3.y = point2.y + (int) ((width * Math.sin(Math.toRadians(45.0))) + 0.5);
//point5 berechnen
point5.x = point1.x - length;
point5.y = point1.y - (int) ((width / 2) + 0.5);
//point6 berechnen
point6.x = point1.x - length;
point6.y = point1.y + (int) ((width / 2) + 0.5);
//point9 berechnen
point9.x = point1.x - (int) ((peaklength * Math.cos(Math.toRadians(45.0))) + 0.5);
point9.y = point1.y + (int) ((peaklength * Math.sin(Math.toRadians(45.0))) + 0.5);
//point8 berechnen
point8.x = point9.x - (int) ((width * Math.cos(Math.toRadians(45.0))) + 0.5);
point8.y = point9.y - (int) ((width * Math.sin(Math.toRadians(45.0))) + 0.5);
/* point4 und point7 zum Schluss,
* da diese Berechnungen auf vorhergehende beruhen
*/
//point4 berechnen
point4.x =
point3.x
+ (int) (((point5.y - point3.y) / Math.tan(Math.toRadians(45.0))) + 0.5);
point4.y = point3.y + (point5.y - point3.y);
//point7 berechnen
point7.x =
point8.x + (int) ((point8.y - point6.y) / Math.tan(Math.toRadians(45.0)) + 0.5);
point7.y = point8.y - (point8.y - point6.y);
if (expansion == 0) {
schalter = false;
} else {
//point10 berechnen
point10.x = point1.x + expansion;
point10.y = point1.y - (int) ((width / 2.0) + 0.5);
//point11 berechnen
point11.x = point1.x + expansion;
point11.y = point1.y + (int) ((width / 2.0) + 0.5);
//point13 berechnen
point13.x =
point1.x - (int) (((width / 2.0) * Math.tan(Math.toRadians(45.0))) + 0.5);
point13.y = point1.y + (int) ((width / 2.0) + 0.5);
//point12 berechnen
point12.x =
point1.x - (int) (((width / 2.0) * Math.tan(Math.toRadians(45.0))) + 0.5);
point12.y = point1.y - (int) ((width / 2.0) + 0.5);
schalter = true;
}
}
if (bias == 'l') {
//point2 berechnen
point2.x = point1.x + (int) ((peaklength * Math.cos(Math.toRadians(45.0))) + 0.5);
point2.y = point1.y + (int) ((peaklength * Math.sin(Math.toRadians(45.0))) + 0.5);
//point3 berechnen
point3.x = point2.x + (int) ((width * Math.cos(Math.toRadians(45.0))) + 0.5);
point3.y = point2.y - (int) ((width * Math.sin(Math.toRadians(45.0))) + 0.5);
//point5 berechnen
point5.x = point1.x + length;
point5.y = point1.y + (int) ((width / 2) + 0.5);
//point6 berechnen
point6.x = point1.x + length;
point6.y = point1.y - (int) ((width / 2) + 0.5);
//point9 berechnen
point9.x = point1.x + (int) ((peaklength * Math.cos(Math.toRadians(45.0))) + 0.5);
point9.y = point1.y - (int) ((peaklength * Math.sin(Math.toRadians(45.0))) + 0.5);
//point8 berechnen
point8.x = point9.x + (int) ((width * Math.cos(Math.toRadians(45.0))) + 0.5);
point8.y = point9.y + (int) ((width * Math.sin(Math.toRadians(45.0))) + 0.5);
/* point4 und point7 zum Schluss,
* da diese Berechnungen auf vorhergehende beruhen
*/
//point4 berechnen
point4.x =
point3.x
+ (int) (((point5.y - point3.y) / Math.tan(Math.toRadians(45.0))) + 0.5);
point4.y = point3.y + (point5.y - point3.y);
//point7 berechnen
point7.x =
point8.x + (int) ((point8.y - point6.y) / Math.tan(Math.toRadians(45.0)) + 0.5);
point7.y = point8.y - (point8.y - point6.y);
if (expansion == 0) {
schalter = false;
} else {
//point10 berechnen
point10.x = point1.x - expansion;
point10.y = point1.y + (int) ((width / 2.0) + 0.5);
//point11 berechnen
point11.x = point1.x - expansion;
point11.y = point1.y - (int) ((width / 2.0) + 0.5);
//point12 berechnen
point12.x =
point1.x + (int) (((width / 2.0) * Math.tan(Math.toRadians(45.0))) + 0.5);
point12.y = point1.y + (int) ((width / 2.0) + 0.5);
//point13 berechnen
point13.x =
point1.x + (int) (((width / 2.0) * Math.tan(Math.toRadians(45.0))) + 0.5);
point13.y = point1.y - (int) ((width / 2.0) + 0.5);
schalter = true;
}
}
if (bias == 'd') {
//point2 berechnen
point2.x = point1.x + (int) ((peaklength * Math.sin(Math.toRadians(45.0))) + 0.5);
point2.y = point1.y - (int) ((peaklength * Math.cos(Math.toRadians(45.0))) + 0.5);
//point3 berechnen
point3.x = point2.x - (int) ((width * Math.sin(Math.toRadians(45.0))) + 0.5);
point3.y = point2.y - (int) ((width * Math.cos(Math.toRadians(45.0))) + 0.5);
//point5 berechnen
point5.x = point1.x + (int) ((width / 2) + 0.5);
point5.y = point1.y - length;
//point6 berechnen
point6.x = point1.x - (int) ((width / 2) + 0.5);
point6.y = point1.y - length;
//point9 berechnen
point9.x = point1.x - (int) ((peaklength * Math.sin(Math.toRadians(45.0))) + 0.5);
point9.y = point1.y - (int) ((peaklength * Math.cos(Math.toRadians(45.0))) + 0.5);
//point8 berechnen
point8.x = point9.x + (int) ((width * Math.sin(Math.toRadians(45.0))) + 0.5);
point8.y = point9.y - (int) ((width * Math.cos(Math.toRadians(45.0))) + 0.5);
/* point4 und point7 zum Schluss,
* da diese Berechnungen auf vorhergehende beruhen
*/
//point4 berechnen
point4.x = point1.x + (int) ((width / 2) + 0.5);
point4.y =
point3.y
- (int) (((point5.x - point3.x) * Math.tan(Math.toRadians(45.0))) + 0.5);
//point7 berechnen
point7.x = point1.x - (int) ((width / 2) + 0.5);
point7.y =
point8.y
- (int) (((point8.x - point6.x) * Math.tan(Math.toRadians(45.0))) + 0.5);
if (expansion == 0) {
schalter = false;
} else {
//point10 berechnen
point10.x = point1.x + (int) ((width / 2.0) + 0.5);
point10.y = point1.y + expansion;
//point11 berechnen
point11.x = point1.x - (int) ((width / 2.0) + 0.5);
point11.y = point1.y + expansion;
//point12 berechnen
point13.x = point1.x - (int) ((width / 2.0) + 0.5);
point13.y =
point1.y - (int) (((width / 2.0) * Math.tan(Math.toRadians(45.0))) + 0.5);
//point12 berechnen
point12.x = point1.x + (int) ((width / 2.0) + 0.5);
point12.y =
point1.y - (int) (((width / 2.0) * Math.tan(Math.toRadians(45.0))) + 0.5);
schalter = true;
}
}
if (bias == 'u') {
//point2 berechnen
point2.x = point1.x - (int) ((peaklength * Math.sin(Math.toRadians(45.0))) + 0.5);
point2.y = point1.y + (int) ((peaklength * Math.cos(Math.toRadians(45.0))) + 0.5);
//point3 berechnen
point3.x = point2.x + (int) ((width * Math.sin(Math.toRadians(45.0))) + 0.5);
point3.y = point2.y + (int) ((width * Math.cos(Math.toRadians(45.0))) + 0.5);
//point5 berechnen
point5.x = point1.x - (int) ((width / 2) + 0.5);
point5.y = point1.y + length;
//point6 berechnen
point6.x = point1.x + (int) ((width / 2) + 0.5);
point6.y = point1.y + length;
//point9 berechnen
point9.x = point1.x + (int) ((peaklength * Math.sin(Math.toRadians(45.0))) + 0.5);
point9.y = point1.y + (int) ((peaklength * Math.cos(Math.toRadians(45.0))) + 0.5);
//point8 berechnen
point8.x = point9.x - (int) ((width * Math.sin(Math.toRadians(45.0))) + 0.5);
point8.y = point9.y + (int) ((width * Math.cos(Math.toRadians(45.0))) + 0.5);
/* point4 und point7 zum Schluss,
* da diese Berechnungen auf vorhergehende beruhen
*/
//point4 berechnen
point4.x = point1.x - (int) ((width / 2) + 0.5);
point4.y =
point3.y
- (int) (((point5.x - point3.x)
* (Math.tan(Math.toRadians(45.0))))
+ 0.5);
//point7 berechnen
point7.x = point1.x + (int) ((width / 2) + 0.5);
point7.y =
point8.y
- (int) (((point8.x - point6.x) / Math.tan(Math.toRadians(45.0))) + 0.5);
if (expansion == 0) {
schalter = false;
} else {
//point10 berechnen
point10.x = point1.x - (int) ((width / 2.0) + 0.5);
point10.y = point1.y - expansion;
//point11 berechnen
point11.x = point1.x + (int) ((width / 2.0) + 0.5);
point11.y = point1.y - expansion;
//point13 berechnen
point13.x = point1.x + (int) ((width / 2.0) + 0.5);
point13.y =
point1.y + (int) (((width / 2.0) * Math.tan(Math.toRadians(45.0))) + 0.5);
//point12 berechnen
point12.x = point1.x - (int) ((width / 2.0) + 0.5);
point12.y =
point1.y + (int) (((width / 2.0) * Math.tan(Math.toRadians(45.0))) + 0.5);
schalter = true;
}
}
Polygon p = new Polygon();
if (schalter) {
p.addPoint(point10.x, point10.y);
p.addPoint(point11.x, point11.y);
p.addPoint(point13.x, point13.y);
p.addPoint(point9.x, point9.y);
p.addPoint(point8.x, point8.y);
p.addPoint(point7.x, point7.y);
p.addPoint(point6.x, point6.y);
p.addPoint(point5.x, point5.y);
p.addPoint(point4.x, point4.y);
p.addPoint(point3.x, point3.y);
p.addPoint(point2.x, point2.y);
p.addPoint(point12.x, point12.y);
} else {
p.addPoint(point1.x, point1.y);
p.addPoint(point2.x, point2.y);
p.addPoint(point3.x, point3.y);
p.addPoint(point4.x, point4.y);
p.addPoint(point5.x, point5.y);
p.addPoint(point6.x, point6.y);
p.addPoint(point7.x, point7.y);
p.addPoint(point8.x, point8.y);
p.addPoint(point9.x, point9.y);
}
g.fillPolygon(p);
}
}
Ich würde mich freue wenn ihr vlt eine Lösung kennt. Und wenn nicht, hab ich Pech gehabt *g*
mfg
hammet