📄 visiblerendering.java
字号:
package edu.odu.cs.zeil.AlgAE.Server;import java.awt.Color;import java.util.Enumeration;import java.util.Hashtable;import edu.odu.cs.zeil.AlgAE.Direction;import edu.odu.cs.zeil.AlgAE.Server.MessageHandler;import edu.odu.cs.zeil.AlgAE.Server.OneTimeQueue;import edu.odu.cs.zeil.AlgAE.Server.Rendering;import edu.odu.cs.zeil.AlgAE.Server.Server;import edu.odu.cs.zeil.AlgAE.Server.ServerThread;import edu.odu.cs.zeil.AlgAE.Server.TimeStamped;import edu.odu.cs.zeil.AlgAE.Server.Visible;/** * This class implements the basic mechanisms for making an object * visible (i.e., sending to the AlgAE client program information * about how to render the object) as well as providing programmers * with access to decorative controls such as highlighting, color * changing, etc. * * To make an object visible in an AlgAE animation, it should * 1) contain a data member of this VisibleRendering class, and * 2) implement the Visible interface. * * @see edu.odu.cs.zeil.AlgAE.Server.Visible **/public class VisibleRendering implements Rendering, TimeStamped{ /** * Create a rendering object. * * @param renders The visible object that this describes. * @param color color to use when displaying object * @param vertical If this object has Visible components, should the * components be stacked vertically (false => horizontally) * Ignored if object has no Visible components. * @note postcondition: renders.getRendering() == this **/ public VisibleRendering (Visible renders, Color color, boolean vertical) { renderingOf = renders; ++lastID; ID = new Integer(lastID); theName = null; defaultColor = color; highlighted = new Highlighting(); highlightDir = new Highlighting[17]; for (int i = 0; i < 17; ++i) highlightDir[i] = new Highlighting(); highlightTo = new Hashtable(); isVertical = vertical; timeStamp = -1; } /** * Create a rendering object. * * @param renders The visible object that this describes. * @param color color to use when displaying object * @note Equivalent to <code>VisibleRendering(renders, color, true)</code> **/ public VisibleRendering (Visible renders, Color color) { renderingOf = renders; ++lastID; ID = new Integer(lastID); theName = null; defaultColor = color; highlighted = new Highlighting(); highlightDir = new Highlighting[17]; for (int i = 0; i < 17; ++i) highlightDir[i] = new Highlighting(); highlightTo = new Hashtable(); isVertical = true; timeStamp = -1; } static class Highlighting { Color color; int timeStamp; static int time = 1; public Highlighting () { color = null; timeStamp = 0; } public boolean isHighlighted() { return (timeStamp == time) && (color != null); } public Color getColor() { return (timeStamp == time) ? color : null; } public Color getColor(Color defaultColor) { return (timeStamp == time) ? ((color == null) ? defaultColor : color) : defaultColor; } public void setHighlight (Color newColor) { color = newColor; timeStamp = time; } static void advanceTime() { ++time; } } /** * Indicates that this object and any objects reachable from it * via the <code>touchAllPointers</code> and * <code>touchAllComponents</code> activities should be portrayed in * the animation. * * @see edu.odu.cs.zeil.AlgAE.Server.Visible#touchAllPointers * @see edu.odu.cs.zeil.AlgAE.Server.Visible#touchAllComponents **/ public void show() { server().show (this); } /** * Indicates that this object should only be portrayed in the * animation if it can be reached from another portrayed object * via the <code>touchAllPointers</code> and * <code>touchAllComponents</code> activities. * * @see edu.odu.cs.zeil.AlgAE.Server.Visible#touchAllPointers * @see edu.odu.cs.zeil.AlgAE.Server.Visible#touchAllComponents **/ public void hide() { server().hide (this); } /** * Indicates that the object vr and any objects "show"n after it * should only be portrayed in the * animation if they can be reached from another portrayed object * via the <code>touchAllPointers</code> and * <code>touchAllComponents</code> activities. * * @see edu.odu.cs.zeil.AlgAE.Server.Visible#touchAllPointers * @see edu.odu.cs.zeil.AlgAE.Server.Visible#touchAllComponents **/ public void hideAllSince() { server().hideAllSince (this); } /** * Called by <CODE>Visible.touchAllPointers()</CODE> to indicate * that an arrow should be drawn connecting this object to the object * <CODE>pointTo</CODE>. * * @param pointTo the object that will be pointed to * @param dir the direction from which the arrow will emerge * from this object * @param label A label to place along the arrow. * * @see edu.odu.cs.zeil.AlgAE.Server.Visible#touchAllPointers **/ public void touch (Visible pointTo, int dir, String label) { VisibleRendering pointedTo = (VisibleRendering)pointTo.getRendering(); if (pointTo != null) { Color c = highlightDir[dir].getColor (Color.black); Highlighting high = (Highlighting)highlightTo.get(pointedTo.ID); if (high != null) c = high.getColor(c); messageHandler().touch (ID, pointedTo.ID, dir, c, label); } else messageHandler().touch (ID, new Integer(0), dir, Color.black, label); OneTimeQueue q = server().touchQueue; if (q != null) q.addToRear (pointedTo); } /** * Called by <CODE>Visible.touchAllPointers()</CODE> to indicate * that an arrow should be drawn connecting this object to the object * <CODE>pointTo</CODE>. * <P> * Equivalent to <CODE>touch(pointTo, dir, "")</CODE> * * @param pointTo the object that will be pointed to * @param dir the direction from which the arrow will emerge * from this object * @param label A label to place along the arrow. * * @see edu.odu.cs.zeil.AlgAE.Server.Visible#touchAllPointers **/ public void touch (Visible pointTo, int dir) { touch (pointTo, dir, ""); } /** * Called by <CODE>Visible.touchAllComponents()</CODE> to indicate * that an object c should be drawn as an internal, contained * component of this one. * * @param component the object to be drawn inside this one * * @see edu.odu.cs.zeil.AlgAE.Server.Visible#touchAllComponents **/ public void touch (Visible component) { VisibleRendering theComponent = (VisibleRendering)component.getRendering(); if (component != null) messageHandler().touch (ID, theComponent.ID); OneTimeQueue q = server().touchQueue; if (q != null) q.addToRear (theComponent); } //////////////////////////////////////////////////// // The remaining functions provide ways to elaborate on basic // animations. /** * Highlight (temporary color change) this object. **/ public void highlight() { Color hc = new Color (255-defaultColor.getRed(), 255-defaultColor.getGreen(), 255-defaultColor.getBlue()); highlight (hc); } /** * Highlight (temporary color change) this object. * * @param color color to use when drawing this obejct **/ public void highlight(Color color) { highlighted.setHighlight (color); } /** * Remove highlighting, returning the object to its default color. **/ public void unHighlight() { highlighted.setHighlight (null); } /** * Highlight (temporary color change) arrows emanating from * this object in the specified direction. **/ public void highlight(int dir) { highlightDir[dir].setHighlight(Color.red); } /** * Remove highlighting of arrows emanating from * this object in the specified direction. **/ public void unHighlight(int dir) { highlightDir[dir].setHighlight(null); } /** * Highlight (temporary color change) arrows from * this object to the object <CODE>pointedTo</CODE> **/ public void highlight(Object pointedTo) { if (pointedTo != null) { Visible vPointedTo = (Visible)pointedTo; VisibleRendering pointTo = (VisibleRendering)vPointedTo.getRendering(); Highlighting high = (Highlighting)highlightTo.get (pointTo.ID); if (high == null) { high = new Highlighting(); highlightTo.put (pointTo.ID, high); } high.setHighlight (Color.red); } } /** * Remove highlighting of arrows from * this object to the object <CODE>pointedTo</CODE> **/ public void unHighlight(Object pointedTo) { if (pointedTo != null) { Visible vPointedTo = (Visible)pointedTo; VisibleRendering pointTo = (VisibleRendering)vPointedTo.getRendering(); highlightTo.remove (pointTo.ID); } } /** * Turn off both object and arrow highlights for all objects. **/ public static void unHighlightAll() { Highlighting.advanceTime(); } /** * Permanent color change to an object **/ public void setColor (Color color) { defaultColor = color; highlighted.setHighlight (null); } /** * What is the current color of this object? **/ public Color color() { return highlighted.getColor (defaultColor); } /** * Assign the object a "name" to be used when displaying it **/ public void setName (String name) { theName = name; } /** * Return the object's assigned name (null if none has been assigned). **/ public String getName() { return theName; } /** * Each rendering object is given a unique ID. * What ID does this obejct have? **/ int getID () { return ID.intValue(); } /////////////////////////////////////////////// /** * Color to use when drawing this object if not highlighted. **/ private Color defaultColor; /** * If this object has contained components, should they be * drawn in a vertical arrangement? **/ private boolean isVertical; /** * Is this object highlighted? **/ private Highlighting highlighted; /** * Are edges out of this object highlighted (by direction)? **/ private Highlighting[] highlightDir; /** * Are edges out of this object highlighted (by object ID)? **/ private Hashtable highlightTo; /** * Are components of this object arranged vertically? */ public boolean getVertical() { return isVertical; } /** * Name for this object **/ private String theName; /** * Unique identifier for all rendering objects. **/ private Integer ID; /** * Used to generate ID's **/ private static int lastID = 1000; /** * Implements TimeStamped **/ private int timeStamp; /** * Implements TimeStamped **/ public int getStamp () {return timeStamp;} /** * Implements TimeStamped **/ public void putStamp (int stamp) {timeStamp = stamp;} /** * What object is this a rendering of? **/ Visible renderingOf; /** * Get the server for this thread */ private static Server server() { ServerThread st = (ServerThread)Thread.currentThread(); return st.getServer(); } /** * Get the message Handler for this thread */ private static MessageHandler messageHandler() { return server().getMessageHandler(); } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -