/* 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 . */ /** * Represents a Graphical Node in a Binary Search Tree. In this application, GraphicalNodes are put into a regular BST and retrieved and drawn when the tree is traversed (See BST's draw methods). * @author Andrew Whitaker (aawhitak@vt.edu) * @version 4/1/2008 */ import java.awt.Color; import java.awt.Graphics; import java.awt.Graphics2D; public class GraphicalNode { /* The label on this node */ private String label; /* Colors associated with the node */ private static Color DEFAULT_NODE_COLOR = Color.white, DEFAULT_TEXT_COLOR = new Color(0, 0, 128), DEFAULT_BORDER_COLOR = new Color(0, 0, 128); private static Color SELECTED_NODE_COLOR = new Color(0, 0, 128), SELECTED_TEXT_COLOR = Color.white, SELECTED_BORDER_COLOR = Color.white; private Color NODE_COLOR, TEXT_COLOR, BORDER_COLOR; /* Node dimensions */ public static int NODE_WIDTH = 30; public static int NODE_HEIGHT = 30; /* The physical x and y positions of the node */ private int xPos, yPos; /* Boolean to tell whether or not the node should be drawn */ private boolean hide; private boolean selected = false; /* * Integers representing the abstract X and Y of this node. Abstract coordinates of the tree look like this: * (0,0) * (1,0) (1,1) * (2,0) (2,1) (2,2) (2,3) */ private int absX; private int absY; /** * Used to hide a node so that previous steps can be shown. private boolean * * Creates a graphical node with a yellow fill, black border, and black * text. */ public GraphicalNode() { NODE_COLOR = DEFAULT_NODE_COLOR; BORDER_COLOR = DEFAULT_BORDER_COLOR; TEXT_COLOR = DEFAULT_TEXT_COLOR; selected = false; hide = false; } /** * "Unselects" this node. The colors associated with the node are simply * inverted. */ public void unSelect() { NODE_COLOR = DEFAULT_NODE_COLOR; BORDER_COLOR = DEFAULT_BORDER_COLOR; TEXT_COLOR = DEFAULT_TEXT_COLOR; selected = false; } /** * "Selects" this node. The colors are changed back to the original. */ public void select() { hide = false; NODE_COLOR = SELECTED_NODE_COLOR; BORDER_COLOR = SELECTED_BORDER_COLOR; TEXT_COLOR = SELECTED_TEXT_COLOR; selected = true; } /** * Creates a graphical node with a yellow fill, black border, and black text * with the specified integer label. * * @param i * The label for this node. */ public GraphicalNode(int i) { this(); label = Integer.toString(i); } /** * Sets the x position of this node. * * @param x * The new x coordinate of the node. */ public void setXPos(int x) { xPos = x; } /** * Sets the y position of this node. * * @param y * The new y coordinate of the node. */ public void setYPos(int y) { yPos = y; } public void setHidden(boolean h) { hide = h; } /** * Draw the GraphicalNode. * @param g The graphics to draw on. */ public void draw(Graphics g) { System.out.println("Drawing " + label); System.out.println("Selected " + selected); if (!hide) { Graphics2D g2d = (Graphics2D) g; System.out.println(NODE_COLOR); g2d.setColor(NODE_COLOR); g2d.fillOval(xPos, yPos, NODE_WIDTH, NODE_HEIGHT); g2d.setColor(BORDER_COLOR); g2d.drawOval(xPos, yPos, NODE_WIDTH, NODE_HEIGHT); g2d.setColor(TEXT_COLOR); g2d.drawString(label, (float) (xPos + NODE_WIDTH / 3.2), (float) (yPos + NODE_HEIGHT / 1.7)); } } public boolean isHidden() { // TODO Auto-generated method stub return hide; } public String toString() { return "(" + label + ") at " + "(" + xPos + ", " + yPos + ")"; } /* * Setters and getters for abstract x and y. */ public int getX() { return xPos; } public int getY() { return yPos; } public int getAbstractX() { return absX; } public int getAbstractY() { return absY; } public void setAbstractX(int x) { absX = x; } public void setAbstractY(int y) { absY = y; } }