A draw method that draws the square at its xy coordinate


Write a class Square that represents a square to be drawn. Store the following information in instance variables: 
? size (length of any side)
? x coord of upper left-hand corner 
? y coord of upper left-hand corner 
? color 

Provide the following methods: 
? A parameterless constructor that generates random values for the size, color, x, and y. Make the size between 100 and 200, x between 0 and 600, y between 0 and 400. The squares can be any color-note that you can pass a single int parameter to the Color constructor, but it will only consider the first 24 bits (8 bits for each of R, G, B component).

? IMPORTANT: Your random number generator must be declared at the class level (not inside the constructor), and must be declared static. So its declaration and initialization should appear with the declarations of size, color, x, and y, and should look like this: 

private static Random generator = new Random();

? A draw method that draws the square at its x,y coordinate in its color. Note that you need a Graphics object, like the page parameter of the paint method, to draw.Your draw method should take a Graphics object as a parameter. 

2. Now write an applet DrawSquares that uses your Square class to create and draw 5 squares. This code should be very simple; the paint method will simply create a Square and then draw it, repeated 5 times. Don't forget to pass the Graphics object to draw. 

Request for Solution File

Ask an Expert for Answer!!
Computer Graphics: A draw method that draws the square at its xy coordinate
Reference No:- TGS0113945

Expected delivery within 24 Hours