2012-03-20 5 views
0

다음 코드는 Red Black Tree Java 구현에서 가져온 것입니다. 이것은 gui 부분입니다. 나는 자바 그래픽에 정말 나쁜, 그리고 난 당신이 볼 수 있듯이자바 GUI. 직사각형 대신 타원형

public void paintComponent(final Graphics g) { 
     super.paintComponent(g); 

     if (tree == null) { 
      return; 
     } 

     tree.traverseInorder(new Inter.Visitor() { 
      private int x = gridwidth; 
      public void visit(LBTN node) { 
       coordinates.put(node, new Point(x, gridheight * (depth(node)+1))); 
       x += gridwidth; 
      } 
     }); 

     tree.traversePostorder(new Inter.Visitor() { 
      public void visit(LBTN node) { 
       String data = node.getinfo().toString(); 
       Point center = (Point)coordinates.get(node); 
       if (node.getParent() != null) { 
        Point parentPoint = (Point)coordinates.get(node.getParent()); 
        g.setColor(Color.black); 
        g.drawLine(center.x, center.y, parentPoint.x, parentPoint.y); 
       } 
       FontMetrics fm = g.getFontMetrics(); 
       Rectangle r = fm.getStringBounds(data, g).getBounds(); 

       r.setLocation(center.x - r.width/2, center.y - r.height/2); 
       Color color = getNodeColor(node); 
       Color textColor = 
        (color.getRed() + color.getBlue() + color.getGreen() < 382) 
        ? Color.white 
        : Color.black; 
       g.setColor(color); 
       g.fillRect(r.x - 2 , r.y - 2, r.width + 4, r.height + 4); 
       g.setColor(textColor); 
       g.drawString(data, r.x, r.y + r.height); 
      } 
     }); 
    } 

가 바닥 코드 부분에서, 사각형은 레드 블랙 트리 노드에 그려되고있다 ..이 문제에 관한 도움을 얻을 기대했다. 나는 프로그램을 기능적으로 만들면서 타원형으로 바꾸고 싶습니다. 즉, 여전히 사각형에 지정된 값을 사용하고 싶습니다. 동일한 값을 기반으로 타원을 만들 수 있기를 원합니다.

어떤 도움을 주시면 감사하겠습니다. 감사합니다

+2

g.fillOval (r.x - 2 r.y - 2 r.width + 4 + 4 r.height에); 타원을 채워야합니다. 치수를 늘려야 할 수도 있습니다. – montardon

+1

@montardon 나는 그것이 좋은 대답을 할 것이라고 생각한다. '어쩌면'이라는 단어에는 공백이 없습니다. –

답변