假设我有一个 JPanel,上面通过 绘制了一个图像作为背景paintComponent()
,这个面板包含一些带有图标的按钮。这些按钮没有自己的背景,并且“继承”了面板的背景。我知道如何使用 使按钮的图标半透明AlphaComposite
,但我怎样才能获得按钮的背景图像(这是JPanel 背景图像的一部分,因为按钮是不透明的并且被绘制在面板的背景上)并使其透明,而不是图标?我什至不知道这是否可能......
以下代码只是使按钮的图标透明。
import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class TestPanel extends JPanel {
private final ImageIcon icon = new ImageIcon(getClass().getResource("path\\to\\image\\file"));
private BufferedImage img;
public TestPanel() {
try {
img = ImageIO.read(new File("path\\to\\background\\image"));
} catch (IOException ex) {}
ImageIcon fadedIcon = getTranslucentImageIcon(icon, 0.5f);
JButton button = new JButton(fadedIcon);
button.setRolloverIcon(icon);
button.setBorder(BorderFactory.createLineBorder(Color.BLACK));
button.setOpaque(false);
button.setContentAreaFilled(false);
button.setFocusPainted(false);
this.add(button);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(img, 0, 0, null);
}
@Override
public Dimension getPreferredSize() {
if (img != null) {
return new Dimension(img.getWidth(), img.getHeight());
}
return super.getPreferredSize();
}
private BufferedImage getTranslucentImage(BufferedImage oldImg, float alpha) {
int w = oldImg.getWidth();
int h = oldImg.getHeight();
BufferedImage newImg = new BufferedImage(w, h, BufferedImage.TRANSLUCENT);
Graphics2D g2d = newImg.createGraphics();
g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha));
g2d.drawImage(oldImg, null, 0, 0);
g2d.dispose();
return newImg;
}
private ImageIcon getTranslucentImageIcon(Icon oldIcon, float alpha) {
int w = oldIcon.getIconWidth();
int h = oldIcon.getIconHeight();
BufferedImage oldImg = new BufferedImage(w, h, BufferedImage.TRANSLUCENT);
Graphics2D g2d = (Graphics2D) oldImg.getGraphics();
oldIcon.paintIcon(null, g2d, 0, 0);
BufferedImage newImg = getTranslucentImage(oldImg, alpha);
g2d.dispose();
return new ImageIcon(newImg);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame frame = new JFrame();
frame.add(new TestPanel());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
});
}
}
您只需要 2 张图像来重现它(背景图像应该更大以使我的观点更清楚)。
PS我还想知道我是否正确地做整个事情(更具体的方法getTranslucentImage
和getTranslucentImageIcon
),或者是否有更好的方法来做到这一点。