Start on the Applet

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:

You should get something like this:

House        --- ( 65, 100) width=110, height=110
Door         --- (120, 165) width= 25, height= 40
Left Window  --- ( 90, 115) width= 30, height= 30
Right Window --- (130, 115) width= 30, height= 30
Tree Trunk   --- (255, 100) width= 10, height=100

Start on the Applet

With this information, you can start coding the applet. The code starts by defining variables that contain the above coordinates. Give the variables names that show their use. For example houseX is the X coordinate of the left edge of the house. houseW is the width of the house.

import javax.swing.JApplet;
import java.awt.*;

// assume that the drawing area is 350 by 250
public class HouseRectangles extends JApplet
{
  final int width  = 350, height = 250;
  final int houseX =  65, houseY = 100, houseW = 110, houseH = 110 ;
  final int doorX  = 120, doorY  = 165, doorW  =  25, doorH  =  40 ;
  final int lWindX =  90, lWindY = 115, lWindW =  30, lWindH =  30 ;
  final int rWindX = 130, rWindY = 115, rWindW =  30, rWindH =  30 ;
  final int trunkX = , trunkY = , trunkW = , trunkH =  ;
  
  public void paint ( Graphics gr )
  { 

     gr.drawRect( houseX , houseY , houseW, houseH); // house
     gr.drawRect( doorX  , doorY  , doorW , doorH ); // door
     gr.drawRect( lWindX , lWindY , lWindW, lWindH); // lwind
     gr.drawRect( rWindX , rWindY , rWindW, rWindH); // rwind
     gr.drawRect(  ,  ,  , ); // trunk
  }
}

You may think that it would be just as easy to put the numbers directly into the drawRect() method. But if you need to make adjustments later on, it is very useful to have names for the various values you wish to change.

QUESTION 4:

Fill in the blanks.