0

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

基本上我的程序应该:

  1. 在边界内的随机位置绘制 25 个随机大小的椭圆 -完成
  2. 画一个球并让它移动 -完成
  3. 让球弹起来——还没做,但我知道怎么做

但它不断重复第一步。

这是我编写的代码,哦,我现在必须使用小程序,这是课程的一部分,请不要建议我使用 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);
      }
}
4

1 回答 1

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
    }
}
于 2013-04-15T01:53:19.480 回答