JApplet with all the Rectangles

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:

All you have to do is copy the information to the blanks.

JApplet with all the Rectangles

Here is the applet with all the blanks filled in. With skillful use of the text editor, much of this filling in is easily done using "copy" and "paste".

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 = 255, trunkY = 100, trunkW =  10, trunkH = 100 ;
  
  public void paint ( Graphics gr )
  { 
     gr.setColor( Color.orange );    // there is no Color brown
     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.fillRect( trunkX , trunkY , trunkW, trunkH); // trunk
  }
}

Here is the beautiful picture that it produces:

Embedded Java applet removed: modern browsers do not support Java applets. The surrounding source code is retained for historical study.
If you see this, your browser is not running Java.

The tree trunk is a solid rectangle because the fillRect() method was used. This method is just like drawRect() except that it fills the figure with the current color.

The door is not really where we want it. We could go back to the graph paper and carefully read off its coordinates, or calculate its coordinates from those of the house.

QUESTION 5:

You want the door horizontally centered.

  • In terms of houseX, and houseW what is the X value of the center of the house?
  • In terms of the previous answer, and the width of the door, doorW, what is the X value of the left edge of the door, doorX ?