user interface - Java- random spawning of rectangles -
here's part of code doesn't work want.when rectangle in loop painted, it's every time painted @ same place, despite fact used random number x , y axis.i paint rectangle 5 times (as it's set in loop) , each on random coords.if whole code necessary, let me know please.thank you!
public void paintcomponent(graphics g){ random=new random(); rx=random.nextint(500); ry=random.nextint(500); super.paintcomponent(g); for(int i=0;i<=5;i++){ g.fillrect(rx,ry,20,20); } g.setcolor(color.red); g.filloval(x,y,20,20); }
currently code generates coordinates 1 time. (thanks jon skeet pointing out)
if want paint 5 different trianlges should move call random.nextint
inside 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); }
Comments
Post a Comment