The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
January 2002

Advertisement

Advertisement

This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.

Message:

How to draw lines to connect buttons ?

Posted by Avin SInanan on January 06, 2002 at 2:26 AM

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 ;
}
}





Replies:

Sponsored Links



Google
  Web Artima.com   
Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us