Currently, I have a transparent image. I draw that image by override paint() method of a JLabel but the color of my image is changed base on background color of JLable as following
class MyCanvas extends JLabel {
float alphaValue = 1.0f;
int compositeRule = AlphaComposite.CLEAR;
AlphaComposite ac;
ImageFilter filter = new RGBImageFilter() {
int transparentColor = Color.white.getRGB() | 0xFF000000;
public final int filterRGB(int x, int y, int rgb) {
if ((rgb | 0xFF000000) == transparentColor) {
return 0x00FFFFFF & rgb;
} else {
return rgb;
}
}
};
public void paint(Graphics g) {
Image imgSpot = Toolkit.getDefaultToolkit().getImage("src/yellowCircle.png");
MediaTracker tracker = new MediaTracker(this);
tracker.addImage(imgSpot, 0);
try {
tracker.waitForID(0);
} catch (InterruptedException e) {
e.printStackTrace();
}
try {
Graphics2D g2D = (Graphics2D) g;
//g2D.setColor(Color.white); //case 1
g2D.setColor(Color.black);//case 2
g2D.fillRect(0, 0, this.getWidth(), this.getHeight());
int imageSpotWidth = imgSpot.getWidth(null);
int imageSpotHeight = imgSpot.getHeight(null);
BufferedImage objTempSpotImage = new BufferedImage(imageSpotWidth, imageSpotHeight,
BufferedImage.TYPE_INT_ARGB);
objTempSpotImage.createGraphics().drawImage(imgSpot, 0, 0, null);
g2D.drawImage(objTempSpotImage, 20, 20, null);
} catch (Exception ee) {
ee.printStackTrace();
}
}
}
But I want to keep the color of my image. How can I resolve this issues? For source code of my testing project, you can get full source code in https://github.com/thanhbinh93-bn/Java_drawImage Thanks in advance!