Re: Draw a scaled arrow

Discussion in 'Java' started by Knute Johnson, May 18, 2008.

  1. 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) && (p.y == yHeadPoints)) {
    > 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 *= scale;
    > System.out.println("getXHeadPoint After " +
    > xHeadPoints);
    > }
    >
    > for (int i = 0; i < yHeadPoints.length; ++i) {
    > System.out.println("getYHeadPoint Before " +
    > yHeadPoints);
    > i *= scale;
    > System.out.println("getYHeadPoint After " +
    > yHeadPoints);
    > }
    > }*/
    >
    > 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
     
    Knute Johnson, May 18, 2008
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bill Priess
    Replies:
    0
    Views:
    418
    Bill Priess
    Aug 5, 2003
  2. steve smith
    Replies:
    2
    Views:
    715
    Jim Sculley
    Jul 13, 2003
  3. active
    Replies:
    15
    Views:
    756
    active
    Jun 27, 2007
  4. Roedy Green

    Re: Draw a scaled arrow

    Roedy Green, May 19, 2008, in forum: Java
    Replies:
    3
    Views:
    527
    John B. Matthews
    May 21, 2008
  5. Replies:
    10
    Views:
    322
    John B. Matthews
    May 2, 2013
Loading...

Share This Page