2

我的目标是在 paintComponent() 方法中使用 2D 图形绘制多个图像。但是,我不确定如何添加 MouseListener 以便每个图像都知道它是否被选中。

到目前为止,我的解决方案过于简单地记录鼠标单击的坐标,并查看它们是否包含在每个图像的边界内。然而,对于具有更复杂边界的图像,这将是困难的。

另一种选择是创建简单的形状并将它们放置在图像上,但同样具有更复杂边界的图像将是困难的。在 此处找到的关于 SO 的另一个讨论中,有人提到使用 GeneralPath 绘制更复杂的形状。我从来没有玩过is,但这似乎令人鼓舞。

在这 2 个选项中,什么似乎是最好的解决方案,或者是否有其他建议

4

1 回答 1

2

您是在彼此之上绘制的图像还是分别绘制的图像。

如果它们是单独绘制的,那么您应该在 JComponent 上进行自定义绘制。然后您可以使用 GeneralPath 进行绘图。您还需要通过检查鼠标单击是否包含在 GeneralPath 中来实现 contains(...) 方法。如果正确实现了 contains() 方法,则 MouseListener 将正确响应。

这是一个更简单的例子:

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.*;

public class RoundButton extends JButton {
    public RoundButton(String label) {
        super(label);

        // These statements enlarge the button so that it
        // becomes a circle rather than an oval.
        Dimension size = getPreferredSize();
        size.width = size.height = Math.max(size.width, size.height);
        setPreferredSize(size);

        // This call causes the JButton not to paint the background.
        // This allows us to paint a round background.
        setContentAreaFilled(false);
    }

    // Paint the round background and label.
    protected void paintComponent(Graphics g) {
    if (getModel().isArmed()) {
            // You might want to make the highlight color
            // a property of the RoundButton class.
            g.setColor(Color.lightGray);
        } else {
            g.setColor(getBackground());
        }
    g.fillOval(0, 0, getSize().width-1, getSize().height-1);

        // This call will paint the label and the focus rectangle.
    super.paintComponent(g);
    }

    // Paint the border of the button using a simple stroke.
    protected void paintBorder(Graphics g) {
        g.setColor(getForeground());
        g.drawOval(0, 0, getSize().width-1, getSize().height-1);
    }

    // Hit detection.
    Shape shape;
    public boolean contains(int x, int y) {
        // If the button has changed size, make a new shape object.
        if (shape == null || !shape.getBounds().equals(getBounds())) {
            shape = new Ellipse2D.Float(0, 0, getWidth(), getHeight());
        }
        return shape.contains(x, y);
    }

    // Test routine.
    public static void main(String[] args) {
        // Create a button with the label "Jackpot".
        JButton button = new RoundButton("Jackpot");
        button.setBackground(Color.green);
        button.setBounds(0, 0, 100, 100);

        JButton button2 = new RoundButton("Jackpot2");
        button2.setBackground(Color.red);
        button2.setBounds(50, 50, 100, 100);

        // Create a frame in which to show the button.
        JFrame frame = new JFrame();
        frame.getContentPane().setBackground(Color.yellow);
        frame.getContentPane().setLayout(null);
        frame.getContentPane().add(button);
        frame.getContentPane().add(button2);
//        frame.getContentPane().setLayout(new FlowLayout());
        frame.setSize(200, 200);
        frame.setVisible(true);

        MouseListener mouseListener = new MouseAdapter() {
            public void mouseEntered( MouseEvent e )
            {}

            public void mouseExited( MouseEvent e )
            {}

            public void mouseClicked( MouseEvent e )
            {
                System.out.println( "clicked " );
            }

            public void mousePressed( MouseEvent e )
            {
                System.out.println( "pressed " );
            }

            public void mouseReleased( MouseEvent e )
            {
                System.out.println( "released " );
            }
        };
        button.addMouseListener( mouseListener );

    }
}
于 2010-10-31T04:08:23.480 回答