Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 图形循环故障,如何修复?_Java_Loops_Graphics - Fatal编程技术网

Java 图形循环故障,如何修复?

Java 图形循环故障,如何修复?,java,loops,graphics,Java,Loops,Graphics,我一直在努力做到这一点,所以有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; pr

我一直在努力做到这一点,所以有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
}}

}
import java.awt.*;
导入javax.swing.*;
公共类测试扩展了JPanel{//JPanel是一个类
int l=0;
私人整数x=10;
私人int y=500;
私人空位移动()
{
x++;
}
布尔红=假;
布尔蓝=假;
布尔绿色=假;
@凌驾
public void paint(Graphics g){//JPanel是中定义的类
超级油漆(g);
Graphics2D g2d=(Graphics2D)g;
g2d.setRenderingHint(RenderingHints.KEY_抗锯齿,
RenderingHints.VALUE_ANTIALIAS_ON);
颜色rectColor=新颜色(0,66,89);
g2d.setColor(rectColor);
//皮带
g2d.setColor(颜色为浅灰色);
g2d.fillRect(04501500200);
g2d.fillRect(700,02010000);
g2d.setColor(颜色为橙色);

对于(inti=-10000;i不幸的是,您做了很多错误的事情

  • 覆盖
    paintComponent
    而不是
    paint
  • 不要使用
    线程。睡眠
    。使用
    摆动计时器
    动作监听器
  • 您在绘制方法中做得太多。所有事件处理,包括调用
    repaint()
    都在事件调度线程(EDT)上完成。因此,所有更新都是在paintComponent内部完成的,因此退出时只显示最后绘制的对象。更新坐标、数据结构以及需要在绘制方法之外绘制的任何其他内容
  • 将您的
    锅炉板代码
    放入您的Testwork类构造函数中

    公共测试工作(){
    setPreferredSize(新尺寸(1000700));
    计时器=新计时器(0,此);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    框架。添加(此);
    //调整框架和jpanel的大小并组织组件。
    frame.pack();
    //使窗口在屏幕中居中
    frame.setLocationRelativeTo(空);
    //以毫秒为单位设置延迟
    定时器设置延迟(100);
    //启动计时器
    timer.start();
    }
    
    下面是您的actionListener代码

    public void actionPerformed(ActionEvent ae){
    //更新任何需要在内部使用的变量
    //在这里画例行公事。这意味着如果你想移动一些东西
    //在此处更新坐标,然后在绘制方法中使用它们。
    }
    
    当你启动你的应用程序时,像这样做

    publicstaticvoidmain(字符串[]args){
    invokeLater(()->newtestwork());
    }
    

    <> P>绘画。我建议在java教程中签出。这是这个网站的另一个例子。

    如果你想让人读你的代码,考虑使用缩进来让它更可读。