我正在制作二叉树的图形表示,但是我遇到了问题

时间:2014-12-15 17:48:28

标签: java swing binary-search-tree

我是Java中2D图形的新手。这是我的第一个实现:

private JFrame finestra = new JFrame();
public void showWindow(){
    finestra.setSize(windowHeight, windowWidth);
    finestra.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    compRicorsiva(radice, ((((this.larghezzaAlbero()/2)*windowWidth))/larghezzaAlbero())-25, 10); //Calcolo metà della finestra, -25 sarebbe metà della larghezza di un rettangolo
    finestra.setVisible(true);

}

private void compRicorsiva(Nodo nodo, int xPos, int yPos){
    if(nodo != null){

        yPos = yPos + 10;
        nodo.sh=new DisegnaNodo(xPos, yPos);
        finestra.add(nodo.sh);
        compRicorsiva(nodo.sx, (xPos-(xPos/2)), yPos);

        //yPos = yPos - 10; //Però no perchè sennè viene asimmetrico

        compRicorsiva(nodo.dx, (xPos+(xPos/2)), yPos);

    }
}

这是在我的树类中(对不起,我是意大利语,引用是后果)。

然后DisegnaNodo(xPos,yPos)使用graphics2D方法,这里是:

package alberovero;
import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;

public class DisegnaNodo extends JComponent{
  private static double height = 25;
  private static double width = 50;

  private double xPos;
  private double yPos;

  //costruttore
  public DisegnaNodo(int posX, int posY){
    this.xPos = (double)posX;
    this.yPos = (double)posY;

  }

  public void setHeight(int h){
    this.height = (double)h;
    return;
  }

  public void setWidth(int w){
    this.width = (double)w;
    return;
  }

  @Override
  public void paint(Graphics g){

    Graphics2D graph2 = (Graphics2D)g;
    graph2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

    Shape rettangoloArrotondato = new RoundRectangle2D.Double(xPos, yPos, width, height, 45, 45);

    graph2.draw(rettangoloArrotondato);
  }
}

然而结果有点奇怪,当我进行调试时它打印出一些矩形,但是如果我只是运行它只绘制一个矩形,好像其他一切都被删除了..

我不明白我的错误..对不起,如果这个问题有点不足,我会尽力在一小时内提出细节......这是我的第一个问题,所以我我不知道我是否正确发布..

0 个答案:

没有答案
相关问题