0

I'm trying to create a game that randomly generates pellets on a frame and you have to catch them. I'm calling a random number between 1 to 640 for my x-coordinate and a random number between 1 to 480 for my y-coordinate, then i'm using those to draw the pellet on the DrawingComponent. It does generate random points but it keeps running so that the points never actually stay put on the frame. I tried substituting the random integer with a concrete integer and it works perfectly. What am I doing wrong?

private class DrawingComponent extends JComponent
{
    protected void paintComponent(Graphics g)
    {
        Graphics2D g2d = (Graphics2D) g;

        //pellet 1 uses the random integers as coordinates and the pellet just jumps all over the place
        //pellet 2 uses concrete integer 100 as coordinates and the pellet stays put...this is what i want to happen
        pellet1 = new Pellet(randX(),randY()); 
        pellet2 = new Pellet(100,100);
        pellet1.draw(g2d); 
        pellet2.drawPellet2(g2d);
        }
    }
public int randX()
{
    Random rand = new Random();
    int x = rand.nextInt((640-1)+1)+1;
    return x;
}
public int randY()
{
    Random rand = new Random();
    int y = rand.nextInt((480-1)+1)+1;
    return y;
}
user8638151
  • 21
  • 1
  • 7

0 Answers0