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

  • Thread starter Thread starter Hiche
  • Start date Start date
  • Tags Tags
    Random
Click For Summary
The discussion revolves around a Java program that generates a random color and uses the StdDraw class to display a filled square in that color. The code utilizes the Color class to create RGB values, which are randomly generated within the range of 0 to 255. The program continuously updates the square's color every half second. A query is raised about whether it's possible to achieve the same effect using only the StdDraw class, without relying on the Color class. Participants are encouraged to explore the StdDraw.java file to identify available public methods and properties that could facilitate this alternative approach.
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.
 
Technology news on Phys.org
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

  • · Replies 4 ·
Replies
4
Views
1K
  • · Replies 0 ·
Replies
0
Views
627
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K
Replies
8
Views
2K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 0 ·
Replies
0
Views
1K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 9 ·
Replies
9
Views
3K