gpt4 book ai didi

java - 图形循环故障,如何解决?

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

我一直在努力完成这项工作,以便有 20 个盒子,每个盒子有 3 种不同的尺寸和 3 种不同的颜色,随机选择,但我无法让它们在不同的时间出现,它们只是互相卡住。颜色乱七八糟之类的,有人知道怎么解决吗?这是我到目前为止得到的:

    import java.awt.*;
import javax.swing.*;
public class testwork extends JPanel { //JPanel is a class

int l = 0;
private int x = 10;
private int y = 500;
private void move()
{
x++;
}
boolean red = false;
boolean blue = false;
boolean green = false;


@Override
public void paint(Graphics g) { //JPanel is a class defined in
super.paint(g);
Graphics2D g2d = (Graphics2D) g;

g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);

Color rectColor = new Color(0, 66, 89);
g2d.setColor(rectColor);
//belt
g2d.setColor(Color.lightGray);
g2d.fillRect(0,450,1500,200);
g2d.fillRect(700,0,200,1000);
g2d.setColor(Color.orange);
for (int i = -10000; i<10000; i=i+50) {
int m= i++;
g2d.fillRect(m, 450, 25, 200);
}
g2d.setColor(Color.DARK_GRAY);
g2d.fillRect(700, 450, 200, 200);
//boxes
while (l<=20) {
if (Math.random() < 0.5)
{g2d.setColor(Color.RED);;}
else if (Math.random() < 0.5) {g2d.setColor(Color.GREEN);}
else {g2d.setColor(Color.BLUE);}

if (Math.random() < 0.5)
{g2d.fillRect(x,y,50,50);}
else if (Math.random() < 0.5) {g2d.fillRect(x,y,50,100);}
else {g2d.fillRect(x,y,100,50);}
l++;
}

}

public static void main(String[] args) throws InterruptedException {
JFrame frame = new JFrame("Frame"); //Add our JPanel to the frame
frame.add(new attempt());//instantiate a new object

frame.setSize(1500, 1000);

frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
testwork p = new testwork();
frame.add(p);
while (true)
{
p.move(); //Updates the coordinates
p.repaint();
Thread.sleep(10); //Pauses for a moment
}}

}

最佳答案

不幸的是,您做错了很多事情。

  1. 覆盖 paintComponent 而不是 paint
  2. 不要使用Thread.sleep。使用 Swing 计时器ActionListener
  3. 你在绘画方法上做得太多了。所有事件处理(包括对 repaint() 的调用)都是在事件调度线程 (EDT) 上完成的。因此,所有更新都是在 PaintComponent 内部完成,因此退出时只会显示最后绘制的对象。更新您的坐标、数据结构以及需要在绘制方法之外绘制的任何其他内容。

将您的样板代码放入您的Testwork类构造函数中。这是一个例子。

  public Testwork() {

setPreferredSize(new Dimension(1000, 700));
Timer timer = new Timer(0, this);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.add(this);
// sizes the frame and jpanel and organizes the components.
frame.pack();
// centers the window in the screen
frame.setLocationRelativeTo(null);
// sets the delay in milliseconds
timer.setDelay(100);
// starts the timer
timer.start();
}

这是您的 actionListener 代码。

     public void actionPerformed(ActionEvent ae) {
// update any variables that need to be used int he
// paint routine here. That means if you want to move something
// update the coordinates here and then use them in the paint method.

}

当你启动你的应用程序时,这样做

    public static void main(String[] args) {
SwingUtilities.invokeLater(() -> new Testwork());
}

绘画还有很多东西。我建议查看Custom Painting在 Java 教程中。 Here是这个(SO)网站上的另一个例子。

关于java - 图形循环故障,如何解决?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58733570/

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