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:

Lines can be drawn now...

Posted by Kishori Sharan on January 26, 2002 at 1:21 PM

Here is your working class. It also draws lines when you double click a button and then you click on another button. There was some design problem with this program.
1. getButton method doesn't always return a button. So, in clicked event I made a check that if there is no prepared button then don't proceed.
2. Your Vectors should be made static because you don't want to keep a list of buttons added for every button. You need only one list.
3. You were adding JButton in vector when you called addButtonMethod. However, JButton dosn't have any prepared attribute and so when you were calling getButton then it will never find a button which is prepared. So, I extended AddButtton from JButton and now I am adding an instance of AddButton to vector. So that when you want to look for any prepared button then you can find it.
4. I have added my comments where I changed the code.
5. There are some changes e.g. instead of calling button.amethod() now we have to call this.amethod().

6. I have drawn lines when you click on a button. However, I think you are planning to keep all lines co-ordinates in some place so that when you resize your frame you can restore the lines already drawn. YOu will have to implement that code.

/////////////////// AvinSplitPane.java /////
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.util.*;
import java.util.Vector;
import java.awt.geom.*;

class AvinSplitPane {
// class that starts the program
public static void main(String[] args){
MainFrame mainframe = new MainFrame();
}
}

/*---------------------------------------------------*/

class MainFrame {
// declare global variables which will be used in the
// constructor, the methods and in inner classes here

protected Graphics offscreen = null ;// MouseHandler has to be able to see it


private JPanel pane2;
private MouseHandler mice;
private JButton button1 ;

public MainFrame() {

// frame to hold split-pane
JFrame frame = new JFrame("Join the Buttons");

// panel to hold the add-button
JPanel pane1 = new JPanel();

// panel to hold the network
pane2 = new JPanel();

// set pane2's layout to null in order to take control of layouting
pane2.setLayout(null);

// build split-pane
JSplitPane splitPane = new JSplitPane(
JSplitPane.HORIZONTAL_SPLIT,pane1, new JScrollPane(pane2));
splitPane.setOneTouchExpandable(true);
splitPane.setDividerLocation(150);

// create add-button and add some action to it
button1 = new JButton("Click to add Button1");
button1.setMnemonic(KeyEvent.VK_I);
button1.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

/*call static method , put pane2 and button1 inton the method also*/
AddButton addbutton = new AddButton();
addbutton.addButtonMethod(pane2);

MainFrame.this.pane2.updateUI();

}
});

// add-button1 to pane1
pane1.add( button1 );

// create mouseHandler and give it a reference to the frame as parameter
mice = new MouseHandler( frame );

// refresh screen when frame is resized
frame.addComponentListener(new ComponentAdapter() {
public void componentResized(ComponentEvent ce) {
//call the drawLines method in MouseHandler
MainFrame.this.mice.drawLines();
}
});

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(splitPane);
frame.setSize(400,400);
frame.setVisible(true);
}
}


/*---------------------------------------------------*/
/** Code addition -- Extended AddButton from JButton -- Kishori **/
class AddButton extends JButton {


int x1 , y1 , x2, y2;
private static int i=0;
int xOffset , yOffset ;
private JPanel pane2 ;
private boolean prepared ;
private static Vector msButtons = new Vector() ;
private static Vector myButtons1 = new Vector() ;


public AddButton() {}

public void addButtonMethod(JPanel paneEx) {
pane2 = paneEx ;

this.setText ("MS"+i);


this.addMouseListener(new MouseListener(){

public void mousePressed(MouseEvent e) {
xOffset = e.getX();
yOffset = e.getY();
}

public void mouseClicked(MouseEvent e) {

if ( e.getClickCount() == 1) {

//set.setPrepared(false,button);
setPrepared( false );

pane2.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));

JButton buf = getButton(msButtons);

/** code addition starts -- Kishori **/
// If there is no button already prepared then it will return null and then
// we should not proceed
if ( buf == null ) {
System.out.println ( "A null button was retuned :" ) ;
return;
}
else {
System.out.println ( "A button was retuned :" + buf.getText ( ) ) ;
}
/** Code addition ends -- kishori **/

//gets the cordinates of the double clicked button and the clicked
// on button,

int x1 = AddButton.this.getX();
int x2 = buf.getX();
int y1 = AddButton.this.getY();
int y2 = buf.getY();

/* this just prints the cordinates of the clicked on button and clicked button*/
System.out.println("Line will be drawn " + x1+" "+ y1+" "+ x2 +" "+ y2 );

// Draw the line
Graphics g = pane2.getGraphics ( ) ;
g.drawLine ( x1, y1, x2, y2 ) ;

setPrepared(false);
pane2.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
}

if (e.getClickCount() == 2) {

pane2.setCursor(new Cursor(Cursor.CROSSHAIR_CURSOR));

//AddButton button1 = new AddButton();
setPrepared(true);
}

}
public void mouseReleased(MouseEvent e){}

public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}


});

this.addMouseMotionListener(new MouseMotionAdapter() {

public void mousePressed(MouseEvent mee) {
// store cursor-position relative to active component

}

public void mouseDragged(MouseEvent mee) {

/*Minus the offset so that the curser does not jump to the
upper left hand side of button when dragging it. However it does not work*/
x1 = mee.getComponent().getX() + mee.getX() - xOffset;
y1 = mee.getComponent().getY() + mee.getY() - yOffset;

AddButton.this.setLocation(x1, y1);
}
});

// ensure visibility in null-layout
this.setBounds(5,5,70,40);

/* add button to pane2*/
pane2.add( this );

//adds button created to the VECTOR msButtons
msButtons.addElement( this );
i++ ;
//return button ;
}

public void setPrepared(boolean isIt ) {
//button = buttonEx ;
prepared = isIt;

// toggle button-color to show something is going on
if (prepared) {
this.setBackground(Color.blue);
}
else {
this.setBackground(SystemColor.control);
}
}

public boolean isPrepared() {
// true if button is supposed to be connected to the next
// button.

return prepared;
}


/* this class clones the button Vector and checks to see if any of the buttons
are prepared ie. Double Clicked on! */

public JButton getButton(Vector buttonsEx ) {
synchronized(this) {
myButtons1 = (Vector)buttonsEx.clone();
}

System.out.println ( "Sixe of vector is :" + buttonsEx.size ( ) ) ;

for (int i = 0; i < myButtons1.size(); i++) {
/*I think the problem is here*/
// Get the button
AddButton tempButton = (AddButton) myButtons1.elementAt(i) ;
if ( tempButton.isPrepared() == true ) {
System.out.println("Hi Guy");
return (JButton)myButtons1.elementAt(i);
}
else {
}
}
System.out.println("Returing null from get button");
return null;
}

}


/*********************************/
class MouseHandler {
private JFrame frame;
public MouseHandler(JFrame myFrame){
frame = myFrame ;
}

public void drawLines(){
// not implemented yet
}
}




Replies:

Sponsored Links



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