简体   繁体   English

每当我开始运行程序时如何设置随机位置?

[英]How to make a random position whenever I start running the program?

I have an image which moves in a random direction. 我有一个向随机方向移动的图像。 My problem is everytime I start running the program, the image always appear at the upper-left corner and moves a diagonal direction and after hitting the wall, it starts to move random direction. 我的问题是,每当我开始运行该程序时,图像总是出现在左上角并向对角线方向移动,碰到墙后,图像就开始向随机方向移动。 How can I make the image appear in a random position everytime I execute or run the program? 每次执行或运行程序时,如何使图像显示在随机位置? Any help would be much appreciated... 任何帮助将非常感激...

Here's the code: 这是代码:

public class Ball extends JPanel implements Runnable
{

private Image ball;
private Thread animator;
private int x;
private int y;
private final int DELAY = 50;
private int xVelocity = 1;
private int yVelocity = 1;
private static final int RIGHT_WALL = 400;
private static final int LEFT_WALL = 1;
private static final int DOWN_WALL = 400;
private static final int UP_WALL = 1;
private boolean showImage;

public Ball()
{
    randomPosition();
    setRandomDirection();
    setBackground(Color.BLACK);
    setDoubleBuffered(true);

    ImageIcon ii = new ImageIcon(this.getClass().getResource("ball.gif"));
    ball = ii.getImage();
    x = y = 10;

}

public void addNotify()
{
    super.addNotify();
    animator = new Thread(this);
    animator.start();
}

public void paint(Graphics g)
{
    super.paint(g);

    Graphics2D g2d = (Graphics2D) g;
    g2d.drawImage(ball, x, y, this);
    Toolkit.getDefaultToolkit().sync();
    g.dispose();
}

public void move()
{

    x += xVelocity;
    y += yVelocity;

    if (x >= RIGHT_WALL)
    {
        x = RIGHT_WALL;

        randomDirection();
    }

    if (y <= UP_WALL)
    {
        y = UP_WALL;

        randomDirection();
    }

    if (x <= LEFT_WALL)
    {
        x = LEFT_WALL;

        randomDirection();
    }
    if (y >= DOWN_WALL)
    {
        y = DOWN_WALL;

        randomDirection();
    }

}

private void randomDirection()
{
    double speed = 2.0;
    double direction = Math.random() * 2 * Math.PI;
    xVelocity = (int) (speed * Math.cos(direction));
    yVelocity = (int) (speed * Math.sin(direction));

}


private void randomPosition()
{
    x = LEFT_WALL + (int) (Math.random() * (RIGHT_WALL - LEFT_WALL));
    y = UP_WALL + (int) (Math.random() * (DOWN_WALL - UP_WALL));
}


public void run()
{
    long beforeTime, timeDiff, sleep;
    beforeTime = System.currentTimeMillis();
    while (true)
    {

        cycle();
        repaint();

        timeDiff = System.currentTimeMillis() - beforeTime;
        sleep = DELAY - timeDiff;

        if (sleep > 2)
        {
            sleep = 1;
        }
        try
        {
            Thread.sleep(sleep);
        }
        catch (InterruptedException e)
        {
            System.out.println("interrupted");
        }

        beforeTime = System.currentTimeMillis();
    }
}
}

All you need is a setter for x and y in your moving object: 您需要做的就是在移动对象中使用x和y的二传手:

public void randomStart(){
    this.x = Math.random() * this.RIGHT_WALL;
    this.y = Math.random() * this.DOWN_WALL;
}

Then you call this from your main(). 然后,您可以从main()调用此函数。

Like in your randomDirection() method, you should initialize the x and y position of you image randomly: 像在randomDirection()方法中一样,您应该随机初始化图像的x和y位置:

private void randomPosition() {
    x = LEFT_WALL + (int) (Math.random() * (RIGHT_WALL - LEFT_WALL));
    y = UP_WALL + (int) (Math.random() * (DOWN_WALL - UP_WALL));
}

You also should call the randomDirection() method once in the constructor, so your image moves in another direction, everytime you start your program. 您还应该在构造函数中调用一次randomDirection()方法,以便每次启动程序时图像都朝另一个方向移动。

So the constructor should look like this: 因此,构造函数应如下所示:

public YourClassName() {
    randomPosition();
    randomDirection();
}

You also have to generate initialized value randomly. 您还必须随机生成初始化值。

Actually you always take : 实际上,您总是:

private int x, y;
private int xVelocity = 1;
private int yVelocity = 1;

so x == 0 , y == 0 , xVelocity == 1 and yVelocity == 1 as static initial values for your variables. 因此x == 0y == 0xVelocity == 1yVelocity == 1是变量的静态初始值。

Random r = new Random()  
public Ball()
{
    randomPosition();
    setRandomDirection();
    setBackground(Color.BLACK);
    setDoubleBuffered(true);

    ImageIcon ii = new ImageIcon(this.getClass().getResource("chicken.gif"));
    ball = ii.getImage();
    this.x = r.nextInt();
    this.y = r.nextInt();

}

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

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