1

Im making an application that should be able to read files in a given directory and then display all image files in a fullscreen borderless window, it should also display text files, but i havent started on that part yet, so never mind the system.out part. So far ive made the JFrame fullscreen and borderless, and ive made an Arraylist containing the files i want shown. I then add a jpanel with the file in the constructor, this jpanel adds the picture to a jlabel and displays it, afterward i remove the jpanel and start over with the next picture.

What i need is a way to make the images fade in from a given color, and then fade out to that same color.

this is where i add the panels and remove them again

for (File f : files) {
        String fileName = f.getName();
        if (fileName.endsWith(".txt")) {
            System.out.println("Txt");
            System.out.println(fileName);
            System.out.println("--");
        } else if (fileName.endsWith(".png") || fileName.endsWith(".jpg") || fileName.endsWith("bmp")) {
            AlbumPanel albumpan = new AlbumPanel(connect, f, this);
            add(albumpan, BorderLayout.CENTER);
            pack();
            try {
                Thread.sleep(current.getFormat().getPicLength()*1000);

            } catch (InterruptedException ex) {
            }
            remove(albumpan);
        }
    }

And this is the JPanel

public class AlbumPanel extends JPanel {

BufferedImage image;
ImageIcon icon;
IConnect connect;
File pic;
JFrame presWin;

public AlbumPanel(IConnect connect, File pic, JFrame presWin) {
    this.connect = connect;
    this.pic = pic;
    this.presWin = presWin;
    this.setLayout(new GridBagLayout());

    try {
        image = ImageIO.read(pic);
    } catch (Exception e) {
        System.out.println(e);
    }

    image = resize(image, presWin.getWidth(), presWin.getHeight());
    icon = new ImageIcon(image);
    JLabel picLabel = new JLabel();
    picLabel.setIcon(icon);
    add(picLabel);
    setVisible(true);
}

private BufferedImage resize(BufferedImage image, int width, int height) {
    BufferedImage bi = new BufferedImage(width, height, BufferedImage.TRANSLUCENT);
    Graphics2D g2d = (Graphics2D) bi.createGraphics();
    g2d.addRenderingHints(new RenderingHints(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY));
    g2d.drawImage(image, 0, 0, width, height, null);
    g2d.dispose();
    return bi;
}
Jonas Olesen
  • 568
  • 5
  • 25

2 Answers2

1

Override the rendering method (probably paintComponent) on the control you are using. Call super.paintComponent and then draw a semi-transparent rectangle of your "fade out" color over it.

Inside paintComponent you are passed a Graphics object. Methods on that object can be used to do things, including drawing a rectangle on the screen. You probably want fillRect.

Pick the opacity to be 0 when the image is fully displayed then move towards 1 when it's fully faded out.

You will need something to trigger redraws at regular intervals (a Swing Timer may be good enough).

Tim B
  • 40,716
  • 16
  • 83
  • 128
  • how do i create a semi-transparent rectangle? just a new JLabel or something ? i havent really worked with graphics in java – Jonas Olesen Apr 24 '15 at 09:50
  • @JonasOlesen added something on that. – Tim B Apr 24 '15 at 09:54
  • Override paintComponent not paint, but yes that's the idea – Tim B Apr 24 '15 at 10:05
  • opacity is part of the color - try doing a search. For example: http://stackoverflow.com/questions/8110975/how-to-make-a-rectangle-in-graphics-in-a-transparent-colour – Tim B Apr 24 '15 at 10:06
1

You can use Color's transparency (the constructor public Color(int r, int g, int b, int a) where the last variable is alpha.

Start a Timer and change the Color's transparency from 0 to 255 and back filling the image (or the panel) with the Color.

StanislavL
  • 56,971
  • 9
  • 68
  • 98