This is my code:
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JComponent;
/*
* Lab 4.3a: This component draws 2 house shapes.
*/
public class Ch3_CarHouseComponent extends JComponent {
public Ch3_CarHouseComponent(int x, int y) {
}
public void paintComponent(Graphics g) {
//Recover Graphic2D
Graphics2D g2 = (Graphics2D) g;
//create house 1 on top-left corner
Ch3_House_ house1 = new Ch3_House(0,0);
//subtract dimensions of car1 to find position of car2
int x = getWidth() - 60;
int y = getHeight() - 60;
//create house2
Ch3_House house2 = new Ch3_House(x,y);
//create 2 cars
Ch3_CarHouseComponent car1 = new Ch3_CarHouseComponent (300,400);
int x2 = getWidth() - 60;
int y2 = getHeight() - 30;
Ch3_CarHouseComponent car2 = new Ch3_CarHouseComponent(x2,y2);
//draw components
house1.draw(g2);
house2.draw(g2);
car1.draw(g2);
car2.draw(g2);
}
private void draw(Graphics2D g2) {
// TODO Auto-generated method stub
}
}
CarHouse class
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
/*
* Lab 4.3b: A house shape that can be positioned anywhere on the screen
*/
public class Ch3_CarHouse {
private int xLeft;
private int yTop;
/**
* Constructs a car with a given top left corner.
* @param x the x-coordinate of the top-left corner
* @param y the y-coordinate of the top-left corner
*/
public Ch3_CarHouse(int x, int y) {
xLeft = x;
yTop = y;
}
/** Draws the house.
* @param g2 the graphics context
*/
public void draw(Graphics2D g2) {
Rectangle body = new Rectangle(xLeft, yTop + 10, 60, 60);
//declares the POINTS for each part of house roof
//The bottom of the front roof
Point2D.Double r1 = new Point2D.Double(xLeft, yTop + 10);
//the middle and top of house roof
Point2D.Double r2 = new Point2D.Double(xLeft + 30, yTop);
//the end of the house roof
Point2D.Double r3 = new Point2D.Double(xLeft + 60, yTop +10);
//connects the points with a LINE
Line2D.Double frontRoof = new Line2D.Double(r1,r2);
Line2D.Double endRoof = new Line2D.Double(r2, r3);
//create rectangle shaped windows and a door
Rectangle window = new Rectangle(xLeft + 10, yTop + 20, 10, 10);
Rectangle window2 = new Rectangle(xLeft + 40, yTop + 20, 10, 10);
Rectangle door = new Rectangle(xLeft + 25, yTop + 45, 10, 20);
//draw the components
g2.draw(body);
g2.draw(window);
g2.draw(window2);
g2.draw(door);
g2.draw(frontRoof);
g2.draw(endRoof);
//DRAWS CAR
Rectangle carBody = new Rectangle(xLeft + 120, yTop + 10, 60, 10); //or 60 isnewad of 120
Ellipse2D.Double frontTire = new Ellipse2D.Double(xLeft + 70, yTop +20, 10, 10);
Ellipse2D.Double rearTire = new Ellipse2D.Double(xLeft + 100, yTop + 20, 10, 10);
//declares the POINTS for each part of car roof
//The bottom of the front windshield
Point2D.Double r4 = new Point2D.Double(xLeft + 10, yTop + 10);
//the front of the roof
Point2D.Double r5 = new Point2D.Double(xLeft + 20, yTop);
//the rear of the roof
Point2D.Double r6 = new Point2D.Double(xLeft + 40, yTop);
//The bottom of the rear windshield
Point2D.Double r7 = new Point2D.Double(xLeft + 50, yTop + 10);
//connects the points with a LINE
Line2D.Double frontWindshield = new Line2D.Double(r4,r5);
Line2D.Double roofTop = new Line2D.Double(r5, r6);
Line2D.Double rearWindshield = new Line2D.Double(r6, r7);
//draw the components
g2.draw(body);
g2.draw(frontTire);
g2.draw(rearTire);
g2.draw(frontWindshield);
g2.draw(roofTop);
g2.draw(rearWindshield);
}
}
And this is the viewer class:
import javax.swing.JFrame;
public class Ch3_CarHouseViewer {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setSize(300, 400);
frame.setTitle("Two cars and houses");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Ch3_CarHouseComponent Ch3_CarHouseComponent = new Ch3_CarHouseComponent(0, 0);
frame.add(Ch3_CarHouseComponent);
frame.setVisible(true);
}
}
I don't think my coordinates are correct. What can I do?
4.3 Write a component class that draws two cars and two houses, using the classes Car and House.