2

Possible Duplicate:
How to draw a directed arrow line in Java?

I am trying to draw a line/arrow between two labels in a JFrame. I know the particular coordinates to both of these labels marked as (x1,y1) (x2,y2). How can I draw a line/arrow between them?.

Community
  • 1
  • 1
arya
  • 33
  • 2
  • 8

3 Answers3

2

This should help http://java-sl.com/connector.html

StanislavL
  • 56,971
  • 9
  • 68
  • 98
0

You actually should use some development platform IDE like Netbeans or some swing specialist IDE's, as they helps you a lot during development.
Using an IDE, you can do it as drag and drop and accordingly it will automatically generate the code.
Not only drawing a line, but you can do all the work with ease. Try it out....

Addicted
  • 1,694
  • 1
  • 16
  • 24
-1

it's not nice but it's running:

public class Example extends JFrame {

private static JLabel a;
private static JLabel b;

public static void main(String[] args) {

    Example example = new Example();

    JPanel panel = new JPanel();
    panel.setLayout(null);

    a = new JLabel("a");
    a.setBounds(50, 50, 10, 10);

    b = new JLabel("b");
    b.setBounds(150, 150, 10, 10);

    panel.add(a);
    panel.add(b);

    example.getContentPane().add(panel);

    example.setGlassPane(new MyGlas());
    example.getGlassPane().setVisible(true);

    example.setSize(400, 400);

    example.setVisible(true);
}

public static class MyGlas extends JComponent {

    public void paint(Graphics g) {

        Rectangle aBounds = a.getBounds();
        Rectangle bBounds = b.getBounds();

        g.drawLine(aBounds.x, aBounds.y, bBounds.x, bBounds.y);
    }
}
}
Thomas Uhrig
  • 30,811
  • 12
  • 60
  • 80
  • : on using ur code i get an error non-static method cannot be accessed from static context from getContentPane method to setVisible. Can u please help.. – arya May 01 '12 at 10:53
  • 1
    If you just copy and past the code into a new class file, it works definitely. So, what have you changed? Post the code and the exact error. – Thomas Uhrig May 01 '12 at 11:13
  • in ur code m declaring jlabels as static,by doing this m getting an error for getContentPane() method that static content cannot be referenced by this method. – arya May 01 '12 at 15:34
  • if you don't post your changes, I can't help you. – Thomas Uhrig May 01 '12 at 15:48
  • This is horrible coding practice and defeats the Object Oriented Programing style. Also, it is very discouraged to use a null layout. – Shawn Shroyer May 01 '12 at 17:39
  • Also, why would you paint on the glass pane? Why not just paint right on the JPanel that you already created? – Shawn Shroyer May 01 '12 at 17:40
  • It's a solution for his problem in just a couple of lines. Of course, I can create a bunch of getter- and setter-methods, some interfaces and a fancy factory. But why? To show him how to draw a line? AND: I use a null layout, so I can position the buttons where ever I want using setBounds. AND I draw on the glasspane because because it's the top pane and I have to overwrite a paint(...) method. There is no OOP practice validated, just two fields are static and public because it's less code for an example... – Thomas Uhrig May 01 '12 at 17:52