package ciips.animation.tree; import java.awt.*; import ciips.animation.*; public class BasicNode {// implements DrawingObj { protected String label; protected Object weight; protected int x, y; protected int depth = -1; protected boolean highlightNode = true; protected Color nodeColor = Color.white; protected Color labelColor = Color.black; public boolean highlight = false; public Dimension node_dim; Font hugeFont, bigFont; protected int diff_y = 20; protected int diff_x = 5; /** * Create a new leaf node with label and weight as specified in the * parameters with multiple children. * @param label The label of the new node. * @param weight The weight of the new node. */ public BasicNode() { } // Constructor public BasicNode( String label, Object weight ) { this.label = label; this.weight = weight; node_dim = new Dimension (20,30); //default for the nodes } /** * Set the weight of this node. * @param weight The weight to be assigned to this node. */ public void setWeight(Object weight) { this.weight = weight; } /** * Set the label of this node. * @param label The label to be assigned to this node. */ public void setLabel(String label) { this.label = new String(label); } /** * Get the weight of this node. * @return Weight of this node. **/ public Object getData() { return weight; } /** * Get the label of this node. * @return Label of this node. */ public String getLabel() { return new String(label); } /** * Check if this node is a leaf node. * @return true if the node is a leaf node; false otherwise. */ public boolean isLeaf() { return true; } /** * Sets the x coordinate of the top-left corner of the node */ public void setX(int x) { this.x = x; } /** * Sets the y coordinate of the top-left corner of the node */ public void setY( int y ) { this.y = y; } /** * Get the left most position of the node. * @return The x coordinates of the top-left corner of the node */ public int getX() { return x; } /** * Get the top most position of the node. * @return The y coordinates of the top-left corner of the node */ public int getY() { return y; } /** * Get the depth of his node. * @return The depth of this node in a tree. */ public int getDepth() { return depth; } /** Start at a node and set the positions for the sub-tree elements * dx is the distance between the first and the last child nodes in x direction * dy is the distance between the parent and the child node in y direction **/ public void setPosition( int x, int y, int dx, int dy) { this.x = x; this.y = y; } /** * Sets the depth of this node corresponding to the root node of the tree. * @param depth Depth of the node. */ public void setDepth(int depth) { this.depth = depth; } /** * Move the node and all its branches based on the parameters. * @param x The horizontal destination position of this node. * @param y The vertical destination position of this node. */ public void move(int x, int y) { this.x = x; this.y = y; } public void setXY( int x, int y){ this.x = x; this.y = y; } /** * Move the tree starting with node dx pixels to the right and dy * pixels down. * @param node The root node of the tree to be moved. * @param dx The change in x direction. * @param dy The change in y direction. */ public void displace(int dx, int dy) { x += dx; y += dy; } /** * Assign some font instances to reduce initialization over during * redraw. */ public void initFonts(Font hugeFont, Font bigFont) { this.hugeFont = hugeFont; this.bigFont = bigFont; } /** * Set the color of the node. * @param nodeColor new color of the node. */ public void initColors(Color nodeColor) { this.nodeColor = nodeColor; } public void setColour( Color x ) { nodeColor = x; } public void setLabelColour( Color x ) { labelColor = x; } /** * Set the highlight condition of a node */ public void setHighlight(){ highlight = true; } public void unHighlight(){ highlight = false; } public boolean getHighlight(){ return highlight; } /** * This method draws the node on the corresponding graphical context * normally passed in from the drawing panel. */ public void draw(Graphics g) { System.out.println("BasicNode:draw " + getData() ); if (highlight) g.setColor(Color.black); else g.setColor( nodeColor ); g.fillRect(x, y, node_dim.width, node_dim.height); g.setColor(Color.black); g.drawRect(x, y, node_dim.width, node_dim.height); if ( label != null ) { if (getLabel().length() > 0) { g.setColor( labelColor ); g.setFont( hugeFont ); g.drawString(getLabel(), x+diff_x, y+diff_y); } } g.setColor( Color.white ); g.setFont( bigFont ); g.drawString(""+getData(), x+diff_x, y+diff_y); System.out.println("BasicNode:draw " + getData() + " exit" ); } }