Java Swing应用程序gif动画留下痕迹 [英] Java Swing applicaion gif animation leaving a trail

查看:121
本文介绍了Java Swing应用程序gif动画留下痕迹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,我想知道是否可以解决动画问题。

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...

更新

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();
            }
        }
    });
}


推荐答案

在Java Swing上渲染图像是技巧是,java有一个称为AWT的主UI线程,它不是并发的。最好使用invokeLater /SwingUtilities.html rel = nofollow noreferrer> SwingUtilities 类。

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

SwingUtilities.invokeLater 接受Runnable并稍后在UI线程中调用它。

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

语法为:

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

这篇关于Java Swing应用程序gif动画留下痕迹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆