Graphics Objects and Pixels

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.
Historical topic: Java appletsModern web browsers do not run Java applets. The Applet API was deprecated for removal before Java 25 and was removed in Java 26. Treat applet examples on this page as historical object-oriented/GUI examples; for new desktop interfaces use Swing where appropriate or JavaFX/OpenJFX.

Answer:

It sets to white the background color of the rectangle controlled by the applet. (Some browsers will not change the color, depending on how they have been set up.)

Graphics Objects and Pixels

The background color of the applet is the color of the drawing area. It can be set to a pre-defined color or to a custom color. This will be discussed further in a few pages.

The drawing area is represented by a Graphics object. A Graphics object has methods for doing graphical things. It is somewhat like the "paint" application of Windows operating systems. It has methods to draw lines, draw circles, write text, and so on. The statement:

gr.drawString("Loveliest of trees, the cherry now", 25, 30);

calls the method drawString() of the Graphics object. It prints the string at the location given by the last two parameters, 25 pixels from the left edge of the applet's area and 30 pixels from the top edge.

drawString( String str, int  x, int  y)
  • str — a String to place into the drawing area
  • x — horizontal distance in pixels from the left edge
  • y — vertical distance in pixels from the top edge
grid

The x and y parameters tell where to place the string within the applet's area. This is like graph paper, except the (0,0) location is the upper left corner (of the applet's drawing area, not of the full screen). Increasing y values move down the area. The location (x, y) is where to place the lower left part of the first character of the string.

Distance is measured in pixels. A pixel is one of the graph paper squares that the video screen has been (conceptually) divided into. If your monitor is set to a resolution of 800 by 600, then the whole screen is divided into 800 horizontal squares and 600 vertical squares. An applet will usually cover only part of that area, perhaps an area 300 horizontal by 150 vertical. It is the responsibility of the graphics card and its driver to implement this idea of graph paper on the actual electronics of you computer system.

A pixel is not one of the little, glowing dots of phosphor on the monitor screen. Depending on the resolution you are using, an image pixel may correspond to several of these dots or a fraction of one dot.

QUESTION 4:

Here is part of the applet:

gr.drawString("Loveliest of trees, the cherry now", 25, 30);
gr.drawString("Is hung with bloom along the bough", , );

Suggest a location for the second line (assume that characters are 10 pixels high.)