Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Re: Draw a scaled arrow

Reply
Thread Tools

Re: Draw a scaled arrow

 
 
Knute Johnson
Guest
Posts: n/a
 
      05-18-2008
RichT wrote:
> 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.


I can't tell what you are doing in that code so maybe just a design
suggestion. Draw your image and your arrow with the same scale so that
when you draw it at 1:1 it will already be correct.

And for my curiosity, what are you doing with this arrow? Are you
dragging it around with a mouse? Does it change direction?

--

Knute Johnson
email s/nospam/linux/

--
Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
------->>>>>>http://www.NewsDemon.com<<<<<<------
Unlimited Access, Anonymous Accounts, Uncensored Broadband Access
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Draw a scaled arrow Roedy Green Java 3 05-21-2008 12:48 AM
I want an image in a tablecell to be scaled to fit the cell without distorting the image active ASP .Net 15 06-27-2007 01:31 PM
Damned red arrow won't go away! Captain Infinity Firefox 7 07-30-2005 03:38 PM
scaled images Anthony Boor Firefox 1 07-01-2004 07:17 PM
How to draw an arrow at the end of a line????????????? steve smith Java 2 07-13-2003 03:58 PM



Advertisments