Question

this is my code, when I run the program it is working as I want, but there are some error which I don't know how to solve it please help.

public class FadeTimer extends JPanel {

    static Image image;
    Timer timerin;
    Timer timerout;
    private float alpha = 0f;
    static int width;
    static int height;
    static Dimension screen;

    public FadeTimer() throws IOException {

        this.setBackground(Color.black);

        this.setLayout(new BorderLayout());

        // create timer with 100 delay and add action perform of this method
        timerin = new Timer(100, new actionfadein());
        timerin.start();

        timerout = new Timer(100, new actionfadeout());
        timerout.start();

    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);

        Graphics2D g2d = (Graphics2D) g;
        g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
                alpha));
        g2d.drawImage(image, (screen.width - width) / 2,
                (screen.height - height) / 2, null);

    }

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

        JFrame frame = new JFrame("fade frame");
        frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
        frame.add(new FadeTimer());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        // set picture in the center of screen
        frame.setVisible(true);

        image = new ImageIcon(ImageIO.read(new File("gummybear.jpg")))
                .getImage();
        width = image.getWidth(frame);
        height = image.getHeight(frame);
        Toolkit tk = frame.getToolkit();
        screen = tk.getScreenSize();

    }

    class actionfadein implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            // TODO Auto-generated method stub
            System.out.println(alpha);
            alpha += 0.1f;
            if (alpha > 1) {
                alpha = 1;
                timerin.stop();
            }
            repaint();

        }
    }

    class actionfadeout implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            // TODO Auto-generated method stub
            System.out.println(alpha);
            alpha -= 0.05f;
            if (alpha < 0) {
                alpha = 0;
                timerout.stop();
            }
            repaint();

        }
    }

}

////////////////////////////////////////////////

Was it helpful?

Solution

Screen size you cat get without using JFrame's instance:

    screen = Toolkit.getDefaultToolkit().getScreenSize();

Also after creating JFrame, add a component listener to update width, height on any resize:

    JFrame frame = new JFrame("fade frame");
    frame.addComponentListener(new ComponentAdapter() {
        @Override
        public void componentResized(ComponentEvent e) {
            width = image.getWidth(frame);
            height = image.getHeight(frame);
        }
    });
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top