Java Program: Rectangles Play Ground


This assignment will give you practice with layout managers and some of the more advanced user interface components. You are to create an application that allows a user to manipulate a two-dimensional grid of cells. The user will have controls to modify the number of rows, the number of columns, whether or not to display grid lines and what color to use for coloring the cells.


  • This frame is 400 by 400 pixels with 8 rows, 8 columns, color set to red and with the grid lines option selected.
  • The cells should always start out white and grid lines should always be drawn in black.
    • As the user clicks inside of any of the cells in the grid, that cell should be colored using the current color (initially red).
    • The rectangles are initially white.
    • If a user clicks on a rectangle that was previously colored, it should be given the current color (e.g., a rectangle previously set to green can be changed to red).
    • The grid lines should be visible even when rectangles have been colored.
  • The user should be allowed to change the current color using the buttons at the top of the frame.
    • At any given time, only one of the three colors should be selected.
    • When the color is changed, future mouse clicks should generate that color but rectangles that were previously given a different color should stay in the original color.
    • The user should, however, be allowed to change the color of a previously colored rectangle by clicking on it a second time.
    • The user also has the option to turn off the grid lines option, in which case no grid lines will be visible (not even white lines where the black lines were).
  • The user should also be allowed to change the number of rows and columns to be used.
    • The user should be allowed to enter the values in labeled text boxes that are each 3 columns wide.
    • Your program does not have to generate an error message if the user types an illegal string or an illegal number of rows and columns (e.g., less than 1)
      • but it should trim the string to ignore extra spaces and is not allowed to generate an exception message in the console window.
      • That means that you must catch the error.
      • It is sufficient to simply have your program ignore an illegal value for rows or columns.
    • You must attach both an action listener and a focus listener to the two text fields so that the panel is updated when the user hits enter and when the user positions to another user interface element.
      • You can use the same listener for both text fields, always updating both the rows and columns of the panel, but you have to make sure that an illegal value for one of the two does not prevent an update of the other.
      • You might want to declare a named inner class so that it can serve as both the action listener and the focus listener.
      • If you’re interested, you can use methods like setNextFocusableComponent or isFocusTraversable to limit the focus to just the two text fields (this is desirable behavior, but you won’t get extra credit for implementing it—just the knowledge that you have a better-behaved program).
  • When rows or columns are changed, your program should resize the rectangles in the grid but not change any of the colors.
    • For example, if the rectangle in row 2 and column 2 was colored red before, it should still be colored red after resizing.
    • If, however, the user reduces the number of rows or the number of columns, some rectangles will no longer be legal, in which case their color information should be forgotten.
      • For example, suppose that the rectangle in row 8 and column 8 of the original figure is colored red. If the user then changes the number of rows to 6, that rectangle no longer exists, so its color information should be forgotten. If the user later sets the number of rows back to 8, the rectangle at row 8 and column 8 should be white, not red.
  • The grid with the colored rectangles should be given all of the extra horizontal and vertical space if the frame is resized.
    • When computing the sizes of the rectangles, you will generally find that there is extra space because the grid size is not an exact multiple of the number of rows or columns.
    • Any excess space should surround the grid uniformly and should be colored black.
    • In other words, you should construct the largest grid possible with cells all the same size and should center it horizontally and vertically within the available space, with any excess space painted black.
    • You should implement the grid of colored squares using a single JPanel.
    • In other words, you are to actually draw lines and rectangles rather than trying to use layout managers to do the work for you.
  • Remember that the outer black area is not part of the grid, so your program must ignore any mouse clicks on the black background.
    • Your program must properly recognize mouse clicks just inside a rectangle, but you can decide which adjacent rectangle to color if the user clicks right on top of a grid line (whether visible or not).
  • You may use “magic numbers” for the original frame size and you may use specific color names, but you should introduce constants for other arbitrary numbers.


By far, this was one of the hardest programs I did for school. There were many fine points that had to be met in the spec. Besides taking clicks and coloring, the grid had to be able to change shape, and maintain a black border around the clicking area that changed depending on shape, size, and number of columns and rows. It was also a great chance to practice my GUI skills.I had to calculate where the user was clicking and then color the appropriate square. Even more complicated was dealing with the changing sizes of the window. This ment adjusting not only the already draw squares, but also adjusting the click map for the bigger squares.


Comments: [Add a Comment]

Creative Commons License   This webpage and all of its contents are licensed under a
Creative Commons License by