The Java Coordinate System

The Java coordinate system is discussed in Section 1.6 of the text. Under this system, the upper left-hand corner of the window is the point (0,0). The X axis goes across the window, and the Y axis goes down the window. So the bigger the X value, the farther a point is to the right. The bigger the Y value, the farther it is down. There are no negative X or Y values in the Java coordinate system. Actually, you can use negative values, but since they're off the screen they won't show up!

  1. Save files Coords.java and Coords.html to your directory. File Coords.java contains an applet that draws a rectangle whose upper lefthand corner is at 0,0. Use the applet viewer to run this applet. Remember that you have to do it through the html file: appletviewer Coords.html.

  2. Modify the applet so that instead of 0,0 for the upper lefthand corner, you use the coordinates of the middle of the applet window. This applet is set up to be 600 pixels wide and 400 pixels high, so you can figure out where the middle is. Save, compile, and view your applet. Does the rectangle appear to be in the middle of the screen? Modify the coordinates so that it does appear to be in the middle.

  3. Now add four more rectangles to the applet, one in each corner. Each rectangle should just touch one corner of the center rectangle and should go exactly to the edges of the window.

  4. Make each rectangle be a different color. To do this, use the setColor method of the Graphics class to change the color (this is already done once). Do not change the background color once it has been set! Doing so causes the screen to flicker between colors.