0

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();
        }

    }
}

enter image description here

enter image description here

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!

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • It seems the 'yellow' in that circle is translucent (**partly** transparent). If you want it to remain solid yellow on every background, remove that transparent element of it. I.E. make is a solid (no transparency) yellow. **General Tips:** 1) For better help sooner, [edit] to add a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) One way to get image(s) for an example is to hot link to images seen in [this Q&A](http://stackoverflow.com/q/19209650/418556). E.G. The code in [this answer](https://stackoverflow.com/a/10862262/418556) hot links to an image .. – Andrew Thompson Aug 19 '21 at 08:24
  • .. embedded in [this question](https://stackoverflow.com/q/10861852/418556). – Andrew Thompson Aug 19 '21 at 08:24
  • 1) Don't extend JLabel to do custom painting. Instead extend JPanel. 2) Override paintComponent(...) not paint. 3) Invoke super.paintComponent(g) to make sure the background is cleared before you do your painting. 4) a painting method should NOT do IO. – camickr Aug 19 '21 at 19:34

0 Answers0