hi, i have a lab monday which requires me to generate random sized squares random poinst. im not to familiar with the random class and the syntax and all that but im guessin i need to use a for loop.
if someone can just point me in the right direction on what to do before the loop that would be nice. i will try and figure more out on my own while i wait for responses

so far i have this. but when i run it it only does one square. i want it to do many squares at once.

import javax.swing.*;
import java.awt.*;
import java.util.*;

public class randomSquares{
  public static void main(String[] args) {
    randomSquares d = new randomSquares();

  public randomSquares(){
    JFrame frame = new JFrame();
    frame.getContentPane().add(new MyComponent());

  public class MyComponent extends JComponent{
    public void paint(Graphics g){
      int height = 500;
      int width = 500;

	Random r = new Random();
	int number = r.nextInt();
	int random_number = number % 500; {
	if (random_number < 1) random_number = random_number + 500;
	for (int i = 0; i < 500; i++);{
		g.drawRect(random_number, random_number, random_number, random_number);

Edited by javanub123: n/a

7 Years
Discussion Span
Last Post by javanub123

i understand now that it is actually generating 500 shapes, there just in the same spot every time. How do i randomize that which is already random?

How do i randomize the
g.drawRect(random args here?);

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.