Lines don't overlap when they should Java Swing

Posted by Sven on Stack Overflow See other posts from Stack Overflow or by Sven
Published on 2012-11-03T10:18:59Z Indexed on 2012/11/03 11:00 UTC
Read the original article Hit count: 220

Filed under:
|
|
|

I'm drawing lines in a JFrame on a self made gridPanel.

Problem is, I draw the lines between 2 points. When I have a line that is between point 1 and point 2 and a line between point 2 and point 3, the lines should connect. This however isn,t the case, there is a small gap in between, no idea why. But it isn't drawing till the end of the specified point. (start point is correct.)

Here is the code of the JFrame:

public void initialize(){
     this.setLayout(new BorderLayout());

    this.setPreferredSize(new Dimension(500, 400));
    gridPane = new GridPane();
    gridPane.setBackground(Color.WHITE);
    gridPane.setSize(this.getPreferredSize());
    gridPane.setLocation(0, 0);
    this.add(gridPane,BorderLayout.CENTER);

    //createSampleLabyrinth();
    drawWall(0,5,40,5);  //These are the 2 lines that don't connect.
    drawWall(40,5,80,5);
    this.pack();
}

drawWall calls a method that calls a method in GridPane. The relevant code in gridPane:

/**
 * Draws a wall on this pane. With the starting point being x1, y1 and its end x2,y2.
 * @param x1
 * @param y1
 * @param x2
 * @param y2
 */
public void drawWall(int x1, int y1, int x2, int y2) {
    Wall wall = new Wall(x1,y1,x2,y2, true);
    wall.drawGraphic();
    wall.setLocation(x1, y1);
    wall.setSize(10000,10000);
    this.add(wall, JLayeredPane.DEFAULT_LAYER);
    this.repaint();
}

This method creates a wall and puts it in the Jframe. The relevant code of the wall:

public class Wall extends JPanel {
private int x1;
private int x2;
private int y1;
private int y2;
private boolean black;

/**
 * x1,y1 is the start point of the wall (line) end is x2,y2
 * 
 * @param x1
 * @param y1
 * @param x2
 * @param y2
 */
public Wall(int x1, int y1, int x2, int y2, boolean black) {
    this.x1 = x1;
    this.x2 = x2;
    this.y1 = y1;
    this.y2 = y2;
    this.black = black;
    setOpaque(false);
}

private static final long serialVersionUID = 1L;

public void drawGraphic() {
    repaint();
}

public void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2 = (Graphics2D) g;
    if(black){
        g2.setColor(Color.BLACK);
        g2.setStroke(new BasicStroke(8));
    } else {
        g2.setColor(Color.YELLOW);
        g2.setStroke(new BasicStroke(3));
    }
    g2.drawLine(x1, y1, x2, y2);
}

}

So, where am I going wrong? The true/false is to determine if the wall should be black or yellow, nothing to be concerned about.

© Stack Overflow or respective owner

Related posts about java

Related posts about swing