2016-07-04 7 views
0

JPanel을 확장하는 MapPanel 클래스가 있습니다. 이 그리드에 몇 가지 객체를 표시하고 일부 객체를 선으로 연결하고자합니다. 그래서이 선을 MapPanel.paintComponent에 그려 넣으려고하는데 아무 선도 표시되지 않았습니다. System.out.println()에 의해 라인 매개 변수 (x1, y1, x2, y2)를 확인하면 올바른 것입니다 (0 < param < 600의 종류, 패널에 완벽하게 맞는 측정 값). 또한 고정 된 매개 변수를 사용하여 한 줄만 그리려고하지만 동일한 문제가 있습니다.DrawLine over GridBagLayout는 아무것도 표시하지 않습니다.

public class NewMapPanel extends JPanel { 

    private static final long serialVersionUID = 1L; 

    private GameMap gameMap; 
    private Grid grid; 
    private JPanel contentPanel; 

    public NewMapPanel() { 
     this.setBackground(Color.white); 
    } 

    public void updateMap(GameMap gameMap) { 
     this.gameMap = gameMap; 

     ...load the object into my custom object grid ... 

     contentPanel = new JPanel(new GridBagLayout()); 
     contentPanel.setBackground(Color.GREEN); 
     GridBagConstraints c = new GridBagConstraints(); 

     // draw the grid 
     for (City city : gameMap.getCities().values()) { 
      CityPanel cityPanel = new CityPanel(city, grid); 

      c = new GridBagConstraints(); 
      c.gridx = grid.getColumn(city); 
      c.gridy = grid.getRow(city); 
      c.gridwidth = 1; 
      c.gridheight = 1; 
      c.fill = GridBagConstraints.BOTH; 
      c.anchor = GridBagConstraints.CENTER; 

      contentPanel.add(cityPanel, c); 
     } 

     add(contentPanel, BorderLayout.CENTER); 
    } 


    @Override 
    public void paintComponent(Graphics g) { 
     super.paintComponent(g); 

     contentPanel.setSize(super.getWidth(), super.getHeight()); 
     grid.setSize(super.getWidth(), super.getHeight()); //to get the right parameters to be used into drawLine 


     for (City city1 : gameMap.getCities().values()) { 
      int x1 = grid.getBarycenterX(city1); 
      int y1 = grid.getBarycenterY(city1); 
      for (City city2 : city1.getAdjacentCities()) { 
       int x2 = grid.getBarycenterX(city2); 
       int y2 = grid.getBarycenterY(city2); 

       System.out.println("(" + x1 + "," + y1 + ") -> (" + x2 + "," + y2 + ")"); 
       g.drawLine(x1, y1, x2, y2); 

      } 
     } 


    } 

} 
+0

우리는 City라는 클래스가 없습니다. 복사하여 붙여 넣을 수있는 [mcve]를 게시하십시오. 그러면 컴파일하고 문제점을 표시하십시오. – Frakcool

+0

['getPreferredSize()'] (http : /stackoverflow.com/q/7229226/230513) 및 초기 형상을 설정하기위한 둘러싸는 창'pack()'. – trashgod

+0

어디에서해야합니까? 그리고 무엇에? – user1315621

답변

0

색상을 설정하려고 했습니까? g.setColor (Color.black);

+0

작동하지 않습니다. – user1315621