There's no specific function call you can make with the ACM libraries that will change the coordinate system. Instead, you can work out the math to determine where the object should be placed in order to center it in the screen.
We can imagine the window as a rectangle whose upper-left corner is at (0, 0) and whose lower-right corner is at (getWidth()
, getHeight()
). If you want to center a box of dimensions W × H, the picture looks like this:
(0, 0) getWidth()/2 getWidth()/2
*-----------------+-----------------+
| | |
| (x, y) W/2 | W/2 |
| *----------+----------+ | getHeight() / 2
| | | | H/2 |
+------+----------+----------+------+
| | | | H/2 |
| +----------+----------+ | getHeight() / 2
| | |
| | |
+-----------------+-----------------*
(getWidth(), getHeight())
Based on this picture, can you mathematically work out what x
and y
are? You can then punch those coordinates in as the first two arguments to the GOval
constructor.
For more practice with ACM graphics math, check out this packet of problems and this follow-up.