0

I draw graphics in frame with this methods.

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

public void screenDraw(Graphics2D g) {
    g.drawImage(BG, 0, 0, null); 
    Graphics2D g2 = (Graphics2D)g;
    if(isMainScreen) {
        //g2.setComposite(alphaComposite);
        g2.drawImage(selectedImage, 100, 220, null);
    }
    paintComponents(g);
    this.repaint();  
}

I want to have selectedImage to be transparency 50% or other integer.

private Image selectedImage = new ImageIcon(Main.class.getResource("../pic/something.jpg")).getImage(); 

this is selectedImage, and it works well.

Steve0320
  • 23
  • 7

1 Answers1

1

Adapt a bit of Image -> BufferedImage code, and then you can set each pixel to be transparent

Image selectedImage = new ImageIcon([...]).getImage();

//Image -> BufferedImage code
BufferedImage img = new BufferedImage(selectedImage.getWidth(null), selectedImage.getHeight(null), BufferedImage.TYPE_INT_ARGB);
Graphics2D g = img.createGraphics();
g.drawImage(selectedImage, 0, 0, null);
g.dispose();

//set each pixel to be transparent
int transparency = 127; //0-255, 0 is invisible, 255 is opaque
int colorMask = 0x00FFFFFF; //AARRGGBB
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) | (transparency << alphaShift));

//img is now transparent, can replace selectedImage if you want (optional)
selectedImage = img;

If you use ImageIO.read(File) instead of ImageIcon.getImage(), it will give you a BufferedImage directly

phflack
  • 2,729
  • 1
  • 9
  • 21
  • 1
    Change `Graphics2D g = img.createGraphics(); g.drawImage(selectedImage, 0, 0, null);` to `Graphics2D g = img.createGraphics(); Composite c = AlphaComposite.getInstance( AlphaComposite.SRC_ATOP, .5f); g.setComposite(c); g.drawImage(selectedImage, 0, 0, null);` and the job is done! – Andrew Thompson Nov 29 '17 at 15:55
  • @AndrewThompson Neat, I was sure there was a way to modify the drawing, but wasn't sure what to use. If I recall correctly, `JFrame.paint(Graphics)` is actually a `Graphics2D` object, and the composite could be applied to that temporarily? – phflack Nov 29 '17 at 16:14
  • Yes, so long as the `Graphics` is cast to, or otherwise created as, a `Graphics2D` object. But don't alter the paint methods of top level containers. Do custom painting in a `JPanel`, – Andrew Thompson Nov 29 '17 at 16:30
  • I have created a method named something like `public Image changedImageAlpha(Image image, int trans)` , and used the exactly same code but instead of `selectedImage=img;` , `return img;` and in screenDraw method, I rewrote the code inside of `if(isMainScreen)` to `g.drawImage(changedImageAlpha(selectedImage, 120), 100, 220, null);`, and it clearly shows me a transparent image, but with nothing on it. It's just clear transparent image. What did I do wrong? – Steve0320 Nov 30 '17 at 08:54
  • @Steve0320 I tested the method and it works correctly for me, if it's a clear transparent image, are you sure you're not setting the transparency to 0? – phflack Nov 30 '17 at 18:53