go to previous page   go to home page   go to next page

Answer:

You should get something like this:

ObjectXYWidthHeight
House 50 100 150 100
Door 120 150 20 50
Left Window 75 140 25 40
Right Window 160 140 25 40
Tree Trunk 260 65 10 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.

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.


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 =  50, houseY = 100, houseW = 150, houseH = 100 ;
  final int doorX  = 120, doorY  = 150, doorW  =  20, doorH  =  50 ;
  final int lWindX =  75, lWindY = 140, lWindW =  25, lWindH =  40 ;
  final int rWindX = 160, rWindY = 140, rWindW =  25, rWindH =  40 ;
  
  final int trunkX = , trunkY = , trunkW = , trunkH =  ;
  
  public void paint ( Graphics gr )
  { 
     gr.setColor( Color.white );                     // set the pen color to white
     gr.fillRect( 0, 0, 350, 250 );                  // fill the drawing area with white

     gr.setColor( Color.orange );                    // set the pen color to orange
     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
  }
}

QUESTION 4:

Fill in the blanks. (Look at the table for the values, then copy variable names into drawRect().)