houseX
, and houseW
what is the X value of the center?
houseX
+ houseW
/2doorW
,
what is the X value of the left edge of the door, doorX
?
doorX
= center - doorW
/2 = houseX
+ houseW
/2- doorW
/2These calculations can be plugged into the final int declaration section of the applet:
import java.applet.Applet;
import java.awt.*;
// assume that the drawing area is 350 by 250
public class HouseRectangles2 extends Applet
{
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:
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 not bother. Instead, look at the graph paper sketch again (click here to see it) and estimate (X, Y), the width, and the height for the rectangle containing the tree's foliage,