简体   繁体   中英

Java Swing applicaion gif animation leaving a trail

Hey all I am wondering if I can fix my animation issue.

Currently it does animate just fine but with one issue - it seems to leave training images behind as it animates.

在此处输入图像描述

This is my code I am using:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Point;
import java.awt.RenderingHints;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.io.File;
import java.io.FileInputStream;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import java.io.*;

@SuppressWarnings("serial")
public class Display extends JFrame {
    private static JPanel container = new JPanel();
    JLabel insidePIV = new JLabel();

    public static void main(String[] args) throws IOException {
        Display blah = new Display();
    }

    public void addListeners() {
        final Point offset = new Point();
        addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(final MouseEvent e) {
                offset.setLocation(e.getPoint());
            }
        });
        addMouseMotionListener(new MouseMotionAdapter() {
            @Override
            public void mouseDragged(final MouseEvent e) {
                setLocation(e.getXOnScreen() - offset.x, e.getYOnScreen() - offset.y);
            }
        });
    }

    public Display() throws IOException {
        JLabel outsidePIV = new JLabel(new ImageIcon(ImageIO.read(new File("c:/temp/pivReaderAlone.png"))));
        MyJLabel antialias = new MyJLabel(
                "<html><div style='text-align: center;'>Please insert your card into the reader.</div></html>");

        antialias.setFont(new Font("Segoe UI", Font.BOLD, 10));
        antialias.setBounds(13, 223, 170, 240);
        container.setLayout(new BorderLayout());
        container.setBackground(new Color(0, 0, 0, 0));

        container.add(antialias, BorderLayout.CENTER);
        container.add(outsidePIV, BorderLayout.CENTER);

        JButton p = new JButton();
        p.setText("Animate");
        p.setBounds(30, 100, 120, 20);

        outsidePIV.add(p, BorderLayout.CENTER);

        p.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                try {
                    File file = new File("c:/temp/pivbatman.gif");
                    Image image = Toolkit.getDefaultToolkit()
                            .createImage(org.apache.commons.io.IOUtils.toByteArray(new FileInputStream(file)));
                    ImageIcon icon = new ImageIcon(image);
                    outsidePIV.setDoubleBuffered(true);
                    outsidePIV.setIcon(icon);
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        });

        addListeners();

        this.setTitle("PIV");
        this.setSize(190, 390);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLocationRelativeTo(null);
        this.setAlwaysOnTop(true);
        this.setUndecorated(true);
        this.setBackground(new Color(0, 0, 0, 0));
        this.setContentPane(container);
        this.setVisible(true);
        this.setResizable(false);
    }

    public class MyJLabel extends JLabel {
        public MyJLabel(String str) {
            super(str);
        }

        public void paint(Graphics g) {
            Graphics2D g2 = (Graphics2D) g;
            g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
            g2.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);

            super.paint(g);
        }
    }
}

If anyone can help me with this error then please do - been trying to fix it for hours now...

UPDATE

public static void main(String[] args) throws IOException {     
    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            try {
                Display blah = new Display();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    });
}

Render images on Java Swing is trick, java has a main UI Thread called AWT that is not concurrent. It´sa good practice to use the method invokeLater from SwingUtilities class.

SwingUtilities.invokeLater takes a Runnable and invokes it in the UI thread later.

The syntax is:

SwingUtilities.invokeLater(new Runnable() {
    @Override
    public void run() {
        //some code that render in the UI here. 
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM