線條不重疊我在自制的gridPanel上繪製JFrame中的線條。當他們應該Java Swing
問題是,我畫了兩點之間的界限。當我有一條線在點1和點2之間以及點2和點3之間的一條線時,這些線應該連接。然而,這不是,這種情況之間存在一個小小的差距,不知道爲什麼。但它不會畫到指定點結束。 (開始點是正確的。)
這裏是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調用調用GridPane的方法的方法。 相關的代碼在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();
}
該方法創建的壁,並把它在JFrame中。 牆上的相關代碼:
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);
}
}
所以,我要去哪裏錯了?真/假是確定牆是黑色還是黃色,沒什麼可擔心的。
爲了更好地幫助越早,張貼[SSCCE(http://sscce.org/)。 –