Slightly New Example

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:

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

You want to start the second line at the same distance from the left edge as the first line, 25. If characters are 10 pixels high, you need to increase y from 30 to something greater than 40 (since there should be some space between lines.)

Slightly New Example

Say that the drawing area is 300 pixels wide and 150 pixels high. Here is a partly completed applet that writes the word "Hello" roughly in the center of this area.

// Put "Hello" at the center of a 300 by 150 drawing area
//
import javax.swing.JApplet;
import java.awt.*;

public class AnotherHello extends JApplet 
{
  public void paint ( Graphics gr )
  { 
    setBackground( Color.lightGray );
    gr.drawString("Hello", , 75 );
   }
}

The background color of the drawing area is set to light gray.

QUESTION 5:

Calculate the X value to fill the blank so that the lower left corner of the 'H' in "Hello" is in the center of the drawing area.