简体   繁体   English

Java Applet For-Loop在5次迭代后停止绘制

[英]Java Applet For-Loop to Stop Paint after 5 iterations

I'm very new to this so bear with me. 我很陌生,所以请耐心等待。 I've made an applet that produces a fan shape starting in the upper left-hand corner of the frame (0,0). 我制作了一个小程序,从框架的左上角开始产生扇形(0,0)。 The program draws 200 randomly colored lines moving from the top to the bottom of the applet. 该程序绘制了从applet的顶部到底部移动的200条随机彩色线条。 It them reverses the process and leaves you with a blank applet frame. 它们会反转过程并为您留下一个空白的小程序框架。 I want to have this iterate 5 times however my attempts at using a for-loop have not been successful. 我希望这次迭代5次,但是我使用for循环的尝试还没有成功。

The code I'm using in the for loop is: for (int times = 0; times < 5; times++) { if (painting) 我在for循环中使用的代码是:for(int times = 0; times <5; times ++){if(painting)

I have a Boolean in the Main class for painting. 我在Main类中有一个用于绘画的布尔值。 boolean painting=true; boolean painting = true;

I've resorted to repeating the code 5 times, but I know there should be a better way using a for-loop. 我已经使用了5次重复代码,但我知道使用for循环应该有更好的方法。

My code with the 5 repeats: 我的代码有5个重复:

import java.awt.Color;
import java.awt.Graphics;
import java.util.Random;
import javax.swing.JApplet;

public class Main extends JApplet {

private static Random rand = new Random();

public void init() {
    setBackground(Color.WHITE);
    setSize(400, 300);
}

public void paint(Graphics g) {

    for (int i = 0; i < 200; i++) {
        Wait.holdOn(10);
        g.setColor(new Color(rand.nextInt(0xFFFFFF)));
        g.drawLine(0, 0, 200 - i, i);
    }
    for (int i = 0; i < 200; i++) {
        Wait.holdOn(10);
        g.setColor(getBackground());
        g.drawLine(0, 0, i, 200 - i);

    }
    for (int i = 0; i < 200; i++) {
        Wait.holdOn(10);
        g.setColor(new Color(rand.nextInt(0xFFFFFF)));
        g.drawLine(0, 0, 200 - i, i);
    }
    for (int i = 0; i < 200; i++) {
        Wait.holdOn(10);
        g.setColor(getBackground());
        g.drawLine(0, 0, i, 200 - i);

    }
    for (int i = 0; i < 200; i++) {
        Wait.holdOn(10);
        g.setColor(new Color(rand.nextInt(0xFFFFFF)));
        g.drawLine(0, 0, 200 - i, i);
    }
    for (int i = 0; i < 200; i++) {
        Wait.holdOn(10);
        g.setColor(getBackground());
        g.drawLine(0, 0, i, 200 - i);

    }
    for (int i = 0; i < 200; i++) {
        Wait.holdOn(10);
        g.setColor(new Color(rand.nextInt(0xFFFFFF)));
        g.drawLine(0, 0, 200 - i, i);
    }
    for (int i = 0; i < 200; i++) {
        Wait.holdOn(10);
        g.setColor(getBackground());
        g.drawLine(0, 0, i, 200 - i);

    }
    for (int i = 0; i < 200; i++) {
        Wait.holdOn(10);
        g.setColor(new Color(rand.nextInt(0xFFFFFF)));
        g.drawLine(0, 0, 200 - i, i);
    }
    for (int i = 0; i < 200; i++) {
        Wait.holdOn(10);
        g.setColor(getBackground());
        g.drawLine(0, 0, i, 200 - i);

    }
}


}

I also have the wait class: 我也有等待班:

public class Wait{
public static void holdOn(long period){
try{
  Thread.sleep(period);
}catch(Exception e){}
}
}

Do not, ever, wait within a paint method...or the Event Dispatching Thread. 不要在paint方法......或Event Dispatching Thread中等待。 This will simply make your program look like's stuttering... 这只会让你的程序看起来像是口吃......

Take a look at Concurrency in Swing and How to Use Swing Timers ... 看看Swing中的Concurrency如何使用Swing Timers ......

You should also ensure that you are calling super.paint before you perform any kind of painting! 在进行任何绘画之前,你还应该确保你正在调用super.paint In fact, you should avoid overriding paint , as this will cause your updates to flicker, instead create a custom component, from something like JPanel and override it's paintComponent method, see Performing Custom Painting for more details... 实际上,你应该避免重写paint ,因为这会导致你的更新闪烁,而不是像JPanel那样创建一个自定义组件,并覆盖它的paintComponent方法,有关更多详细信息,请参阅执行自定义绘画 ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM