NOTE The completed code must pass in the following compiler
NOTE: The completed code must pass in the following compiler. Please make absolutely sure it does before posting: http://codecheck.it/codecheck/files?repo=bj4fp&problem=ch03/c03_exp_3_16
Write a program that draws a picture of a house. It could be as simple as the accompanying figure, or if you like, make it more elaborate (3-D, skyscraper, marble columns in the entryway, whatever). Implement a class House and supply a method draw(Graphics2D g2) that draws the house. Here is a sample program output: (example image found on above compiler link)
Use the following class as your main class:
Complete the following class in your solution:
Use the following class in your solution:
Solution
//House.java
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.Line2D;
/**
* This class draws a house by allowing the user to specify houses of different
* sizes.
*/
public class House {
public int x;
public int y;
/**
* Constructor to initiate the bottom left corner
*
* @param x the left corner coordinate
* @param y the bottom corner coordinate
*/
public House(int x, int y) {
this.x = x;
this.y = y;
}
/**
* Draws the house
*
* @param g2 the graphics context
*/
public void draw(Graphics2D g2) {
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2.setPaint(Color.blue);
int x1 = x, y1 = y, x2 = 200, y2 = 200;
//first draw horizontal line
g2.draw(new Line2D.Double(x1, y1, x2, y2));
//find midpoints of aboveline and sustract y cordinate by 100 or any positive integer
int mx = Math.abs((x1+x2)/2);
int my = Math.abs((y1+y2)/2);
//draw tha line from both end of above line to the (mx,my)
g2.draw(new Line2D.Double(x1, y1, mx, my-100));
g2.draw(new Line2D.Double(x2, y2, mx, my-100));
//get the width of the line
int sqrWidth = Math.abs(x1-x2);
//draw tha squares
g2.drawRect (x1,y1,sqrWidth, sqrWidth);
g2.drawRect (mx-30,y1+50,20,50);
g2.drawRect (mx+10,y1+60,20,20);
}
}

