Is it possible to generate random colors using only the StdDraw class in Java?

  • Context: Java 
  • Thread starter Thread starter Hiche
  • Start date Start date
  • Tags Tags
    Random
Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
1 reply · 5K views
Hiche
Messages
82
Reaction score
0
Code:
import java.awt.Color;

public class ChangingColor 
{	
	public static void main(String[] args)
	{
		while (true)
		{
			int R = (int) (Math.random() * 256);
			int G = (int) (Math.random() * 256);
			int B = (int) (Math.random() * 256);	
			Color randomColor = new Color(R, G, B);
			
			StdDraw.setPenColor(randomColor);
			StdDraw.filledSquare(.5, .5, .25);
			StdDraw.show(500);
		}
	}

}

Okay, this works. But is there another way to code this using only the StdDraw class? I used Color, but we haven't covered this a lot.
 
Physics news on Phys.org