Java Applet添加线程使while循环无限

时间:2013-04-14 19:42:30

标签: java multithreading graphics applet awt

我正在尝试制作一个生成25个随机椭圆的程序,然后画一个球并让它反弹,我做了一些,我生成了椭圆形,我让球移动但是当我添加线程时它不断重复绘制椭圆形循环,我有点为什么会发生这种情况,但我不知道如何解决它。

基本上我的程序应该:

  1. 在边框内的随机位置绘制25个随机大小的椭圆 - 已完成
  2. 画一个球然后移动 - 完成
  3. 让球反弹 - 没有完成,但我知道该怎么做
  4. 但它不断重复第一步。

    这是我写的代码,哦,我现在必须使用applet它的部分课程请不要建议我使用swing或其他东西:

    import java.applet.Applet;
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Image;
    
    public class As4B extends Applet implements Runnable 
    {
    
        public int x, y;
        public int width = 854;
        public int height = 480;
        public int border = 20;
        public Image offscreen;
        public Graphics d;
    
    
        public void init()
        {
            setSize(width,height);
            Thread th = new Thread(this);
            th.start();
            offscreen = createImage(width,height);
            d = offscreen.getGraphics();
        }
    
        public void run() 
        {
            x = 100;
            y = 100;
            while(true)
            {
                x ++;
                y ++;
                repaint();
                try {
                    Thread.sleep(50);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
    
        }
        public void paint(Graphics gfx)
        {
            d.setColor(java.awt.Color.black);
            d.fillRect(0, 0, width, height);
            d.setColor(java.awt.Color.green);
            d.fillRect(0 + border, 0 + border, (width - (border * 2)), (height - (border* 2)));
            genOval(25, d);
    
            d.setColor(Color.gray);
            d.fillOval(x, y, 50, 50);
            gfx.drawImage(offscreen, 0, 0, this);
    
        }
        public int random(int low, int high)
        {
            int answer =(int)((Math.random()*(high-low))+ low);
            return answer;
        }
        public void genOval(int amount, Graphics f)
        {
            int ranWidth, ranHeight, ranX, ranY, red, blue, green;
            int i = 0;
            while(i < 25)
            {
                green = random(0,255);
                blue = random(0,255);
                red = random(0,255);
    
                f.setColor(new Color(red,green,blue));
    
                ranWidth = random(30,400);
                ranHeight = random(30,200);
                ranX = random(0 + border, ((width - border)- (ranWidth)));
                ranY = random(0 + border , ((height - border)- (ranHeight)));
    
                f.fillOval(ranX, ranY, ranWidth, ranHeight);
                i++;
            }   
        }
    
        public void update(Graphics gfx) {
            paint(gfx);
          }
    }
    

1 个答案:

答案 0 :(得分:0)

你的genOval()方法没有持久的支持。每次调用repaint()时(通过你的线程),都会调用paint()方法,这会为随机椭圆生成新的位置。您需要为该信息创建持久性源,如下所示:

List<Rectangle> rectangles = new ArrayList<Rectangle>();
List<Color> colors = new ArrayList<Color>();
public void init() {
    ...
    for (int i = 0; i < 25; i++) {
        int green = random(0,255);
        int blue = random(0,255);
        int red = random(0,255);

        colors.add(new Color(red,green,blue));

        ranWidth = random(30,400);
        ranHeight = random(30,200);
        ranX = random(0 + border, ((width - border)- (ranWidth)));
        ranY = random(0 + border , ((height - border)- (ranHeight)));

        rectangles.add(new Rectangle(ranX, ranY, ranWidth, ranHeight));
    }
}

public void genOval(Graphics g) {
    for (int i = 0; i < 25; i++) {
        Color color = colors.get(i);
        Rectangle rectangle = rectangle.get(i);
        // Draw using color & rectangle
    }
}