Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 987
Abu Muhammad Avatar asked Jan 08 '11 13:01

Abu Muhammad


2 Answers

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());
   }
 }
like image 150
Vincent Ramdhanie Avatar answered Sep 21 '22 06:09

Vincent Ramdhanie


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.

like image 37
camickr Avatar answered Sep 21 '22 06:09

camickr