【问题标题】:Image mapping like feature in Java swing, Points vs GeneralPathJava swing、Points vs GeneralPath 中的图像映射功能
【发布时间】:2010-10-31 03:56:50
【问题描述】:

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

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

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

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

【问题讨论】:

    标签: java imagemap


    【解决方案1】:

    你们的图像是重叠绘制的还是分开绘制的。

    如果它们是分开绘制的,那么您应该在 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 );
    
        }
    }
    

    【讨论】:

    • 这看起来应该可以。关于您是否在彼此之上绘制图像的问题......我希望在一个 paintComponent() 方法中绘制大约 7-8 个图像。这也需要 7-8 个 contains() 方法。难道我不能有单独的类,每个类都有自己的形状来实现这一点,而不是分离图像本身吗?谢谢。
    • 你应该有单独的组件,这样每个组件都可以有自己的形状。然后,您只需将这些“形状组件”添加到面板,就像使用任何其他 Swing 组件一样。如果组件没有分层,则使用起来会更容易。但是,组件可以分层,但请记住,根据组件的 ZOrder,只有最上面的组件会接收鼠标事件。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-12
    • 2013-12-17
    • 1970-01-01
    相关资源
    最近更新 更多