Question

Is there any way to add a MouseListener to a Graphic object.
I have this simple GUI that draw an oval.
What I want is handling the event when the user clicks on the oval

import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.*;

public class Gui2 extends JFrame {
 JFrame frame = new JFrame();
 MyDrawPanel drawpanel = new MyDrawPanel();

 public static void main(String[] args) {
  Gui2 gui = new Gui2();
  gui.go();
 }

 public void go() {

  frame.getContentPane().add(drawpanel);
  // frame.addMouseListener(this);

  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  frame.setSize(300, 300);
  frame.setVisible(true);

 }

}

class MyDrawPanel extends JComponent implements MouseListener {

 public void paintComponent(Graphics g) {

  int red = (int) (Math.random() * 255);
  int green = (int) (Math.random() * 255);
  int blue = (int) (Math.random() * 255);
  Color startrandomColor = new Color(red, green, blue);

  red = (int) (Math.random() * 255);
  green = (int) (Math.random() * 255);
  blue = (int) (Math.random() * 255);
  Color endrandomColor = new Color(red, green, blue);

  Graphics2D g2d = (Graphics2D) g;
  this.addMouseListener(this);
  GradientPaint gradient = new GradientPaint(70, 70, startrandomColor,
    150, 150, endrandomColor);

  g2d.setPaint(gradient);
  g2d.fillOval(70, 70, 100, 100);

 }

 @Override
 public void mouseClicked(MouseEvent e) {
  if ((e.getButton() == 1)
    && (e.getX() >= 70 && e.getX() <= 170 && e.getY() >= 70 && e
      .getY() <= 170)) {
   this.repaint();
   // JOptionPane.showMessageDialog(null,e.getX()+ "\n" + e.getY());
  }

 }

 @Override
 public void mouseEntered(MouseEvent e) {
  // TODO Auto-generated method stub

 }

 @Override
 public void mouseExited(MouseEvent e) {
  // TODO Auto-generated method stub

 }

 @Override
 public void mousePressed(MouseEvent e) {
  // TODO Auto-generated method stub

 }

 @Override
 public void mouseReleased(MouseEvent e) {
  // TODO Auto-generated method stub

 }

}

This Works Except it fires when the click is within a virtual box around the oval.
Could anyone help me to have it fire when the click is EXACTLY on the oval.
Thanks in advance.

Was it helpful?

Solution

The simplest way I can think of to do this is to avoid fillOval and use the geometry in the java.awt.geom package. So you can declare an Ellipse since it appears that the oval that you are drawing is statically sized.

 class MyDrawPanel extends JComponent implements MouseListener {
       Ellipse2D oval = new Ellipse2D.Double(70, 70, 100, 100);

       ....

 }

Then in the paintComponent you use the fill(Shape) method to draw this.

  public void paintComponent(Graphics g) {
      ....
      Graphics2D g2d = (Graphics2D) g;
      ....
      g2d.fill(oval);

  }

Then in the mouse event you can detect if the click is in the oval or not like this:

 public void mouseClicked(MouseEvent e) {
   if ((e.getButton() == 1) && oval.contains(e.getX(), e.getY()) ) {
      repaint();
    // JOptionPane.showMessageDialog(null,e.getX()+ "\n" + e.getY());
   }
 }

OTHER TIPS

All the code should be contained in your custom component class. You have the custom code to paint the oval. Now you need custom code for the click detection. You do this by overriding the contains(...) method. Based on Vincents suggestion you would add something like:

public boolean contains(int x, int y) 
{
    return oval.contains(x, y);
}

This way you don't need any custom code in your MouseListener.

No, you cannot add mouse listener to Graphics directly because it is not a Component. Mouse listener should be added to your Component (or JComponent) that receives the suitable Graphics object passed when Swing invokes paint(Graphics) and friends.

If something changes in respose to the mouse actions, you should modify the state (fields) of your component and then invoke repaint. The paint group methods will be invoked later in appropriate time.

Even more you cannot put a listener on the the geometric figure like oval. You need to get the mouse click coordinates and try to determine one or another way if the click has been inside your figure.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top