/* This file is part of the algoviz@vt collection of algorithm visualizations. This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this distribution. If not, see . */ package bst; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.image.*; import java.util.ArrayList; import javax.swing.JPanel; /** * A JPanel with a graphical BST drawn on it. The BST lies underneath and this * class interacts with it, telling it to draw itself. * * @author Andrew Whitaker (aawhitak@vt.edu) * */ public class TreePanel extends JPanel { public static final int NODE_VERTICAL_DISTANCE = 100; public static int ANIMATION_TIME = 15; public BST bst; private static int NODE_SPACING = 30; private int lowestLevel = -1; private boolean repaintBST, recalculateBST; private int Y_ANIMATION_INCREMENT = 1; private int X_ANIMATION_INCREMENT = 1; private boolean paint=false; private TreeApplet applet; /** * Creates a new TreePanel, with a parent TreeApplet. * @param a */ public TreePanel(TreeApplet a) { super(); this.applet = a; repaintBST = recalculateBST = true; bst = new BST(this); //avl.insert(1, new GraphicalNode(1)); //avl.insert(2, new GraphicalNode(2)); //avl.insert(3, new GraphicalNode(3)); //avl.insert(6, new GraphicalNode(6)); //avl.insert(5, new GraphicalNode(5)); } /** * Insert an integer to the tree. * * @param i */ public void insert(int i) { GraphicalNode insertMe = new GraphicalNode(i); //insertMe.setHidden(true); bst.insert(i, insertMe); } public void find(int i) { bst.find(i); } public void delete(int i){ bst.remove(i); } public void paintComponent(Graphics g) { if(!paint){ _AntiAliasOn(g); super.paintComponent(g); bst.drawPostOrder(g, recalculateBST, repaintBST); } else{ super.paintComponent(g); } } /** * @return The BST's state list for the last operation. */ public ArrayList getStateList() { return bst.getStateList(); } /*public Timer getTimer() { return timer; }*/ /** * Draws a line between two nodes. * * @param x1 * @param y1 * @param x2 * @param y2 * @param g */ public void drawLine(int x1, int y1, int x2, int y2, Graphics g) { g.drawLine(getPhysicalX(x1, y1) + 15, y1 * 100 + 15, getPhysicalX(x2, y2) + 15, y2 * 100); } /** * Gets the parent abstract position of an x value. * * @param x * @return */ public int getParentX(int x) { return x / 2; } /** * Get the physical X position of a node based on its abstract x and y. * * @param x * @param y * @return */ public int getPhysicalX(int x, int y) { int w = getWidth(); int base = w / (y + 2); // int parent = w / (y + 1); // int spacing = NODE_SPACING * ((lowestLevel - y) + 1); if (y == lowestLevel + 1) { if (x % 2 != 1) { return base + ((x * NODE_SPACING)) / 2; } else { return getPhysicalX(x - 1, y) + NODE_SPACING; } } else { int pos = (getPhysicalX(x * 2, y + 1) + getPhysicalX(x * 2 + 1, y + 1)) / 2; return pos; } } public void setRepaint(boolean r) { repaintBST = r; } public void setRecalculateTree(boolean r) { recalculateBST = r; } public void setLevels(int levels) { lowestLevel = levels; } public static void _AntiAliasOn(Graphics g) { if(g instanceof Graphics2D) ((Graphics2D)g).setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); else System.out.println("Anti Aliasing disabled!"); } public void enableButtons(boolean doIt) { applet.enableButtons(doIt); } public void clear(){ bst.clear(); paint=true; this.paintComponent(this.getGraphics()); paint=false; } }