gpt4 book ai didi

java - 动画和 SwingUtilities.invokeLater

转载 作者:行者123 更新时间:2023-11-29 04:35:25 29 4
gpt4 key购买 nike

我想使用 SwingUtilities.invokeLater 方法,以便我的程序的所有 Swing 组件都由事件调度线程更新,因为这是一个很好的做法.

但是如果我将 main 方法的所有代码包装在 SwingUtilities.invokeLater(new Runnable { public void run() {/* code */}}); 窗口卡住(这是正常的,因为我的代码有一个需要几秒钟才能完成的动画循环)。我应该将 SwingUtilities.invokeLater 方法放在哪里?

程序代码(没有SwingUtilities.invokeLater方法)

import java.awt.Color;
import java.awt.Dimension;
import java.awt.geom.Rectangle2D;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Test {

public static void main(String[] args) {
int width = 854;
int height = 480;
String title = "Test";
BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
JFrame frame = new JFrame();
JPanel panel = new JPanel() {
protected void paintComponent(Graphics graphics) {
super.paintComponent(graphics);
Graphics2D graphics2D = (Graphics2D) graphics;
graphics2D.drawImage(bufferedImage, 0, 0, null);
}
};
frame.add(panel);
frame.getContentPane().setPreferredSize(new Dimension(width, height));
frame.pack();
frame.setTitle(title);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
int size = height/3;
int x = -size;

while (x <= width) {
Graphics2D graphics2D = (Graphics2D) bufferedImage.getGraphics();
graphics2D.setColor(Color.RED);
graphics2D.fill(new Rectangle2D.Double(x, 0, size, size));
graphics2D.setColor(Color.GREEN);
graphics2D.fill(new Rectangle2D.Double(x, size, size, size));
graphics2D.setColor(Color.BLUE);
graphics2D.fill(new Rectangle2D.Double(x, 2 * size, size, size));
graphics2D.dispose();
panel.repaint();
++x;

try {
Thread.sleep(10);
} catch (InterruptedException exception) {
exception.printStackTrace();
}
}

frame.dispose();
}
}

程序截图

animation

这是一个动画,其中三个彩色 strip 逐渐向窗口的右边缘延伸。

最佳答案

根据@MadProgrammer 的建议,这是已更新为使用 Timer 的自包含代码(顺便说一句,发布该代码时非常有用)。为了访问 x 变量,它被移动到为计时器定义的 Action 监听器中。为了从 Action 监听器访问 Timer,它被移动为一个类属性。后者意味着更容易将大部分代码移动到对象实例的构造函数中。

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import javax.swing.*;

public class Test {

Timer timer;

Test() {
int width = 854;
int height = 480;
String title = "Test";
BufferedImage bufferedImage = new BufferedImage(
width, height, BufferedImage.TYPE_INT_RGB);
JFrame frame = new JFrame();
JPanel panel = new JPanel() {
@Override
protected void paintComponent(Graphics graphics) {
super.paintComponent(graphics);
Graphics2D graphics2D = (Graphics2D) graphics;
// when you have an ImageObserver, may as well use it
//graphics2D.drawImage(bufferedImage, 0, 0, null);
graphics2D.drawImage(bufferedImage, 0, 0, this);
}

@Override
public Dimension getPreferredSize() {
return new Dimension(width,height);
}
};
frame.add(panel);
frame.pack();
frame.setTitle(title);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
int size = height / 3;

ActionListener animationListener = new ActionListener() {

int x = -size;

@Override
public void actionPerformed(ActionEvent e) {
if (x <= width) {
Graphics2D graphics2D = (Graphics2D) bufferedImage.getGraphics();
graphics2D.setColor(Color.RED);
graphics2D.fill(new Rectangle2D.Double(x, 0, size, size));
graphics2D.setColor(Color.GREEN);
graphics2D.fill(new Rectangle2D.Double(x, size, size, size));
graphics2D.setColor(Color.BLUE);
graphics2D.fill(new Rectangle2D.Double(x, 2 * size, size, size));
graphics2D.dispose();
panel.repaint();
++x;
} else {
timer.stop();
frame.dispose();
}
}
};
timer = new Timer(10, animationListener);
timer.start();
}

public static void main(String[] args) {
Runnable r = () -> {
new Test();
};
SwingUtilities.invokeLater(r);
}
}

关于java - 动画和 SwingUtilities.invokeLater,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41818129/

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