Draw a scaled arrow
- From: RichT <someone@xxxxxxxxxxxxx>
- Date: Sun, 18 May 2008 15:52:25 +0100
Hi,
I have managed to draw an arrow at a 1:1 scale, but I am having problems
drawing this correctly when the image I am drawing to is scaled.
I am having 2 problems.
1. Drawing the arrow so that it is scaled on to the canvas when I tried multiplying by a scale factor of 1.5 various combinations produced either a distorted arrow head or a line and arrow head that were drawn
in different places, and still the thickness of the line did not seem to scale either.
2 Drawing the Arrow in the actual BufferedImage, the arrow and line are correctly aligned but drawn at a different place in the image.
What I ideally want is when the arrow is being drawn on the scaled canvas, it is drawn to scale with the image and when I want to make it permanent, draw it to the image at 1:1 from the scaled coords.
The BufferedImage size never changes, and a scaled version of it is drawn to the canvas.
Below is the code that sets the x y values from the mouseDragged event.
arrow.setEndX(me.getX());
aeadArrow.setEndY(me.getY());
repaint();
Below is the code I am using to draw the canvas & the image, the arguments scale passed to the draw method are what I was using to scale the arrow, anything from 1.0 to 4.0
public class Arrow {
private static final double HEAD_LENGTH = 15;
private static final double HEAD_WIDTH = 7; // actually half the width
private static final double TAN = getHEAD_WIDTH() / getHEAD_LENGTH();
private int x0;
private int y0;
private int x1;
private int y1;
private int[] xHeadPoints = new int[3];
private int[] yHeadPoints = new int[3];
public Arrow() {
}
public int[] getXHeadPoints() {
return xHeadPoints;
}
public int[] getYHeadPoints() {
return yHeadPoints;
}
public int getXHeadPoint(int index) {
if ((index >= 0) && (index < xHeadPoints.length)) {
return xHeadPoints[index];
} else {
return 0;
}
}
public int getYHeadPoint(int index) {
if ((index >= 0) && (index < yHeadPoints.length)) {
return yHeadPoints[index];
} else {
return 0;
}
}
public void setXHeadPoint(int index, int val) {
if ((index >= 0) && (index < xHeadPoints.length)) {
xHeadPoints[index] = val;
}
}
public void setYHeadPoint(int index, int val) {
if ((index >= 0) && (index < yHeadPoints.length)) {
yHeadPoints[index] = val;
}
}
public Rectangle getBoundingRect() {
return new Rectangle(x0, y0, (x1 - x0), (y1 - y0));
}
public boolean containsPoint(Point p) {
boolean containsPoint = false;
for (int i = 0; i < xHeadPoints.length; ++i) {
if ((p.x == xHeadPoints[i]) && (p.y == yHeadPoints[i])) {
containsPoint = true;
}
}
return containsPoint;
}
public int getStartX() {
return x0;
}
public int getEndX() {
return x1;
}
public int getStartY() {
return y0;
}
public int getEndY() {
return y1;
}
public void setStartX(int newXStart) {
x0 = newXStart;
}
public void setEndX(int newXEnd) {
x1 = newXEnd;
}
public void setStartY(int newYStart) {
y0 = newYStart;
}
public void setEndY(int newYEnd) {
y1 = newYEnd ;
}
public void draw(Graphics g, double scale, boolean isDrawingToImage) {
int dx = (int)(((x1) - (x0 )));
int dy = (int)(((y1) - (y0)));
double lineLength = Math.sqrt((dx * dx) + (dy * dy));
double ratio = (getHEAD_LENGTH() / lineLength);
xHeadPoints[0] = (int)((x0 + (int) Math.round((1 - ratio) * dx
+ (ratio * dy) * getTAN())));
yHeadPoints[0] = (int)((y0 + (int) Math.round((1 - ratio) * dy
- (ratio * dx) * getTAN())) );
xHeadPoints[1] = (int)((x0 + (int) Math.round((1 - ratio) * dx
- (ratio * dy) * getTAN())));
yHeadPoints[1] = (int) ((y0 + (int) Math.round((1 - ratio) * dy
+ (ratio * dx) * getTAN())));
// 3rd point of arrow head is line end
xHeadPoints[2] = (int)(x1);
yHeadPoints[2] = (int)(y1 );
g.setColor(Color.WHITE);
/*if (isDrawingToImage) {
g.drawLine((int)(x0 / scale), (int)(y0 / scale),
(int)(x1 / scale), (int)(y1 / scale));
} else {*/
g.drawLine((int)(x0), (int)(y0),
(int)(x1), (int)(y1));
//}
System.out.println("x0: " + x0);
System.out.println("x1: " + x1);
System.out.println("y0: " + y0);
System.out.println("y1: " + y1);
System.out.println("Tan: " + TAN);
/* if (isDrawingToImage) {
for (int i : xHeadPoints) {
System.out.println("getXHeadPoint Before " + i);
i /= scale;
System.out.println("getXHeadPoint After " + i);
}
for (int i : yHeadPoints) {
System.out.println("getYHeadPoint Before " + i);
i /= scale;System.out.println("getYHeadPoint After " + i);
}
} else {
for (int i = 0; i < xHeadPoints.length; ++i) {
System.out.println("getXHeadPoint Before " + xHeadPoints[i]);
i *= scale;
System.out.println("getXHeadPoint After " + xHeadPoints[i]);
}
for (int i = 0; i < yHeadPoints.length; ++i) {
System.out.println("getYHeadPoint Before " + yHeadPoints[i]);
i *= scale;
System.out.println("getYHeadPoint After " + yHeadPoints[i]);
}
}*/
g.fillPolygon(getXHeadPoints(), getYHeadPoints(), 3);
//g.draw3DRect(200, 200, 100, 100, true);
g.dispose();
}
/**
* TODO Add method comment.
* @return the hEAD_LENGTH
*/
public static double getHEAD_LENGTH() {
return HEAD_LENGTH;
}
/**
* TODO Add method comment.
* @return the hEAD_WIDTH
*/
public static double getHEAD_WIDTH() {
return HEAD_WIDTH;
}
/**
* TODO Add method comment.
* @return the tAN
*/
public static double getTAN() {
return TAN;
}
}
I would really appreciate some help here
Rich.
.
- Follow-Ups:
- Re: Draw a scaled arrow
- From: Roedy Green
- Re: Draw a scaled arrow
- From: Knute Johnson
- Re: Draw a scaled arrow
- Prev by Date: Re: Where in the world are you calling from?
- Next by Date: Re: Where in the world are you calling from?
- Previous by thread: java & stourstup
- Next by thread: Re: Draw a scaled arrow
- Index(es):
Relevant Pages
|