-1

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.

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
jowjowe
  • 1
  • 1
  • You can try some [debugging techniques](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). – Joe C Oct 03 '16 at 05:27

1 Answers1

2

Several problems become apparent when running your code; see the modifications suggested below:

image

  • Invoke the parent class's paintComponent() to avoid "visual artifacts." Use the coordinates passed to the constructor in your implementation.

    private static class Ch3_CarHouseComponent extends JComponent {
        final int x;
        final int y;
    
        public Ch3_CarHouseComponent(int x, int y) {
            this.x = x;
            this.y = y;
        }
    
        @Override
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            Ch3_CarHouse ch1 = new Ch3_CarHouse(x, y);
            Ch3_CarHouse ch2 = new Ch3_CarHouse(x, y + 100);
            ch1.draw((Graphics2D) g);
            ch2.draw((Graphics2D) g);
        }
    
        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }
    }
    
  • Override getPreferredSize() then pack() the enclosing frame to establish the initial size.

  • Ch3_CarHouse still needs work, but you can now see the shapes to debug the coordinates. In particular, carBody is never used; the following change is pictured above.

    //draw the components
    g2.draw(carBody); // <--
    g2.draw(frontTire);
    …
    
  • Construct and manipulate Swing GUI objects only on the event dispatch thread.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045