我正在尝试使用 AWT 制作 UI。我只想使用图像和透明组件。现在我无法理解如何制作一个应该是具有自定义形状的 PNG 图像的主窗口。图像中所有透明的区域都被替换为黑色。这是我使用的代码:
public class Test {
static Image image;
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws IOException {
//switch to the right thread
image = ImageIO.read(Test.class.getClassLoader().getResource("resources/images/panel.png").openStream());
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
Frame frame = new Frame("Test");
frame.setUndecorated(true);
frame.setBackground(new Color(0,0,0,0));
frame.add(new BackGround(image,image.getWidth(frame),image.getHeight(frame)));
frame.pack();
frame.setSize(image.getWidth(frame), image.getHeight(frame));
frame.setVisible(true);
frame.setLocationRelativeTo(null);
}
}
);
}
private static class BackGround extends Component {
private Image img;
private int wid, hgt;
public BackGround(Image img, int wid, int hgt){
this.img=img;
this.wid=wid;
this.hgt=hgt;
}
@Override
public void paint(Graphics graphics) {
graphics.drawImage(image,0,0,wid,hgt,0,0,wid,hgt,null);
}
}
}