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

我一直在努力使之成为一个随机选择的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,200,1000);
g2d.setColor(Color.orange);
for (int i = -10000; i<10000; i=i+50) {
    int m= i++;
    g2d.fillRect(m,25,200);
}
g2d.setColor(Color.DARK_GRAY);
g2d.fillRect(700,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,100);}
else {g2d.fillRect(x,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
}}

}
ccczzzxxx 回答:图形循环故障,如何解决?

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

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

boiler plate code放入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());
    }

绘画还有很多。我建议在Java教程中签出Custom PaintingHere是该(SO)网站上的另一个示例。

本文链接:https://www.f2er.com/3150898.html

大家都在问