Results 1 to 6 of 6
  1. #1
    sandhyau is offline Member
    Join Date
    Jan 2008
    Posts
    7
    Rep Power
    0

    Default how to draw an arrow mark using java swing

    how to draw an arrow mark in a straight line using java swing?.

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
    import java.awt.*;
    import java.awt.geom.*;
    import javax.swing.*;
    import javax.swing.event.*;
    
    public class Arrow extends JPanel implements ChangeListener {
        Path2D.Double arrow = createArrow();
        double theta = 0;
    
        public void stateChanged(ChangeEvent e) {
            int value = ((JSlider)e.getSource()).getValue();
            theta = Math.toRadians(value);
            repaint();
        }
    
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D)g;
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                                RenderingHints.VALUE_ANTIALIAS_ON);
            int cx = getWidth()/2;
            int cy = getHeight()/2;
            AffineTransform at = AffineTransform.getTranslateInstance(cx, cy);
            at.rotate(theta);
            at.scale(2.0, 2.0);
            Shape shape = at.createTransformedShape(arrow);
            g2.setPaint(Color.blue);
            g2.draw(shape);
        }
    
        private Path2D.Double createArrow() {
            int length = 80;
            int barb = 15;
            double angle = Math.toRadians(20);
            Path2D.Double path = new Path2D.Double();
            path.moveTo(-length/2, 0);
            path.lineTo(length/2, 0);
            double x = length/2 - barb*Math.cos(angle);
            double y = barb*Math.sin(angle);
            path.lineTo(x, y);
            x = length/2 - barb*Math.cos(-angle);
            y = barb*Math.sin(-angle);
            path.moveTo(length/2, 0);
            path.lineTo(x, y);
            return path;
        }
    
        private JSlider getSlider() {
            JSlider slider = new JSlider(-180, 180, 0);
            slider.addChangeListener(this);
            return slider;
        }
    
        public static void main(String[] args) {
            Arrow test = new Arrow();
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.add(test);
            f.add(test.getSlider(), "Last");
            f.setSize(400,400);
            f.setLocationRelativeTo(null);
            f.setVisible(true);
        }
    }

  3. #3
    sandhyau is offline Member
    Join Date
    Jan 2008
    Posts
    7
    Rep Power
    0

    Default

    thanks for u r help. i have dought in the code. what is path2D. i am getting error in that .is it path2D is the seperate class?

  4. #4
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Path2D is new in j2se 1.6. For earlier versions use GeneralPath instead.

  5. #5
    sandhyau is offline Member
    Join Date
    Jan 2008
    Posts
    7
    Rep Power
    0

    Default

    thank you.

  6. #6
    sandhyau is offline Member
    Join Date
    Jan 2008
    Posts
    7
    Rep Power
    0

    Default i can draw an arrow mark in a line but the arrow is not rotating when the line move.

    i can draw an arrow mark in a line but the arrow is not rotating when the line moves. here i am placing the code . can any one help me that line should move as well as arrow mark should also move in the same direction.please send as soon as possible.thanks.

    AffineTransform at = AffineTransform.getTranslateInstance((point2.x + point4.x)/2, (point2.y + point4.y)/2);
    at.rotate(angle);
    int length = 6;
    int b = 10;
    double angle = Math.toRadians(45);
    GeneralPath path = new GeneralPath();
    path.moveTo(5, 6);
    path.moveTo(0,0);
    // Double d = new Double(angle);
    // int a = d.intValue();
    int x = (int) (-b* Math.cos(angle));
    int y = (int) (b * Math.sin(angle));
    path.lineTo(x, y);
    x = (int) (-b * Math.cos(-angle));
    y = (int) (b * Math.sin(-angle));
    path.moveTo(5, 6);
    path.moveTo(0,0);
    path.lineTo(x, y);
    Shape shape = at.createTransformedShape(path);
    // graphics2D.setPaint(Color.black);
    graphics2D.draw(shape);
    drawLine(graphics2D, point2.x, point2.y, point4.x, point4.y);

Similar Threads

  1. Mark thread RESOLVED.
    By Eranga in forum Suggestions & Feedback
    Replies: 45
    Last Post: 04-02-2008, 11:34 AM
  2. Replies: 1
    Last Post: 02-17-2008, 12:22 AM
  3. Draw an arrow
    By Albert in forum SWT / JFace
    Replies: 3
    Last Post: 02-01-2008, 09:11 AM
  4. BufferedReader .mark(int readAheadLimit)
    By ShoeNinja in forum New To Java
    Replies: 1
    Last Post: 11-16-2007, 11:58 PM
  5. how to draw in Java
    By Heather in forum AWT / Swing
    Replies: 2
    Last Post: 07-12-2007, 12:01 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •