[英]Java Swing applicaion gif animation leaving a trail
嘿,我想知道我是否可以解決我的 animation 問題。
目前它的動畫效果很好,但有一個問題——它似乎在動畫時留下了訓練圖像。
這是我正在使用的代碼:
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);
}
}
}
如果有人可以幫助我解決這個錯誤,那么請做 - 已經嘗試修復它幾個小時了......
更新
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 線程,它不是並發的。 使用SwingUtilities class 中的方法invokeLater
是一種很好的做法。
SwingUtilities.invokeLater
接受一個 Runnable 並稍后在 UI 線程中調用它。
語法是:
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
//some code that render in the UI here.
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.