I have a JLabel on which a png like a BufferedImage is drawn. The problem is that although the background of the png is transparent, it is displayed with the background.
The "CentreOnDefaultScreen" class detects which screen is the primary and takes its size, then centers the JFrame in the center of the screen.
The "Shape" class simply defines the position, length and height of the JLabel rectangle, and an Optional of the direction in which the object will move.
The "ImageLoader" class simply looks for the png given a title and returns a BufferedImage. If the direction is RIGHT, the image returns to the right and vice versa
I don't know what to do to make the background of the png transparent. I think JLabel is creating the background but I don't know how to prevent this.
public class Actor extends JLabel{
/**
*
*/
private static final long serialVersionUID = 1967410748214691856L;
private static final int SIZEX = CenterOnDefaultScreen.center().width*70/100;
private static final int SIZEY = CenterOnDefaultScreen.center().height*70/100;
private Shape s;
private final ImageLoader iLoader = new ImageLoader();
public Actor(Shape s){
this.s = s;
super.setBounds(this.s.getRectangle());
super.setOpaque(false);
}
public void changeLocation(BirdPos2D pos){
super.setLocation(pos.x, pos.y);
this.s = new Shape(pos, this.s.getDimensions().getX(),
this.s.getDimensions().getY(), Optional.of(this.getShape().getDireciton()));
}
public Shape getShape(){
return this.s;
}
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.clearRect(0, 0, SIZEX, SIZEY);
g2.drawImage(iLoader.getBirdImage(this.getShape().getDireciton() == Directions.RIGHT ? Directions.RIGHT : Directions.LEFT),
0, 0, null);
g2.dispose();
}
}
To paint this JLabel on the JFrame I have a Visual class which extends Thread in which run() method for each position change I repaint it. It all works fine but my only problem is the png background.