2014-11-12 3 views
0

이미지 레이블에 그릴 필요가있는 10 개의 다각형에 약 10 세트의 좌표계가 있습니다. 이것은 지금까지 코딩 한 것입니다 :JLabel에 여러 개의 다각형 그리기 Java Swing

하지만 저는 paintComponent를 개별적으로 호출 할 수 없으므로 JLabel을 인스턴스화하는 동안 호출하고 있는데 문제가 발생합니다. 결국 새로운 jLabel이 만들어지기 때문에 마지막 폴리곤을 이미지에 그려 넣었습니다. 누군가가 이것을 개선하여 같은 JLabel에 여러 개의 다각형을 그릴 수있는 방법을 지적 할 수 있습니까?

private void setMapImage() 
{ 
    Queries queries = new Queries(dbConnection, connection); 
    List<ArrayList<Integer>> allGeo = queries.getBuilding(); 

    for(int i = 0; i < allGeo.size(); i++) 
    { 
     int[] xPoly = queries.separateCoordinates(allGeo.get(i),0); 
     int[] yPoly = queries.separateCoordinates(allGeo.get(i),1); 
     poly = new Polygon(xPoly, yPoly, xPoly.length); 

     poly = new Polygon(xPoly, yPoly, xPoly.length); 
     background=new JLabel(new ImageIcon("image.JPG"),SwingConstants.LEFT) 
     { 
      @Override 
      protected void paintComponent(Graphics g) { 
       super.paintComponent(g); 
       g.setColor(Color.YELLOW); 
       g.drawPolygon(poly); 

      } 
      @Override 
      public Dimension getPreferredSize() { 
       return new Dimension(820, 580); 
      } 
     }; 
    } 
    background.setVerticalAlignment(SwingConstants.TOP); 
    frame.add(background); 
    background.setLayout(new FlowLayout()); 
    frame.setVisible(true); 
} 
+0

내가 특별히 JLabel의에 페인트 이유를 모르겠습니다. 유리창 사용을 ​​고려 했습니까? 살펴보고 싶은 경우 [Java 자습서] (https://docs.oracle.com/javase/tutorial/uiswing/components/rootpane.html)를 참조하십시오. – hfontanez

답변

0

당신은 JLabel의 paintComponent 메소드에있는 폴리곤을 반복해야한다고 생각합니다. 그 전에 모든 폴리곤을 목록에 추가해야합니다. 예를 들어

:

private void setMapImage() 
{ 
    Queries queries = new Queries(dbConnection, connection); 
    List<ArrayList<Integer>> allGeo = queries.getBuilding(); 
    List<Polygon> polyList = new ArrayList<Polygon>(); 
    for(int i = 0; i < allGeo.size(); i++) 
    { 
     int[] xPoly = queries.separateCoordinates(allGeo.get(i),0); 
     int[] yPoly = queries.separateCoordinates(allGeo.get(i),1); 
     poly = new Polygon(xPoly, yPoly, xPoly.length); 
     polyList.add(poly); 
    } 
    background=new JLabel(new ImageIcon("image.JPG"),SwingConstants.LEFT) 
    { 
     @Override 
     protected void paintComponent(Graphics g) { 
      super.paintComponent(g); 
      g.setColor(Color.YELLOW); 
      for(final Polygon poly : polyList){ 
       g.drawPolygon(poly); 
      } 
     } 
     @Override 
     public Dimension getPreferredSize() { 
      return new Dimension(820, 580); 
     } 
    }; 

    background.setVerticalAlignment(SwingConstants.TOP); 
    frame.add(background); 
    background.setLayout(new FlowLayout()); 
    frame.setVisible(true); 
} 
+0

예, 작동했습니다! 엄청 고마워 – user16666