线条不重叠我在自制的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/)。 –