Search code examples
javaimagecollision

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();
    }
}
}

Solution

  • 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();
    
    }