In a class which creates a menu using Swing I have added 4 items, each one of them is responsible for drawing a circle of different color. I have assigned to each item of that menu an ActionListener just to specify the color of the desired circle. I need following to click the mouse anywhere in the Panel and there the circle to be created. To this effect, I have created another class ScrollPane which implements the mouseListener and it is responsible to draw the circle. I do not know though, how to trigger the mouseListener to the second class which will do the job. This should be done only after the listener of the menu has been invoked. I need obviously a reference to class ScrollPane and then to assign on that the mouseListener.
I do not know however what kind of parameter I should use to the method addMouseListener. Or this is declared in ScrollPane class? I think it is simple in case all tasks are carried out in one class which implements both listeners, but what if I want to separate them?What else should the actionPerformed do? Here is the relevant code:
public class CreateMenu implements ActionListener {
private ScrollPane scrollPane;
public void actionPerformed(ActionEvent e) {
JMenuItem source = (JMenuItem)(e.getSource());
if(source.getText() == "Green Circle")
this.scrollPane.setColor(Color.green);
else if(source.getText() == "Blue Circle")
this.scrollPane.setColor(Color.blue);
else if(source.getText() == "Red Circle")
this.scrollPane.setColor(Color.red);
else if(source.getText() == "Yellow Circle")
this.scrollPane.setColor(Color.yellow);
else
return;
}
}
.
public class ScrollPane extends JPanel implements MouseListener {
private Dimension area;
private Vector<Circle> circles;
private Color color;
private JPanel drawingPane;
public ScrollPane() {
super(new BorderLayout());
area = new Dimension(0,0);
circles = new Vector<Circle>();
//Set up the drawing area.
drawingPane = new DrawingPane();
drawingPane.setBackground(Color.white);
drawingPane.addMouseListener(this);
//Put the drawing area in a scroll pane.
JScrollPane scroller = new JScrollPane(drawingPane);
scroller.setPreferredSize(new Dimension(200,200));
add(scroller, BorderLayout.CENTER);
}
public class DrawingPane extends JPanel {
protected void paintComponent(Graphics g, Color color ) {
super.paintComponent(g);
Rectangle rect;
for (int i = 0; i < circles.size(); i++) {
rect = circles.elementAt(i).getRect();
g.setColor(circles.elementAt(i).getTheColor());
g.fillOval(rect.x, rect.y, rect.width, rect.height);
}
}
}
public void mouseReleased(MouseEvent e) {
final int W = 100;
final int H = 100;
boolean changed = false;
if(SwingUtilities.isLeftMouseButton(e)) {
int x = e.getX() - W/2;
int y = e.getY() - H/2;
if (x < 0) x = 0;
if (y < 0) y = 0;
Rectangle rect = new Rectangle(x, y, W, H);
Circle newCircle = new Circle(rect, this.color);
circles.addElement(newCircle);
drawingPane.scrollRectToVisible(rect);
int this_width = (x + W + 2);
if (this_width > area.width) {
area.width = this_width; changed=true;
}
int this_height = (y + H + 2);
if (this_height > area.height) {
area.height = this_height; changed=true;
}
}
if (changed) {
//Update client's preferred size because
//the area taken up by the graphics has
//gotten larger or smaller (if cleared).
drawingPane.setPreferredSize(area);
//Let the scroll pane know to update itself
//and its scrollbars.
drawingPane.revalidate();
}
drawingPane.repaint();
}
public void mouseClicked(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mousePressed(MouseEvent e){}
public void setColor(Color color) {
this.color = color;
}
public JPanel getDrawingPane() {
return drawingPane;
}
}