Click to See Complete Forum and Search --> : GUI code need help


typeR323
11-21-2005, 08:46 PM
I have a problem with my GUI in java code, and I can't draw points on the mousePanel by using mouse click.

Can anyone help me about this. If so, I will be very appreciate.
Thank you very much.

--------------------------------------------------------------------------
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;


public class MouseEventDemo extends JFrame implements ActionListener {

Point[] pt = new Point[3];
Point clickPoint = null;
int mouseArray[][] = new int[20][2];
int xCoordinate, yCoordinate;
int rowCounter = 20;
JPanel mousePanel, buttonPanel;
JButton OKButton, randomButton, removeButton;

public MouseEventDemo() {

mousePanel = new JPanel();
buttonPanel = new JPanel();
buttonPanel = new JPanel(new GridLayout(1,3)); // 1 row, 3 columns

buttonPanel.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createTitledBorder("Option"),
BorderFactory.createEmptyBorder(2,2,2,2)));


add(mousePanel, BorderLayout.CENTER);
add(buttonPanel, BorderLayout.SOUTH);

OKButton = new JButton("Compute Hull");
randomButton = new JButton("Random");
removeButton = new JButton("Clear");
OKButton.addActionListener(this);
randomButton.addActionListener(this);
removeButton.addActionListener(this);
OKButton.setActionCommand("OK");
randomButton.setActionCommand("Random");
removeButton.setActionCommand("Clear");
buttonPanel.add(OKButton);
buttonPanel.add(randomButton);
buttonPanel.add(removeButton);

MouseHandler handle = new MouseHandler();
mousePanel.addMouseListener(handle);

}

public void actionPerformed(ActionEvent event) {
if("OK".equals(event.getActionCommand())) {

}
if("Random".equals(event.getActionCommand())) {

}
if("Clear".equals(event.getActionCommand())) {

}
}

class MouseHandler implements MouseListener {
public void paint(Graphics g) {
for(int i = 0; i < rowCounter; i++) { g.setColor(getForeground());
g.fillRect(mouseArray[i][0] - 2, mouseArray[i][1] - 2, 4, 4 );
}
repaint();
}


public void mouseClicked(MouseEvent event) {
for(int i = 0; i < rowCounter; i++) {
int xCoordinate = event.getX();
int yCoordinate = event.getY();

mouseArray[i][0] = xCoordinate;
mouseArray[i][1] = yCoordinate;
}
event.consume();
repaint();
}

public void mousePressed(MouseEvent event) {}
public void mouseReleased(MouseEvent event) {}
public void mouseEntered(MouseEvent event) {}
public void mouseExited(MouseEvent event) {}

}

public static void main(String[] args) {
MouseEventDemo DemoFrame = new MouseEventDemo();
DemoFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
DemoFrame.setSize(400,400);
DemoFrame.setVisible(true);
}
}
--------------------------------------------------------------------------