Generating Squares

Write a class named SquareGenerator that extends the OneButtonBreadboard class in the acm.breadboards package. When run, this class should prompt the user to click the button labeled "Draw!" to draw 50 squares in the central graphics area of the breadboard window, pausing 50 milliseconds between drawing each one. They should be of random size (i.e., side length), color and location, unless the user types one of the following in the input text field:

  • "color = " followed by either "red", "green" or "blue"; or
  • "size = " followed by an integer or decimal value

In the first case, the color of the rectangle drawn should be either red, green, or blue, as indicated by the user -- while the size and location should still be chosen at random.

In the second case, the side length of the square drawn should agree with the user input -- while the color and location are still chosen at random.

While the square's location is always chosen randomly, it must be chosen so that it appears fully in the central graphics area of the application window. (i.e., no squares should be only "partly visible")

Below shows what this program could look like after repeated hits of the "Draw!" button with the shown text in the input text field each time:


  • One can find the width and height of the central drawing area of a OneButtonBreadboard by doing something similar to what is used in the following code:

    import java.awt.Color;
    import acm.breadboards.OneButtonBreadboard;
    public class BorderFun extends OneButtonBreadboard{
      public void run() {
        double textAreaPanelHeight = 
        double textFieldPanelHeight = 
        //draw a pink rectangle that exactly fills the central drawing area
        GRect rect = new GRect(0,0,499,499-textAreaPanelHeight-textFieldPanelHeight);
        rect.setFillColor(new Color(255,200,200));
  • Don't compare strings with the "==" operator, which is meant only for use with primitive data types. Instead, use the equals() method of the String class as shown in the following:

    String s1 = "apple";
    String s2 = "orange";
    String s3 = "orange";
    boolean firstTwoEqual = s1.equals(s2);    // which is false in this case
    boolean lastTwoEqual = s2.equals(s3);     // which is true in this case
  • Every color under the rainbow can be expressed as some combination of the primary colors red, green, and blue. To get different colors, one simply varies the intensity of each primary color combined. For example,

    • purple = 100% red + 0% green + 100% blue
    • orange = 100% red + 68% green + 0% blue
    • gray = 50% red + 50% green + 50% blue
    • white = 100% red + 100% green + 100% blue
    • black = 0% red + 0% green + 0% blue
    When we create a new instance of the java.awt.Color class, we can specify the intensity of each primary color with an integer from 0 to 255, with 0 corresponding to 0% and 255 corresponding to 100%. With this in mind, and presuming that random is an instance of the java.util.Random class, the following code will create a random color:
    int red = random.nextInt(256);
    int green = random.nextInt(256);
    int blue = random.nextInt(256);
    Color randomColor = new Color(red,green,blue);
  • The java.awt.Color class offers a quick way to specify certain colors. For example, in the following code snippet, both color1 and color2 end up being green:

    Color color1 = new Color(0,255,0); 
    Color color2 = Color.GREEN;
    To find out which colors with which you can use this shortcut, consult the Java API.
  • Presuming rect is an instance of the class and someFillColor is an instance of the java.awt.Color class, the following will fill the rectangle rect with the color someFillColor.
  • Don't forget that instances of GRect must be manually added to the window after they have been created, as shown below:
    GRect myRect = new GRect(x,y,height,width);  //creates GRect named myRect, presuming 
                                                 //x, y, height, & width are doubles 
                                                 //that already have assigned values
    this.add(myRect);   //adds the rectangle to the window 
  • You can look in the ACM documentation to find out more about GRects and the methods they offer.

  • Notice that there is a space between the word "color" and the "=", and another between the "=" and the color the user specifies. There are similar spaces in the user-entered command to fix the size of the squares. The presence of these spaces may offer you a way to parse the user-input command with an instance of the java.util.Scanner class...