[英]Animations and SwingUtilities.invokeLater
I would like to use the method SwingUtilities.invokeLater
so that all the Swing components of my program are updated by the event dispatch thread , since it is a good practice. 我想使用方法
SwingUtilities.invokeLater
以便我的程序的所有Swing组件都由事件派发线程更新,因为这是一个很好的做法。
But if I wrap all the code of the main
method in SwingUtilities.invokeLater(new Runnable { public void run() { /* code */ }});
但是如果我在
SwingUtilities.invokeLater(new Runnable { public void run() { /* code */ }});
包装main
方法的所有代码SwingUtilities.invokeLater(new Runnable { public void run() { /* code */ }});
the window freezes (which is normal since my code has an animation loop that takes a few seconds to complete). 窗口冻结(这是正常的,因为我的代码有一个动画循环需要几秒钟才能完成)。 Where should I put that
SwingUtilities.invokeLater
method? 我应该在哪里放置
SwingUtilities.invokeLater
方法?
SwingUtilities.invokeLater
method) 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();
}
}
This is an animation in which the three coloured strips are gradually stretching to the right edge of the window. 这是一个动画,其中三个彩色条带逐渐拉伸到窗口的右边缘。
Here is that self-contained code (good call on posting that, BTW) updated to use a Timer
as suggested by @MadProgrammer. 以下是@MadProgrammer建议的自包含代码(发布的好消息,BTW)更新为使用
Timer
。 In order to access the x
variable, it was moved into the action listener defined for the timer. 为了访问
x
变量,它被移动到为计时器定义的动作侦听器中。 In order to access the Timer
from within the action listener, it was moved to being a class attribute. 为了访问
Timer
从动作监听器内 ,它被转移到作为一个类属性。 The latter meant it was easier to move the bulk of the code into a constructor for an instance of the object. 后者意味着将大量代码移动到对象实例的构造函数中更容易。
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);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.