Is there any way to add a MouseListener to a Graphic object ?

Posted by Fahad on Stack Overflow See other posts from Stack Overflow or by Fahad
Published on 2011-01-08T13:37:19Z Indexed on 2011/01/08 13:54 UTC
Read the original article Hit count: 246

Filed under:
|

Hi,

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.

© Stack Overflow or respective owner

Related posts about java

Related posts about swing