2012-11-03 51 views
0

线条不重叠我在自制的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); 
} 

}

所以,我要去哪里错了?真/假是确定墙是黑色还是黄色,没什么可担心的。

+2

为了更好地帮助越早,张贴[SSCCE(http://sscce.org/)。 –

回答

1

您已经设置使用this.setLayout(new BorderLayout());

你那么GridPane添加到中心位置this.add(gridPane,BorderLayout.CENTER);

然后尝试和墙壁添加到使用this.add(wall, JLayeredPane.DEFAULT_LAYER);主要布局的主要布局BorderLayout ...但是主要布局是BorderLayout

这将会给你带来一些问题

已更新

您遇到的另一个问题是在Wall#paintComponent方法中。

您正在绘制从x1y1位置偏移的行,但组件已经定位在此位置。

顶部,任何组件的左上角总是为0x0

线g2.drawLine(x1, y1, x2, y2);应该多看一些像...

int x = x2 - x1; 
int y = y2 - y1; 
g2.drawLine(0, 0, x, y); 

修订

你也应该避免设置大小您的组件的某些任意值(如1000x1000),并更多地依靠您的组件提供反馈的能力......

public Dimension getPreferredSize() { 
    int width = Math.max(x1, x2) - Math.min(x1, x2); 
    int height = Math.max(y1, y2) - Math.min(y1, y2); 

    if (black) { 
     width += 8; 
     height += 8; 
    } else { 
     width += 3; 
     height += 3; 
    } 

    return new Dimension(width, height); 
} 

然后加入Wall时,您可以使用wall.setSize(wall.getPreferredSize()),而不是...

+0

不记得我为什么这么做。你会如何推荐我解决它? – Sven

+1

从代码的外观来看,期望是将墙放置在“JLayeredPane”上。我想通过使'GridPane'从'JLayeredPane'扩展或者向'GridPane'添加一个(使GridPane的布局成为'BorderLayout''''''''''''''''''''''')来添加一个方法,到它。另一种解决方案是调查形状API – MadProgrammer

+0

谢谢,我会试试看! – Sven

相关问题