'Java Jframe: rectangles on the right coordinates

I'm stuck on a little graphic issue here. I'm trying to create corridors(rectangles) between rooms(rectangles). The method drawCorridors(Graphics g)has the assignment to create these corridors, and to do so it uses coordinates from the two rectangles that sed corridor should connect between.

The ArrayList roomList.size()is located in another class and holds all of the rooms(which are objects), don't mind it tho, it's simply a normal ArrayList.

The rooms have their own coordinates(X,Y), Width, Height and Color

Now, you might be wondering; what does "room.northWall..." mean? Well, within each roomobject there is four methods that control weather the specific room has a corridor on either the north, south, east or west side. So calling "room.northWall.." for example, returns the room that "room" in "room.northWall" is connected to by a corridor through the north side. So for example if I call "room5.northWall.Height" and room5 is set to be connected with room4 by the north wall, I would get "room4.Height" instead.

public void connectNorthTo(Room r) {
        northWall = r;
    } 

public Room(int dx, int dy, Color color) {
        this.northWall = null;
        this.eastWall = null;
        this.westWall = null;
        this.southWall = null;
        this.Color = color;
        this.Height = dy;
        this.Width = dx;
        this.Y = 0;
        this.X = 0;
        this.bottomY = this.Y + dy;
        this.RightX = this.X + dx;

    }
public void paintComponent(Graphics g) {
            super.paintComponent(g);
            drawRooms(g);
            drawCorridors(g);
        }
private void drawCorridors(Graphics g) {
            for (int i = 0; i < lv.roomList.size(); i++) {

                Room room = lv.roomList.get(i);
                g.setColor(room.Color);

                if (room.northWall != null) {

                    g.drawRect((room.RightX - (room.Width / 2)), room.northWall.bottomY, 5,
                            (room.Y - room.northWall.bottomY));

                }
                if (room.southWall != null) {

                    g.drawRect((room.RightX - (room.Width / 2)), room.bottomY, 5,
                            (room.southWall.Y - room.bottomY));

                }
                if (room.eastWall != null) {

                    g.drawRect(room.RightX, (room.bottomY - (room.Height / 2)), 
                            (room.eastWall.X - room.RightX), 5);

                }
                if (room.westWall != null) {

                    g.drawRect(room.westWall.RightX, (room.bottomY - (room.Height / 2)),
                            (room.X - room.westWall.RightX), 5);

        }
    }
}

The rectangles work perfectly fine, and are at the intended coordinates and dimensions.

The problem is that I get this output: (minus the set.fillrect which I removed to minimize code)

enter image description here

Which is of course not what I intended, At All.

I tried it with only two rooms and putting the X to 10 and the Y to 20 for the corridor to see how it would react. And... this happened... (this was with the left room connected to the right room with a corridor on the east side.)

enter image description here

I put a print to get the coordinates and dimensions of the placed corridors(continuing the test above with only two rooms). And I got this output.

corridor has been placed at (100,50) and has the width: 530 and height: 5
corridor has been placed at (100,50) and has the width: 530 and height: 5
corridor has been placed at (100,50) and has the width: 530 and height: 5
corridor has been placed at (100,50) and has the width: 530 and height: 5

which doesn't make much sense. For one, there are four of them, and second of they're at (100, 50) which they shouldn't. And the dimensions are wrong, shouldn't have a 530 width, it should be 30.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source