mardi 25 avril 2017

java program with unknown loop

I am making a program for school and I seem to have created a loop somewhere and I can't find the source. The program is supposed to prompt the user once for the color of the body, door, windows, and trim, and then print all three houses the same color. If someone could point out what I'm doing wrong, that would be great.

package programs;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;

import javax.swing.JOptionPane;

public class House
{
    public House(double x, double y, double width, double height)
    {
    this.x = x;
    this.y = y;
    xUnit = width / 12;
    yUnit = height / 8.5;
    }

    public void draw(Graphics2D g2)
    {
        // plot the points
        Point2D.Double p1 = new Point2D.Double(x, y - 5.5 * yUnit);
        Point2D.Double p2 = new Point2D.Double(x + 6 * xUnit, y - 8.5 * 
yUnit);
        Point2D.Double p3 = new Point2D.Double(x + 12 * xUnit, y - 5.5 * 
yUnit);

        // draw lines from the points
        Line2D.Double leftRoof = new Line2D.Double(p1, p2);
        Line2D.Double rightRoof = new Line2D.Double(p2, p3);    

        // construct rectangles to build the house
        Rectangle2D.Double body = new Rectangle2D.Double(x + 1 * xUnit, y - 
6 * yUnit, 10 * xUnit, y - 5 * yUnit);
        Rectangle2D.Double door = new Rectangle2D.Double(x + 5 * xUnit, y - 
4 * yUnit, 2 * xUnit, y - 3 * yUnit);
        Rectangle2D.Double leftWindow = new Rectangle2D.Double(x + 2.5 * 
xUnit, y - 1.5 * yUnit, 1.5 * xUnit, y - 0.5 * yUnit);
        Rectangle2D.Double rightWindow = new Rectangle2D.Double(x + 8 * 
xUnit, y - 1.5 * yUnit, 1.5 * xUnit, y - 0.5 * yUnit);

         // take user input on color of the body of the house
        String input = JOptionPane.showInputDialog("Enter what color you 
would like the body of the house to be (options: pink, blue, gray, or 
black)");

        if (input.equalsIgnoreCase("pink"))
        {
            g2.setColor(Color.pink);        // fill body of house pink
            g2.fill(body);
        }

        else if (input.equalsIgnoreCase("blue"))
        {
            g2.setColor(Color.blue);        // fill body of house blue
            g2.fill(body);
         }

        else if (input.equalsIgnoreCase("gray"))
        {
            g2.setColor(Color.gray);        // fill body of house gray
            g2.fill(body);
        }

        else if (input.equalsIgnoreCase("black"))
       {
            g2.setColor(Color.black);       // fill body of house black
            g2.fill(body);
        }

        else
        {
            System.out.println("Wrong input!");     //if user input does not 
 match, return wrong input
        }


        // take user input for color of the front door
        String frontDoor = JOptionPane.showInputDialog("Enter what color you 
would like the front door of the house to be (options: pink, blue, gray, or 
black)");

        if(frontDoor.equalsIgnoreCase("pink"))
        {
            g2.setColor(Color.pink);        // fill door of house pink
            g2.fill(door);
        }

        else if(frontDoor.equalsIgnoreCase("blue"))
        {
            g2.setColor(Color.blue);        // fill door of house blue
            g2.fill(door);
        }

        else if(frontDoor.equalsIgnoreCase("gray"))
        {
            g2.setColor(Color.gray);        // fill door of house gray
            g2.fill(door);
        }

        else if(frontDoor.equalsIgnoreCase("black"))
        {
             g2.setColor(Color.black);      // fill door of house black
             g2.fill(door);
         }

        else
        {
            System.out.println("Wrong input!");     //return wrong input if 
user doesn't enter correct color
        }


        // take user input to determine what color the windows will be
        String windows = JOptionPane.showInputDialog("Enter what color you 
would like the windows on the house to "
            + "be (options: pink, blue, gray, or black)");

        if(windows.equalsIgnoreCase("pink"))
        {
            g2.setColor(Color.pink);        //fill windows of house with 
 pink
            g2.fill(leftWindow);
            g2.setColor(Color.pink);
            g2.fill(rightWindow);
        }

        else if(windows.equalsIgnoreCase("blue"))
        {
            g2.setColor(Color.blue);        //fill windows of house with 
blue
            g2.fill(leftWindow);
            g2.setColor(Color.blue);
            g2.fill(rightWindow);
        }

        else if(windows.equalsIgnoreCase("gray"))
        {
            g2.setColor(Color.gray);        //fill windows of house with 
gray
            g2.fill(leftWindow);
            g2.setColor(Color.gray);
            g2.fill(rightWindow);
        }

        if(windows.equalsIgnoreCase("black"))
        {
            g2.setColor(Color.black);       //fill windows of house with 
black
            g2.fill(leftWindow);
            g2.setColor(Color.black);
            g2.fill(rightWindow);
        }

        // user will input the color of the trim of the house
        String trim = JOptionPane.showInputDialog("Enter what color you 
would like the trim of the house to be "
                + "(options: brown, white, black, or gray): ");

        if(trim.equalsIgnoreCase("brown"))
        {
            Color brown = new Color(139, 69, 19);       //make trim of house 
brown
            g2.setColor(brown);
            g2.draw(body);
            g2.draw(door);
            g2.draw(leftWindow);
            g2.draw(rightWindow);
            g2.draw(leftRoof);
            g2.draw(rightRoof);
        }

        else if(trim.equalsIgnoreCase("white"))
        {
            g2.setColor(Color.white);       //make trim of house white
            g2.draw(body);
            g2.draw(door);
            g2.draw(leftWindow);
            g2.draw(rightWindow);
            g2.draw(leftRoof);
            g2.draw(rightRoof);
        }

        else if(trim.equalsIgnoreCase("black"))
        {
            g2.setColor(Color.black);       //make trim of house black
            g2.draw(body);
            g2.draw(door);
            g2.draw(leftWindow);
            g2.draw(rightWindow);
            g2.draw(leftRoof);
            g2.draw(rightRoof);
        }

        else if(trim.equalsIgnoreCase("gray"))
        {
            g2.setColor(Color.gray);        //make trim of house gray
            g2.draw(body);
            g2.draw(door);
            g2.draw(leftWindow);
            g2.draw(rightWindow);
            g2.draw(leftRoof);
            g2.draw(rightRoof);
       }

    }

    // construct instance fields
    private double x;
    private double y;
    private double xUnit;
    private double yUnit;
}

Aucun commentaire:

Enregistrer un commentaire