codice:
public class BinaryTreeView extends JPanel {
// the binary tree
private BinAlbero tree = null;
// the node location of the tree
private HashMap nodeLocations = null;
// the sizes of the subtrees
private HashMap subtreeSizes = null;
// do we need to calculate locations
private boolean dirty = true;
// space between nodes
private int parent2child = 50, child2child = 20;
// helpers
private Dimension empty = new Dimension(0, 0);
private FontMetrics fm = null;
JScrollBar hbar;
private org.accada.hal.impl.sim.Tag tag;
public BinaryTreeView(BinAlbero tree, org.accada.hal.impl.sim.Tag t) {
this.tree = tree;
this.tag = t;
nodeLocations = new HashMap();
subtreeSizes = new HashMap();
//registerKeyboardAction(this, "add", KeyStroke.getKeyStroke(KeyEvent.VK_A, 0), WHEN_IN_FOCUSED_WINDOW);
JFrame f = new JFrame("Albero Binario");
hbar = new JScrollBar(JScrollBar.HORIZONTAL);
hbar.setBlockIncrement(10);
hbar.addAdjustmentListener(new AdjustmentListener() {
public void adjustmentValueChanged(AdjustmentEvent ae) {
// the value of scrollbar has changed
repaint();
}
});
// f.add(pane.add(hbar),BorderLayout.SOUTH);
f.getContentPane().add(hbar, BorderLayout.NORTH);
f.getContentPane().add(this);
// repaint();
// create and add an event handler for window closing event
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
}
});
f.setSize(1000,1000);
f.show();
}
// event handler for pressing "A"
/* public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("add")) {
String c = JOptionPane.showInputDialog("Add a new node:");
if (c != null && !"".equals(c)) {
tree.addNode(c);
dirty = true;
repaint();
}
}
}*/
// calculate node locations
private void calculateLocations() {
nodeLocations.clear();
subtreeSizes.clear();
Nodo n = tree.albero[0];
if (n != null) {
calculateSubtreeSize(n);
calculateLocation(n, Integer.MAX_VALUE, Integer.MAX_VALUE, 0);
}
}
// calculate the size of a subtree rooted at n
private Dimension calculateSubtreeSize(Nodo n) {
if (n == null) return new Dimension(0, 0);
int s = (n.getEtichetta());
System.out.println(s);
Dimension ld = calculateSubtreeSize(n.getFiglioSinistro());
Dimension rd = calculateSubtreeSize(n.getFiglioDestro());
int h = fm.getHeight() + parent2child + Math.max(ld.height, rd.height);
int w = ld.width + child2child + rd.width;
Dimension d = new Dimension(w, h);
subtreeSizes.put(n, d);
return d;
}
// calculate the location of the nodes in the subtree rooted at n
private void calculateLocation(Nodo n, int left, int right, int top) {
if (n == null) return;
Dimension ld = (Dimension) subtreeSizes.get(n.getFiglioSinistro());
if (ld == null) ld = empty;
Dimension rd = (Dimension) subtreeSizes.get(n.getFiglioDestro());
if (rd == null) rd = empty;
int center = 0;
if (right != Integer.MAX_VALUE)
center = right - rd.width - child2child/2;
else if (left != Integer.MAX_VALUE)
center = left + ld.width + child2child/2;
int width =fm.stringWidth(String.valueOf(n.getEtichetta()));
Rectangle r = new Rectangle(center - width/2 - 3, top, width + 6, fm.getHeight());
nodeLocations.put(n, r);
calculateLocation(n.getFiglioSinistro(), Integer.MAX_VALUE, center - child2child/2, top + fm.getHeight() + parent2child);
calculateLocation(n.getFiglioDestro(), center + child2child/2, Integer.MAX_VALUE, top + fm.getHeight() + parent2child);
}
// draw the tree using the pre-calculated locations
private void drawTree(Graphics2D g, Nodo n, int px, int py, int yoffs,int j,Color c) {
if (n == null) return;
g.setColor(c);
Rectangle r = (Rectangle) nodeLocations.get(n);
g.draw(r);
g.drawString(String.valueOf(n.getEtichetta()), r.x + 3, r.y + yoffs);
if (px != Integer.MAX_VALUE){
g.drawLine(px, py, r.x + r.width/2, r.y);
}
System.out.println("valore j "+j+" etichetta"+n.getEtichetta());
if( (j!=-1) && (tag!=null)){
if(tag.strada!=null){
if(tag.strada[j].equalsIgnoreCase("SX")){
if(j<tag.strada.length-1){
j++;
}
drawTree(g, n.getFiglioSinistro(), r.x + r.width/2, r.y + r.height, yoffs,j, Color.red);
}
else{
drawTree(g, n.getFiglioSinistro(), r.x + r.width/2, r.y + r.height, yoffs,-1,Color.black);
}
}
else{
drawTree(g, n.getFiglioSinistro(), r.x + r.width/2, r.y + r.height, yoffs,j,Color.black);
}
}
else{
drawTree(g, n.getFiglioSinistro(), r.x + r.width/2, r.y + r.height, yoffs,j,Color.black);
}
if( (j!=-1) && (tag!=null)){
if(tag.strada!=null){
if(tag.strada[j].equalsIgnoreCase("DX")){
if(j<tag.strada.length-1){
j++;
}
drawTree(g, n.getFiglioDestro(), r.x + r.width/2, r.y + r.height, yoffs,j,Color.red);
}
else{
j=-1;
drawTree(g, n.getFiglioDestro(), r.x + r.width/2, r.y + r.height, yoffs,j,Color.black);
}
}
else{
drawTree(g, n.getFiglioDestro(), r.x + r.width/2, r.y + r.height, yoffs,j, Color.black);
}
}
else{
drawTree(g, n.getFiglioDestro(), r.x + r.width/2, r.y + r.height, yoffs,j, Color.black);
}
}
public void paint(Graphics g) {
super.paint(g);
fm = g.getFontMetrics();
// if node locations not calculated
if (dirty) {
calculateLocations();
dirty = false;
}
Graphics2D g2d = (Graphics2D) g;
g2d.translate(getWidth() / 2, parent2child);
drawTree(g2d, tree.albero[0], Integer.MAX_VALUE, Integer.MAX_VALUE, fm.getLeading() + fm.getAscent(),0,Color.black);
fm = null;
}
}
aiutatemi grazie a tutti