Java AWT custom window shape using transparent image
I am trying to make a UI using awt. I want to use only images and
transparent components. Right now I cant understand how to make a main
window which is supposed to be a PNG image with a custom shape. All the
areas that are tranparent in the image are replaced with a black color.
Here is the code I use:
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);
}
}
}
No comments:
Post a Comment