aspose file tools*
The moose likes Java in General and the fly likes How to draw lines to connect buttons. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to draw lines to connect buttons." Watch "How to draw lines to connect buttons." New topic
Author

How to draw lines to connect buttons.

Avin Sinanan
Ranch Hand

Joined: Nov 07, 2001
Posts: 109
Ok before I ask my Question let me tell you what the program does. When you load the program two button will appear within the window. You can ckick and drag these buttons anywhere you want on the screen. The code for this is very sinple. The code works perfectly. So its obvious that my question is not to find the error in my code. However I want to extend my code to do the following :
(1) I want to add an extra button .. it does not matter where on the screen [ JButton newButton = new JButton() ]
(2) When I press this button I want a line to appear between the two moveable buttons ( ie the two buttons are joined together by the line)
(3)Now here is the hard part.. Whenever I move either of the two buttons the line stretches so that the two buttons are still connected. Hence the line is like a rubberband and will always connect the two button no matter where they are positioned.
Please help in anyway.. any kind of help would be appreciated. Onemust be wondering why I need code for this.. well basicllay am suppose to write the sourse code to smimulate a celluar (GSM) network.

code----->
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.util.*;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
class MoveablePics
{
public static void main(String[] args)
{
JFrame workBench = new JFrame("Work Bench");
final JPanel pane1 = new JPanel();

Picture1 pic1 = new Picture1();
Picture2 pic2 = new Picture2();

JButton moveablePicture1 = pic1.picture1Method1();
JButton moveablePicture2 = pic2.picture1Method2();

pane1.add(moveablePicture1);
pane1.add(moveablePicture2);

workBench.getContentPane().add(pane1);
workBench.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
workBench.setSize(500,500);
workBench.pack();
workBench.setVisible(true) ;

}
}

class Picture1
{
public JButton picture1Method1()
{
JButton picture1 = new JButton();
picture1.addMouseMotionListener(new MouseMotionAdapter() {
public void mouseDragged(MouseEvent me) {
me.getComponent().setLocation(me.getComponent().getX() + me.getX(),
me.getComponent().getY() + me.getY());

}
});

return picture1 ;
}
}

class Picture2
{
public JButton picture1Method2()
{
JButton picture2 = new JButton();
picture2.addMouseMotionListener(new MouseMotionAdapter() {
public void mouseDragged(MouseEvent me) {
me.getComponent().setLocation(me.getComponent().getX() + me.getX(),
me.getComponent().getY() + me.getY());

}
});

return picture2 ;
}
}


yours respectfully<br />Avin Sinanan
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to draw lines to connect buttons.