Java AWT 如何延迟绘制对象
Java AWT How to draw objects with delay
我想每 2 秒绘制一个新的随机形状。
我已经有一个 window,可以立即显示一些形状。我试着用 Timer 弄乱,让新的东西在几秒钟后出现在 window 中,但是没有用,或者整个程序卡住。使用定时器是个好主意吗?我应该如何实施它,使其发挥作用?
import javax.swing.*;
import java.awt.*;
import java.util.Random;
class Window extends JFrame {
Random rand = new Random();
int x = rand.nextInt(1024);
int y = rand.nextInt(768);
int shape = rand.nextInt(2);
Window(){
setSize(1024,768);
setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public void paint(Graphics g) {
super.paint(g);
g.setColor(new Color(0, 52, 255));
switch(shape) {
case 0:
g.fillOval(x, y, 50, 50);
break;
case 1:
g.fillRect(x,y,100,100);
break;
}
repaint();
}
}
public class Main {
public static void main(String[] args) {
Window window = new Window();
}
}
我也想画一些随意的形状。为此可以在 paint 方法中使用 switch 吗?我会做一个随机变量,如果它是 1 它将绘制矩形,如果它是 2 它将绘制椭圆形等。
首先,不要继承 JFrame;改为子类 JPanel,并将该面板放在 JFrame 中。
其次,不要覆盖 paint() - 而是覆盖 paintComponent() 。
第三,创建一个 Swing Timer,并在其 actionPerformed() 方法中进行所需的更改,然后调用 yourPanel.repaint()
首先,不要改变 JFrame
的绘制方式(换句话说,不要覆盖 JFrame
的 paintComponent()
)。创建 JPanel
的扩展 class 并改为绘制 JPanel
。其次,不要覆盖 paint()
方法。覆盖 paintComponent()
。第三,总是 运行 带有 SwingUtilities.invokeLater()
的 Swing 应用程序,因为它们应该 运行 在它们自己的名为 EDT(事件调度线程)的线程中。最后,javax.swing.Timer
就是您要找的。
看看这个例子。它每 1500 毫秒在随机 X、Y 中绘制一个椭圆形。
预览:
源代码:
import java.awt.BorderLayout;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
public class DrawShapes extends JFrame {
private ShapePanel shape;
public DrawShapes() {
super("Random shapes");
getContentPane().setLayout(new BorderLayout());
getContentPane().add(shape = new ShapePanel(), BorderLayout.CENTER);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(500, 500);
setLocationRelativeTo(null);
initTimer();
}
private void initTimer() {
Timer t = new Timer(1500, e -> {
shape.randomizeXY();
shape.repaint();
});
t.start();
}
public static class ShapePanel extends JPanel {
private int x, y;
public ShapePanel() {
randomizeXY();
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.fillOval(x, y, 10, 10);
}
public void randomizeXY() {
x = (int) (Math.random() * 500);
y = (int) (Math.random() * 500);
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> new DrawShapes().setVisible(true));
}
}
我想每 2 秒绘制一个新的随机形状。
我已经有一个 window,可以立即显示一些形状。我试着用 Timer 弄乱,让新的东西在几秒钟后出现在 window 中,但是没有用,或者整个程序卡住。使用定时器是个好主意吗?我应该如何实施它,使其发挥作用?
import javax.swing.*;
import java.awt.*;
import java.util.Random;
class Window extends JFrame {
Random rand = new Random();
int x = rand.nextInt(1024);
int y = rand.nextInt(768);
int shape = rand.nextInt(2);
Window(){
setSize(1024,768);
setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public void paint(Graphics g) {
super.paint(g);
g.setColor(new Color(0, 52, 255));
switch(shape) {
case 0:
g.fillOval(x, y, 50, 50);
break;
case 1:
g.fillRect(x,y,100,100);
break;
}
repaint();
}
}
public class Main {
public static void main(String[] args) {
Window window = new Window();
}
}
我也想画一些随意的形状。为此可以在 paint 方法中使用 switch 吗?我会做一个随机变量,如果它是 1 它将绘制矩形,如果它是 2 它将绘制椭圆形等。
首先,不要继承 JFrame;改为子类 JPanel,并将该面板放在 JFrame 中。 其次,不要覆盖 paint() - 而是覆盖 paintComponent() 。 第三,创建一个 Swing Timer,并在其 actionPerformed() 方法中进行所需的更改,然后调用 yourPanel.repaint()
首先,不要改变 JFrame
的绘制方式(换句话说,不要覆盖 JFrame
的 paintComponent()
)。创建 JPanel
的扩展 class 并改为绘制 JPanel
。其次,不要覆盖 paint()
方法。覆盖 paintComponent()
。第三,总是 运行 带有 SwingUtilities.invokeLater()
的 Swing 应用程序,因为它们应该 运行 在它们自己的名为 EDT(事件调度线程)的线程中。最后,javax.swing.Timer
就是您要找的。
看看这个例子。它每 1500 毫秒在随机 X、Y 中绘制一个椭圆形。
预览:
源代码:
import java.awt.BorderLayout;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
public class DrawShapes extends JFrame {
private ShapePanel shape;
public DrawShapes() {
super("Random shapes");
getContentPane().setLayout(new BorderLayout());
getContentPane().add(shape = new ShapePanel(), BorderLayout.CENTER);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(500, 500);
setLocationRelativeTo(null);
initTimer();
}
private void initTimer() {
Timer t = new Timer(1500, e -> {
shape.randomizeXY();
shape.repaint();
});
t.start();
}
public static class ShapePanel extends JPanel {
private int x, y;
public ShapePanel() {
randomizeXY();
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.fillOval(x, y, 10, 10);
}
public void randomizeXY() {
x = (int) (Math.random() * 500);
y = (int) (Math.random() * 500);
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> new DrawShapes().setVisible(true));
}
}