有没有办法将MouseListener添加到Graphic对象? [英] Is there any way to add a MouseListener to a Graphic object?

查看:86
本文介绍了有没有办法将MouseListener添加到Graphic对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法将MouseListener添加到Graphic对象。

我有这个绘制椭圆的简单GUI。

我想要的是当用户处理事件时点击椭圆

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.

推荐答案

我能想到的最简单的方法是避免使用fillOval并使用java.awt.geom包中的几何体。所以你可以声明一个Ellipse,因为看起来你绘制的椭圆是静态大小的。

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);

       ....

 }

然后在paintComponent中使用fill(Shape)方法绘制它。

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());
   }
 }

这篇关于有没有办法将MouseListener添加到Graphic对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆