Calculating a Coordinate.

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:

  • In terms of houseX, and houseW what is the X value of the center?
    • center of the House = houseX + houseW/2
  • 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 ?
    • doorX = center - doorW/2 = houseX + houseW/2- doorW/2

Calculating a Coordinate.

These calculations can be plugged into the declaration section of the applet:

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

// assume that the drawing area is 350 by 250
public class HouseRectangles2 extends JApplet
{
  final int width  = 350, height = 250;
  final int houseX =  65, houseY = 100, houseW = 110, houseH = 110 ;
  final int               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 ;

  final int doorX  = houseX + houseW/2 - doorW/2;
  
  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 revised 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 same sort of calculation could be done with other points in the picture. The windows could be better centered, for example. But let's move on to the tree. Look at the graph paper sketch again (click here to see it) and estimate the location of the upper left corner, the width, and the height of the rectangle that contains the tree's foliage,

QUESTION 6:

Fill in the blanks:

  • X:    
  • Y:    
  • width:    
  • height: