⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 edgerenderer.java

📁 用JGraph编的软件
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* * @(#)EdgeRenderer.java	1.0 1/1/02 *  * Copyright (c) 2001-2004, Gaudenz Alder  * All rights reserved.  *  * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: *  * - Redistributions of source code must retain the above copyright notice, *   this list of conditions and the following disclaimer. * - Redistributions in binary form must reproduce the above copyright notice, *   this list of conditions and the following disclaimer in the documentation  *   and/or other materials provided with the distribution. * - Neither the name of JGraph nor the names of its contributors may be used *   to endorse or promote products derived from this software without specific *   prior written permission. *  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. * */package org.jgraph.graph;import java.awt.BasicStroke;import java.awt.Color;import java.awt.Component;import java.awt.Dimension;import java.awt.FontMetrics;import java.awt.Graphics;import java.awt.Graphics2D;import java.awt.Point;import java.awt.Polygon;import java.awt.Rectangle;import java.awt.Shape;import java.awt.geom.Ellipse2D;import java.awt.geom.GeneralPath;import java.awt.image.BufferedImage;import java.io.Serializable;import java.util.Map;import javax.swing.JComponent;import javax.swing.UIManager;import org.jgraph.JGraph;/** * This renderer displays entries that implement the CellView interface * and supports the following attributes: * <li> * GraphConstants.POINTS * GraphConstants.FONT * GraphConstants.OPAQUE * GraphConstants.BORDER * GraphConstants.BORDERCOLOR * GraphConstants.LINECOLOR * GraphConstants.LINEWIDTH * GraphConstants.FOREGROUND * GraphConstants.BACKGROUND * GraphConstants.DASHPATTERN * GraphConstants.LINESTYLE * GraphConstants.START * GraphConstants.END * GraphConstants.STARTSIZE * GraphConstants.ENDSIZE * </li> * * @version 1.0 1/1/02 * @author Gaudenz Alder */public class EdgeRenderer	extends JComponent	implements CellViewRenderer, Serializable {	/** Static Graphics used for Font Metrics */	protected transient Graphics fontGraphics =		new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB).getGraphics();	/** Reference to the font metrics of the above */	protected transient FontMetrics metrics;	/** Cache the current graph for drawing */	protected transient JGraph graph;	/** Cache the current edgeview for drawing */	protected transient EdgeView view;	/** Painting attributes of the current edgeview */	protected transient int beginDeco, endDeco, beginSize, endSize, lineStyle;	/** Width of the current edge view */	protected transient float lineWidth;	/** Boolean attributes of the current edgeview. Fill flags are checked	 *  for valid decorations.	 */	protected transient boolean labelBorder,		beginFill,		endFill,		focus,		selected,		preview,		opaque;	/** Color attributes of the current edgeview. This components foreground	 *  is set to the edgecolor, the fontColor is in an extra variable. If	 *  the fontColor is null, the current foreground is used.	 *  The default background instead is used for text and is not visible	 *  if the label is not visible or if opaque is true.	 */	protected transient Color borderColor,		defaultForeground,		defaultBackground,		fontColor;	/** Contains the current dash pattern. Null means no pattern. */	protected transient float[] lineDash;	/**	 * Constructs a renderer that may be used to render edges.	 */	public EdgeRenderer() {		defaultForeground = UIManager.getColor("Tree.textForeground");		defaultBackground = UIManager.getColor("Tree.textBackground");	}	/**     * Sets view to work with, caching necessary values     * until the next call of this method or until some other methods with     * explicitly specified different view     */     void setView(CellView value) {         if (value instanceof EdgeView) {             if (view != value) {                 view = (EdgeView) value;                 installAttributes(view);             }         } else {             view = null;         }     }    /**	 * Configure and return the renderer based on the passed in	 * components. The value is typically set from messaging the	 * graph with <code>convertValueToString</code>.	 *	 * @param   graph the graph that that defines the rendering context.	 * @param   value the object that should be rendered.	 * @param   selected whether the object is selected.	 * @param   hasFocus whether the object has the focus.	 * @param   isPreview whether we are drawing a preview.	 * @return	the component used to render the value.	 */	public Component getRendererComponent(		JGraph graph,		CellView view,		boolean sel,		boolean focus,		boolean preview) {		if (view instanceof EdgeView && graph != null) {			this.graph = graph;			this.focus = focus;			this.selected = sel;			this.preview = preview;			setView(view);			return this;		}		return null;	}	/**	 * Returns true if the edge shape intersects the given rectangle.	 */	public boolean intersects(Graphics g, CellView value, Rectangle r) {		if (value instanceof EdgeView && g != null && value != null) {            setView(value);            Graphics2D g2 = (Graphics2D) g;            boolean hit = g2.hit(r, view.getShape(), true);			if (hit)				return true;			Rectangle rect = view.getLabelBounds();			if (rect != null)				return rect.intersects(r);		}		return false;	}	/**	 * Returns the bounds of the edge shape.	 */	public Rectangle getBounds(CellView value) {		if (value instanceof EdgeView && value != null) {            setView(value);			Rectangle r = getPaintBounds(view);			Rectangle label = getLabelBounds(view);			if (label != null)				r = r.union(label);			int b = (int) Math.ceil(lineWidth);			r.x = r.x - b;			r.y = r.y - b;			r.width = r.width + 2 * b;			r.height = r.height + 2 * b;			return r;		}		return null;	}	/**	 * Returns the label bounds of the specified view in the given graph.	 */	public Rectangle getLabelBounds(EdgeView view) {        setView(view);		Point p = getLabelPosition(this.view);		Dimension d = getLabelSize(this.view);		if (p != null && d != null) {			p.translate(-d.width / 2, -d.height / 2);			return new Rectangle(p.x, p.y, d.width + 1, d.height + 1);		}		return null;	}	/**	 * Returns the label position of the specified view in the given graph.	 */	public Point getLabelPosition(EdgeView view) {        setView(view);		Rectangle tmp = getPaintBounds(view);		Point pos = view.getLabelPosition();		int unit = GraphConstants.PERMILLE;		Point p0 = view.getPoint(0);		Point pe = view.getPoint(view.getPointCount()-1);		if (pos != null && tmp != null) {		    int x0 = tmp.x;		    int xdir = 1;		    if (p0.x > pe.x) {			x0 += tmp.width;			xdir = -1;		    }		    int y0 = tmp.y;		    int ydir = 1;		    if (p0.y > pe.y) {			y0 += tmp.height;			ydir = -1;		    }		    int x = x0 + xdir * (tmp.width * pos.x / unit);		    int y = y0 + ydir * (tmp.height * pos.y / unit);		    return new Point(x, y);		}		return null;	}	/**	 * Returns the label size of the specified view in the given graph.	 */	public Dimension getLabelSize(EdgeView view) {        setView(view);		Object label = view.getGraph().convertValueToString(view);		if (label != null && label.toString().length() > 0) {			fontGraphics.setFont(				GraphConstants.getFont(view.getAllAttributes()));			metrics = fontGraphics.getFontMetrics();			int sw = metrics.stringWidth(label.toString());			int sh = metrics.getHeight();			return new Dimension(sw, sh);		}		return null;	}	/**	 * Installs the attributes of specified cell in this	 * renderer instance. This means, retrieve every published	 * key from the cells hashtable and set global variables	 * or superclass properties accordingly.	 *	 * @param   cell to retrieve the attribute values from.	 */	protected void installAttributes(CellView view) {		Map map = view.getAllAttributes();		beginDeco = GraphConstants.getLineBegin(map);		beginSize = GraphConstants.getBeginSize(map);		beginFill = GraphConstants.isBeginFill(map) && isFillable(beginDeco);		endDeco = GraphConstants.getLineEnd(map);		endSize = GraphConstants.getEndSize(map);		endFill = GraphConstants.isEndFill(map) && isFillable(endDeco);		lineWidth = GraphConstants.getLineWidth(map);		lineStyle = GraphConstants.getLineStyle(map);		lineDash = GraphConstants.getDashPattern(map);		borderColor = GraphConstants.getBorderColor(map);		Color foreground = GraphConstants.getLineColor(map);		setForeground((foreground != null) ? foreground : defaultForeground);		Color background = GraphConstants.getBackground(map);		setBackground((background != null) ? background : defaultBackground);		setOpaque(GraphConstants.isOpaque(map));		setFont(GraphConstants.getFont(map));		Color tmp = GraphConstants.getForeground(map);		fontColor = (tmp != null) ? tmp : getForeground();		fontGraphics.setFont(getFont());		metrics = fontGraphics.getFontMetrics();	}	protected boolean isFillable(int decoration) {		return !(			decoration == GraphConstants.ARROW_SIMPLE				|| decoration == GraphConstants.ARROW_LINE				|| decoration == GraphConstants.ARROW_DOUBLELINE);	}	/**	 * Returns the bounds of the edge shape without label	 */	public Rectangle getPaintBounds(EdgeView view) {        setView(view);		return view.getShape().getBounds();	}	/**	 * Paint the renderer.	 */	public void paint(Graphics g) {		Shape edgeShape = view.getShape();		// Sideeffect: beginShape, lineShape, endShape		if (edgeShape != null) {			Graphics2D g2 = (Graphics2D) g;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -