This is a bonus program! It's meant to be a bit difficult.
Write a GraphicsProgram that draws a centered, numbered square grid on the screen.
Your program should be able to handle different sizes of the grid:
Guidelines
Start by making a grid that starts at position (0,0):
Once you've made the above image, think about how to center the grid.
Hints and tips:
- Use at least two methods: one that takes in the overall size (5-by-5), and one that draws an individual square.
- You will need to use a nested for loop to draw all the squares!
- To draw and label an individual square, you will need the
(i, j)
location in the grid, as well as the total width (in squares, not pixels) of the grid (e.g., 5 for a 5x5 grid). These values are essential for determining the location of the square on the canvas.
- My squares have a width of 50 pixels. The gap between each square is 10 pixels.
- GLabel tips:
- To make a GLabel with numerical text, use
new GLabel(x + "")
, where x
is an integer or double.
- The location of a GLabel object is the bottom-left of the label.
- I used "Helvetica-30" as the font size for my labels.
- Start by positioning the label in the top left of each square.
- Once you are confident, first center the GLabel horizontally in each square. Then center the GLabel horizontally. Recall that the height of a GLabel can be gotten using
label.getAscent()
.