简体   繁体   English

鼠标包含在对象内

[英]Mouse contain within object

I'm new to learning Java and for a few days I have searched for a way to click drawn objects, to contain the mouse within a object such as g.drawRect(x, x, x, x)... 我刚开始学习Java,几天来,我一直在寻找一种单击绘制对象的方法,以将鼠标包含在诸如g.drawRect(x,x,x,x)之类的对象中...

I came across the code below, I'm trying to make it work so I can learn what is needed to contain the mouse within a object but I cant seem to make it work. 我遇到了下面的代码,我试图使其正常工作,以便我可以了解将鼠标包含在对象中所需的内容,但我似乎无法使其正常工作。

Got code from " Is there any way to add a MouseListener to a Graphic object? " 从“ 是否有任何方法可以将MouseListener添加到Graphic对象? ”中获得代码。

I've tried to make it work according to the tips they gave but no hope :(. 我试图根据他们给的提示使它工作,但没有希望:(。

Any help is appreciated. 任何帮助表示赞赏。

 import java.awt.*; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.geom.Ellipse2D; import javax.swing.*; public class Gui3 extends JFrame { JFrame frame = new JFrame(); MyDrawPanel drawpanel = new MyDrawPanel(); public static void main(String[] args) { Gui3 gui = new Gui3(); 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 { Ellipse2D oval = new Ellipse2D.Double(70, 70, 100, 100); public void paintComponent(Graphics g) { Graphics2D g2d = (Graphics2D) g; g2d.fillOval(70, 70, 100, 100); } @ Override public void mouseClicked(MouseEvent e) { if ((e.getButton() == 1) && oval.contains(e.getX(), e.getY())) { 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) { } @ Override public void mouseReleased(MouseEvent e) { // TODO Auto-generated method stub } public boolean contains(int x, int y) { return oval.contains(x, y); } } 

Your commented line hints at your issue - you never actually add a MouseListener to the GUI, so no one is being notified of mouse events. 您的注释行暗示了您的问题-您实际上从未向GUI添加MouseListener,因此没有人收到有关鼠标事件的通知。 It appears you've made your panel the mouse listener (which is fine), so register it in a constructor for the MyDrawPanel class. 看来您已经使面板成为鼠标侦听器(很好),因此将其注册到MyDrawPanel类的构造函数中。 Add this constructor to your MyDrawPanel, and you should be good to go. 将此构造函数添加到MyDrawPanel中,您应该会很好。

public MyDrawPanel(){
    addMouseListener(this);
}

Also, it would probably be a good idea to draw your actual oval variable instead of using the drawOval(..) method of Graphics, because you are creating a shape in memory and an image that are disjoint from one another. 同样,最好绘制一个实际的oval变量而不是使用Graphics的drawOval(..)方法,因为您正在内存中创建一个形状并且图像彼此不相交。 Try this instead for your paintComponent(Graphics g) : 为您的paintComponent(Graphics g)尝试以下方法:

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

Basically, you need to register the MouseListener with the DrawPanel , you want to do this, because you want the mouse events to be within the context of the DrawPanel otherwise the MouseEvent location information won't be correct. 基本上,您需要向DrawPanel注册MouseListener ,因为您希望鼠标事件位于DrawPanel的上下文内,否则MouseEvent位置信息将不正确。

class MyDrawPanel extends JComponent implements MouseListener {

    Ellipse2D oval = new Ellipse2D.Double(70, 70, 100, 100);

    public MyDrawPanel() {
        addMouseListener(this);
    }

You will also want to call super.paintComponent before you do any custom painting 您还需要在进行任何自定义绘画之前调用super.paintComponent

@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2d = (Graphics2D) g;

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

}

You should also ensure that you are creating/modifying your UI from within the context of the Event Dispatching Thread. 您还应确保从事件调度线程的上下文中创建/修改UI。 See Initial Threads for more details 有关更多详细信息,请参见初始线程

public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        @Override
        public void run() {
            try {
                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                ex.printStackTrace();
            }

            Gui3 gui = new Gui3();
            gui.go();
        }
    });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM