/** * Stuff that all nodes and branches in the * scene tree know. **/ abstract public class DrawableNode { /** * The next branch/node/whatever to process **/ public DrawableNode next; /** * Constructor for DrawableNode just sets * next to null **/ public DrawableNode(){ next = null; } /** * Methods to set and get next elements * @param nextOne next element in list **/ public void setNext(DrawableNode nextOne){ this.next = nextOne; } public DrawableNode getNext(){ return this.next; } /** * Use the given turtle to draw oneself * @param t the Turtle to draw with **/ abstract public void drawWith(Turtle t); // No body in the superclass /** * Draw on the given picture **/ public void drawOn(Picture bg){ Turtle t = new Turtle(bg); t.setPenDown(false); this.drawWith(t); } /** Method to remove node from list, fixing links appropriately. * @param node element to remove from list. **/ public void remove(DrawableNode node){ if (node==this) { System.out.println("I can't remove the first node from the list."); return; }; DrawableNode current = this; // While there are more nodes to consider while (current.getNext() != null) { if (current.getNext() == node){ // Simply make node's next be this next current.setNext(node.getNext()); // Make this node point to nothing node.setNext(null); return; } current = current.getNext(); } } /** * Insert the input node after this node. * @param node element to insert after this. **/ public void insertAfter(DrawableNode node){ // Save what "this" currently points at DrawableNode oldNext = this.getNext(); this.setNext(node); node.setNext(oldNext); } /** * Return the last element in the list **/ public DrawableNode last() { DrawableNode current; current = this; while (current.getNext() != null) { current = current.getNext(); }; return current; } /** * Add the input node after the last node in this list. * @param node element to insert after this. **/ public void add(DrawableNode node){ this.last().insertAfter(node); } }