-1

This is my code please help and explain what I did wrong thank you very much. Also I am a bit confuse about Thread whether I did correctly way.

public class Fade extends JPanel implements Runnable {

    static Image image;
    private float alpha = 0f;
    static JFrame frame;

    public static void main(String[] args) throws IOException {

        image = new ImageIcon(ImageIO.read(new File("gummybear.jpg")))
                .getImage();

        frame = new JFrame("fade frame");
        frame.add(new Fade());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(image.getWidth(frame), image.getHeight(frame));
        // set picture in the center of screen
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);

        ExecutorService executor = Executors.newFixedThreadPool(1);
        Runnable fade = new Fade();
        executor.execute(fade);

        // executor.shutdown();

        // while (!executor.isTerminated()) {
        // }
        // System.out.println("Finished fade in / fade out threads");

    }

    public void run() {

        while (alpha < 1) {
            try {
                System.out.println(alpha);
                alpha += 0.1f;
                this.repaint();

                Thread.sleep(100);
            } catch (InterruptedException ex) {
                Logger.getLogger(Fader.class.getName()).log(Level.SEVERE, null,
                        ex);
            }

        }

    }

    @Override
    public void paintComponent(Graphics g) {

        super.paintComponent(g);

        Graphics2D g2d = (Graphics2D) g;

        // SRC_OVER: If pixels in the source and the destination overlap, only
        // the source
        // pixels outside of the overlapping area are rendered. The pixels in
        // the overlapping area are not changed.

        g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
                alpha));

        g2d.drawImage(image, 0, 0, null);

        // Color c = new Color(255, 255, 255, alpha);

        // g2d.setColor(c);

        // g2d.fillRect(0, 0, image.getWidth(frame), image.getHeight(frame));

        System.out.println("repaint");

    }

}

...............

user314104
  • 1,528
  • 14
  • 31
  • *"I am a bit confuse about Thread whether I did correctly way."* No. Better to use a Swing `Timer` when calling methods like `repaint()`. That would obey the EDT (Event Dispatch Thread) rule which says GUI updates should be performed on the EDT. General tips: 1) For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete and Verifiable Example). 2) One way to get image(s) for an example is to hot-link to the images seen in [this answer](http://stackoverflow.com/a/19209651/418556). – Andrew Thompson Apr 17 '14 at 15:13

2 Answers2

2

Image Stuff

It looks to me that you want to add transparency to an image that was loaded from a JPG file. JPG files don't have an alpha channel (that is used for transparency), so this will not work.

You find a good example for working with transparency (and loading images from JPG) in the one of the Java tutorials: Drawing an Image

There you find a nice example with code that does nearly what you want: change the transparency of an image loaded from a JPG file. The difference is that the opacity value is taken from a slider control instead of a timed variable.

Threading Stuff

Edit: I just realized, that you are using Swing, an API that is not designed to be thread safe.

I just have to point you to the Java Tutorials How to Use Swing Timers and Concurrency in Swing then.

cyberbrain
  • 3,433
  • 1
  • 12
  • 22
  • Neither the fact that JPG does not support transparency, not the fact that Swing is not thread-safe is in *any* way related to this question. – Marco13 Apr 17 '14 at 14:20
1

The problem is that you are not changing the alpha value. At least, not the alpha value of the Fade instance that you are showing:

// Here you are adding a "Fade" instance to the frame.
frame.add(new Fade());
...

// Here you are creating a NEW "Fade" instance. Only in 
// this instance, the alpha value will be affected
Runnable fade = new Fade();
executor.execute(fade);

Change this to

// Create a Fade instance and add it to the frame
Fade fade = new Fade();
frame.add(fade);
...

// Submit the SAME Fade instance to the executor
executor.execute(fade);

You'll also have to verify that the alpha value remains in [0,1], but this can be done with something like

alpha += 0.1f;
alpha = Math.min(1.0f, alpha);
Marco13
  • 53,703
  • 9
  • 80
  • 159