w3hello.com logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML videos Categories
Java- random spawning of rectangles

Currently your code only generates the coordinates only one time. (Thanks to Jon Skeet for pointing it out)

If you want it to paint five different trianlges you should move the call to random.nextInt inside the loop.

public void paintComponent(Graphics g){
    random=new Random();

    super.paintComponent(g);    

    for(int i=0; i<=4; i++){
        rX=random.nextInt(500);
        rY=random.nextInt(500);
        g.fillRect(rX,rY,20,20);
    }


    g.setColor(Color.red);
    g.fillOval(x,y,20,20);

}




© Copyright 2018 w3hello.com Publishing Limited. All rights reserved.