Search code examples
javatransparency

Java - Image Transparency With Graphics g


I would like to create a fade in and fade out effect for one of the characters in my video game. To do this I would like to have the image of them be fade out and fade back in. I would like to change the opacity of the image but have no idea how. I am using the java Graphics. Here is some code.

g.drawImage(joople, jx, jy, sx, sy, this);

I am running this code in this method:

public void paint(Graphics g) {

Many thanks, Oak


Solution

  • You will want to have a look at Compositing Graphics

    enter image description here

    import java.awt.AlphaComposite;
    import java.awt.Dimension;
    import java.awt.EventQueue;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.IOException;
    import javax.imageio.ImageIO;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.Timer;
    import javax.swing.UIManager;
    import javax.swing.UnsupportedLookAndFeelException;
    
    public class FadeMe {
    
        public static void main(String[] args) {
            new FadeMe();
        }
    
        public FadeMe() {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                        ex.printStackTrace();
                    }
    
                    JFrame frame = new JFrame("Testing");
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.add(new TestPane());
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                }
            });
        }
    
        public class TestPane extends JPanel {
    
            private BufferedImage img;
            private float alpha = 0;
            private float delta = 0.01f;
    
            public TestPane() {
                try {
                    img = ImageIO.read(...);
                    Timer timer = new Timer(40, new ActionListener(){ 
                        @Override
                        public void actionPerformed(ActionEvent e) {
                            alpha += delta;
                            if (alpha > 1.0) {
                                alpha = 1.0f;
                                delta *= -1.0f;
                            } else if (alpha < 0.0) {
                                alpha = 0.0f;
                                delta *= -1.0f;
                            }
                            repaint();
                        }
                    });
                    timer.start();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
    
            @Override
            public Dimension getPreferredSize() {
                return img != null ? new Dimension(img.getWidth(), img.getHeight()) : new Dimension(200, 200);
            }
    
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                Graphics2D g2d = (Graphics2D) g.create();
                g2d.setComposite(AlphaComposite.SrcOver.derive(alpha));
                int x = (getWidth() - img.getWidth()) / 2;
                int y = (getHeight() - img.getHeight()) / 2;
                g2d.drawImage(img, x, y, this);
                g2d.dispose();
            }
    
        }
    
    }