1

I think something is wrong with paint method, but I can't figure it out.

public void paint(Graphics g) {
        screenImage = createImage(1280, 720);
        screenGraphic = screenImage.getGraphics();
        screenDraw(screenGraphic);
        g.drawImage(screenImage, 0, 0, null);
    }

    public void screenDraw(Graphics g) {
        g.drawImage(BG, 0, 0, null);
        if(isMainScreen) {
            g.drawImage(changedImageAlpha(selectedImage, 120), 130, 360, null);
        }
        paintComponents(g);
        this.repaint();
    }

    public Image changedImageAlpha(Image image, int trans) {
        BufferedImage img = new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_ARGB);
        Graphics2D g = img.createGraphics(); 
        Composite c = AlphaComposite.getInstance( AlphaComposite.SRC_ATOP, .5f); 
        g.setComposite(c); 
        g.drawImage(image, 0, 0, null);
        g.dispose();

        int colorMask = 0x00FFFFFF;
        int alphaShift = 24;
        for(int y=0; y<img.getHeight(); y++){
            for(int x=0; x<img.getWidth(); x++) {
                img.setRGB(x, y, (img.getRGB(x, y) & colorMask) | (trans << alphaShift));
            }
        }
        return img;
    }

BG is an Image object, an also is screenImage. I expected image to be a transparent image, and yes. I see some transparent image, but nothing in it. It's just a clear transparent image, no color, nothing. What could be the problem?

Steve0320
  • 23
  • 7

1 Answers1

1

As a comment to the answer to your previous question, I mentioned there was an easier than the bit manipulation suggested in the answer.

But I meant instead of not in addition to!

So this:

public Image changedImageAlpha(Image image, int trans) {
    BufferedImage img = new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_ARGB);
    Graphics2D g = img.createGraphics(); 
    Composite c = AlphaComposite.getInstance( AlphaComposite.SRC_ATOP, .5f); 
    g.setComposite(c); 
    g.drawImage(image, 0, 0, null);
    g.dispose();

    int colorMask = 0x00FFFFFF;
    int alphaShift = 24;
    for(int y=0; y<img.getHeight(); y++){
        for(int x=0; x<img.getWidth(); x++) {
            img.setRGB(x, y, (img.getRGB(x, y) & colorMask) | (trans << alphaShift));
        }
    }
    return img;
}

Should be this:

public Image changedImageAlpha(Image image, int trans) {
    BufferedImage img = new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_ARGB);
    Graphics2D g = img.createGraphics(); 
    Composite c = AlphaComposite.getInstance( AlphaComposite.SRC_ATOP, .5f); 
    g.setComposite(c); 
    g.drawImage(image, 0, 0, null);
    g.dispose();

    return img;
}

And as a tip: Try to understand the code others are providing. 'Cut & paste' programming usually ends in failure.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433