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
Click For Summary
SUMMARY

The forum discussion confirms that generating random colors in Java using the StdDraw class is feasible by utilizing the Color class for RGB values. The provided code snippet demonstrates how to create a continuously changing color square by generating random integers for red, green, and blue components. While the user expresses a desire to explore alternatives using only StdDraw, the current implementation effectively achieves the desired outcome. The StdDraw library, commonly used in Princeton's introductory programming courses, offers various methods that can be leveraged for graphical applications.

PREREQUISITES
  • Java programming fundamentals
  • Understanding of the StdDraw library
  • Basic knowledge of RGB color model
  • Familiarity with loops and random number generation in Java
NEXT STEPS
  • Explore the StdDraw documentation for additional methods and properties
  • Investigate alternative color generation techniques in Java
  • Learn about Java's Graphics class for more advanced drawing capabilities
  • Experiment with creating animations using StdDraw
USEFUL FOR

Java developers, computer science students, and educators looking to enhance their understanding of graphical programming with the StdDraw library.

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

Similar threads

  • · Replies 4 ·
Replies
4
Views
1K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K
Replies
8
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 9 ·
Replies
9
Views
3K