-2

I have this code which uses an open source API (Sweethome3D). What I want to do is to check whether the Room is Parallelogram or Square and then call a method and change the values of the walls and the Room. But if I try to do this, it gives me error. Do you have any idea how I can do that?

@Override
public Home createHome() {
    Home home = super.createHome();

    if (Shape_of_Room.equals("Parallelogram")) {
        float[][] points = { { 0f, 0f }, { 0f, 400f }, { 625f, 400f },
                { 625f, 400f }, { 625f, 0f }, { 0f, 0f } };
        Room Parallelogram_Room = new Room(points);
        Parallelogram_Room.setAreaVisible(true);

        // Add new Room to Home
        home.addRoom(Parallelogram_Room);
        // Check for Ceiling and Floor Display
        if (Floor_Display.equals("true")) {
            Parallelogram_Room.setFloorVisible(true);
        }

        else if (Floor_Display.equals("false")) {
            Parallelogram_Room.setFloorVisible(false);
        }

        if (Ceiling_Display.equals("true")) {
            Parallelogram_Room.setCeilingVisible(true);
        }

        else if (Ceiling_Display.equals("false")) {
            Parallelogram_Room.setCeilingVisible(false);
        }

        Wall Left_Wall = new Wall(0, 0, 0, 400, (float) 7.62);
        Wall North_Wall = new Wall(0, 400, 625, 400, (float) 7.62);
        Wall Right_Wall = new Wall(625, 400, 625, 0, (float) 7.62);
        Wall South_Wall = new Wall(625, 0, 0, 0, (float) 7.62);

        Left_Wall.setWallAtEnd(North_Wall);
        North_Wall.setWallAtEnd(Right_Wall);
        Right_Wall.setWallAtEnd(South_Wall);
        South_Wall.setWallAtEnd(Left_Wall);
        // Check the color of the Walls
        // Values set from site
        // http://cloford.com/resources/colours/500col.htm
        // and by the calculator
        // http://www.shodor.org/stella2java/rgbint.html

        int k;
        String[] Color = { "Grey", "Red", "Green", "Blue", "Yellow",
                "White" };
        int[] Color_Number = { 13882323, 16764108, 7456369, 13421823,
                11394815, 16777215 };

        for (k = 0; k < 6; k++) {
            if (Left_Wall_Color.equals(Color[k])) {
                Left_Wall.setLeftSideColor(Color_Number[k]);
            }

            if (North_Wall_Color.equals(Color[k])) {
                North_Wall.setLeftSideColor(Color_Number[k]);
            }

            if (Right_Wall_Color.equals(Color[k])) {
                Right_Wall.setLeftSideColor(Color_Number[k]);
            }

            if (South_Wall_Color.equals(Color[k])) {
                South_Wall.setLeftSideColor(Color_Number[k]);
            }
        }

        // Check if the walls are Matt or Shiny

        if (Shiny_Matt.equals("Matt")) {
            Left_Wall.setLeftSideShininess(0);
            North_Wall.setLeftSideShininess(0);
            Right_Wall.setLeftSideShininess(0);
            South_Wall.setLeftSideShininess(0);
        }
        else if (Shiny_Matt.equals("Shiny")) {

            Left_Wall.setLeftSideShininess(1);
            North_Wall.setLeftSideShininess(1);
            Right_Wall.setLeftSideShininess(1);
            South_Wall.setLeftSideShininess(1);
        }

        home.addWall(Left_Wall);
        home.addWall(North_Wall);
        home.addWall(Right_Wall);
        home.addWall(South_Wall);

    }

    // Modify home as you wish here
    System.out.println("method called");
    return home;
}
Mitsaki
  • 11
  • 2
  • 6
  • The error is that eclipse doesn't recognize the objects of Room and Home that I have created when I try to use it in another method.(Or even if I use them outside of If-statement) – Mitsaki Aug 23 '11 at 14:05
  • Can you post also the code that is not working (the main??)? The method code is not enough to let us help you. – Fabrizio D'Ammassa Aug 23 '11 at 14:10
  • 2
    You should use the Java naming conventions: variables and method names are in lowerCamelCase. When something starts with an upper case letter, Java developers will think you are calling a static member. – Sean Patrick Floyd Aug 23 '11 at 14:10
  • Actually the main is not the problem, because I call this method and inside this method, I want to call another one. – Mitsaki Aug 23 '11 at 14:16

3 Answers3

1

The variables inside a method are not within the scope of your other methods. You have to explicitly pass them as parameters. It's unclear what you want to do with the Home and Room objects or which method you would like them to be available in.

The other alternative to passing these objects as method parameters is to store them as class fields; create a private Home home and private Room room and assign those fields in your createHome method. You can then refer to home and room inside other methods in the same class. This approach really depends on what you need those variables for, though -- it might not represent a solid object-oriented design.

Some more information on what you want to accomplish would help us to provide better suggestions.

Thorn G
  • 12,620
  • 2
  • 44
  • 56
0

You initialized your variable Home inside of the method and Room inside of the if-statement. As soon as Java exits the if-statement or the method, the variables are forgotten instantly. You need to declare them in the root of your class outside of any method:

public class YourClass {

    private Home home;
    private Room room;

    public void createHome() {
        home = super.createHome();
        // ...
    }
}
Pit
  • 3,606
  • 1
  • 25
  • 34
0

That doesn't look like valid java code. You are probably getting compiler errors. For every error you get google it and find out exactly what that error means and understand why your code is invalid. That way you'll learn how to write Java code.

You don't show where variables like Shape_of_Room are declared, you're just trying to assign to them. Those should be static variables on the the class level to be able to access them from within the static main method.

Sarel Botha
  • 12,419
  • 7
  • 54
  • 59