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:

Can some spot the error..? Its about vectors somewhat!!

Posted by Avin Sinanan on January 26, 2002 at 3:08 AM

Hi there, Ok the peice of code I am about to show you is a bit long. But most of the cod eis very simple.The code is suppose to do the following
1)When you click the button on LHS JPanel a button is added to the RHS JPanl. If you click the buttons again more buttons are added.
Also you can drag the added buttons around. It should be noted that all button are added to the same position so you have to drag the butons to see the ones under it.

2)If you double click on a button properly the curser changes to a CROSSHAIR "+ " curser and the button turns dark blue. It should be noted that all the added buttons are stored in a vector.

3)When you add more that one buttons to the screen do the following. Double click on one of the buttons till the cursers changes to "+" then click once on another button. The curser changes back to normal. The problem is that the program is suppose to print the cordinates of the double clicked button and the button single clicked on. This is not happening. In line 128 of the code you will see the code that is suppsoe to print these cordinates.
I think the problem is that the code cannot detremine if buttons were "prepared" in line 222.
The whole idea of this program later on is to make the program such that when you double click on one button and signle click on another button a line appears and connects the two buttons.

here is the code.. thanks in advance :

code->


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,button1);

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


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

class AddButton {


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


public AddButton() {}

public void addButtonMethod(JPanel paneEx , JButton buttonEx){
pane2 = paneEx ;
button = buttonEx ;

button = new JButton("MS"+i);


button.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);

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

int getX1 = button.getX();
int getX2 = buf.getX();
int getY1 = button.getY();
int getY2 = buf.getY();

/* this just prints the cordinates of the clicked on button and clicked button*/
System.out.println("Line will be drawn " + getX1+" "+ getY1+" "+ getX2 +" "+ getY2 );

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){}


});

button.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;

button.setLocation(x1, y1);
}
});

// ensure visibility in null-layout
button.setBounds(5,5,70,40);
/* add button to pane2*/
pane2.add(button);
//adds button created to the VECTOR msButtons
msButtons.addElement(button);
i++ ;
//return button ;
}

public void setPrepared(boolean isIt ) {
//button = buttonEx ;
prepared = isIt;
// toggle button-color to show something is going on
if (prepared) {
button.setBackground(Color.blue);
} else {
button.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();

}


for (int i = 0; i < myButtons1.size(); i++)
{
/*I think the problem is here*/
if (isPrepared()==true )
{
System.out.println("Hi Guy");
return (JButton)myButtons1.elementAt(i);

}
else
{

}
}
return null;
}


}


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

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

--------------------

Thanks a lot





Replies:

Sponsored Links



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