JButton中不透明像素的事件检测

时间:2012-12-11 17:13:37

标签: java swing jbutton

我有一个扩展JButton的课程,我正在尝试将 .png 图片应用到。{ 图像形状不规则,并被透明像素包围。我已覆盖paintComponent()中的JButton方法,以将我的缓冲图像应用于按钮。现在,图像是唯一被绘制的东西,这就是我想要的。

但是,该按钮仍在检测周围矩形中的事件。有没有办法将检测限制在仅包含不透明像素的区域(或者更确切地说检测透明像素上的事件)?

按钮类的代码如下。

public class DrawButton extends JButton{

    private BufferedImage bi;

    public DrawButton(BufferedImage bi){
        setPreferredSize(new Dimension(bi.getWidth(), bi.getHeight()));
        this.bi = bi;
    }

    @Override
    protected void paintComponent(Graphics g){
        g.drawImage(bi, 0, 0, null);
        g.dispose();
    }
}

1 个答案:

答案 0 :(得分:8)

我建议使用MouseAdapter并覆盖mouseClicked(..)。在mouseClicked中检查点击点处的像素是 alpha ,如果它什么也不做,如果没有做的话。

  • 始终在覆盖super.paintComponent(..)方法中拨打paintComponent作为第一个电话,但是因为,特别是按钮,这会重新标记JButton setContentAreaFilled(false)后台电话JButton实例来阻止这个。您可能也需要setBorderPainted(false)

这是我做的一个小例子(改编自here):

enter image description here

如果点击笑脸:

enter image description here

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;

public class TransparentButton {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                final JFrame frame = new JFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                MyButton button = null;

                try {
                    button = new MyButton(scaleImage(100, 100, ImageIO.read(new URL("http://2.bp.blogspot.com/-eRryNji1gQU/UCIPw0tY5bI/AAAAAAAASt0/qAvERbom5N4/s1600/original_smiley_face.png"))));
                } catch (Exception ex) {
                    ex.printStackTrace();
                }

                button.addMouseListener(new MouseAdapter() {
                    @Override
                    public void mouseClicked(MouseEvent me) {
                        super.mouseClicked(me);
                        MyButton mb = ((MyButton) me.getSource());
                        if (!isAlpha(mb.getIconImage(), me.getX(), me.getY())) {
                            JOptionPane.showMessageDialog(frame, "You clicked the smiley");
                        }
                    }

                    private boolean isAlpha(BufferedImage bufImg, int posX, int posY) {
                        int alpha = (bufImg.getRGB(posX, posY) >> 24) & 0xFF;
                        return alpha == 0;
                    }
                });

                frame.add(button);

                frame.pack();
                frame.setVisible(true);
            }
        });
    }

    public static BufferedImage scaleImage(int w, int h, Image img) throws Exception {
        BufferedImage bi;
        bi = new BufferedImage(w, h, BufferedImage.TRANSLUCENT);
        Graphics2D g2d = (Graphics2D) bi.createGraphics();
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2d.addRenderingHints(new RenderingHints(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY));
        g2d.drawImage(img, 0, 0, w, h, null);
        return bi;
    }
}

class MyButton extends JButton {

    BufferedImage icon;

    MyButton(BufferedImage bi) {
        this.icon = ((BufferedImage) bi);
        setContentAreaFilled(false);
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(icon.getWidth(), icon.getHeight());
    }

    public BufferedImage getIconImage() {
        return icon;
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawImage(icon, 0, 0, this);
    }
}