gpt4 book ai didi

Java Swing 应用程序留下痕迹的 gif 动画

转载 作者:行者123 更新时间:2023-12-02 09:20:08 24 4
gpt4 key购买 nike

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

目前它的动画效果很好,但有一个问题 - 它似乎在动画时留下了训练图像。

enter image description here

这是我正在使用的代码:

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 中的 invokeLater 方法是一个很好的做法。类(class)。

SwingUtilities.invokeLater 获取一个 Runnable 并稍后在 UI 线程中调用它。

语法是:

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

关于Java Swing 应用程序留下痕迹的 gif 动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58754566/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com