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

📄 graphitemdiamond.java

📁 JGraph扩展应用。自定义Renderer,自定义视图View实现自定义工作流控件
💻 JAVA
字号:
package flow.graph.gui.graph.item;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Rectangle2D;
import java.awt.geom.RoundRectangle2D;

import org.jgraph.graph.CellViewRenderer;
import org.jgraph.graph.GraphConstants;
import org.jgraph.graph.VertexRenderer;
import org.jgraph.graph.VertexView;

public class GraphItemDiamond extends GraphVertexView {
	public static transient DiamondRenderer renderer = new DiamondRenderer();

	public GraphItemDiamond() {
		super();
	}

	public GraphItemDiamond(Object cell) {
		super(cell);
	}

	/**
	 * Returns the intersection of the bounding rectangle and the straight line
	 * between the source and the specified point p. The specified point is
	 * expected not to intersect the bounds.
	 */
	// todo public Point2D getPerimeterPoint(Point source, Point p) {
	/**
	 * getArcSize calculates an appropriate arc for the corners of the rectangle
	 * for boundary size cases of width and height
	 */
	public static int getArcSize(int width, int height) {
		int arcSize;

		// The arc width of a activity rectangle is 1/5th of the larger
		// of the two of the dimensions passed in, but at most 1/2
		// of the smaller of the two. 1/5 because it looks nice and 1/2
		// so the arc can complete in the given dimension

		if (width <= height) {
			arcSize = height / 5;
			if (arcSize > (width / 2)) {
				arcSize = width / 2;
			}
		} else {
			arcSize = width / 5;
			if (arcSize > (height / 2)) {
				arcSize = height / 2;
			}
		}

		return arcSize;
	}

	public CellViewRenderer getRenderer() {
		return renderer;
	}

	public static class DiamondRenderer extends GraphVertexRender {

		/**
		 * Return a slightly larger preferred size than for a rectangle.
		 */
		public Dimension getPreferredSize() {
			Dimension d = super.getPreferredSize();
			d.width += d.height / 5;
			//System.out.println("d1="+d);
			return d;
		}

		public void paint(Graphics g) {
			int b = borderWidth;
			Graphics2D g2 = (Graphics2D) g;
			//g2.setFont(new Font("SansSerif", Font.PLAIN, 10));      // select font
			g2.setStroke(new BasicStroke(1f));                    // 2 pixel lines
			g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,    // antialiasing
					   RenderingHints.VALUE_ANTIALIAS_ON);
			//g2.translate(1, 1);                                   // margins
			Dimension d = getSize();
			int roundRectArc = GraphItemDiamond.getArcSize(d.width - b,d.height - b);
			//Shape sp = new RoundRectangle2D.Float(b / 2, b / 2, d.width - (int) (b * 1.5),
			//		d.height - (int) (b * 1.5), roundRectArc, roundRectArc);
			Shape sp = new Diamond2D(b/2+0.5, b/2+0.5, d.width - (int) (b * 1.5), d.height - (int) (b * 1.5));
			//Shape sp = new Diamond2D(b/2, b/2, 1, 1);
			boolean tmp = selected;
		    //g2.setColor(Color.yellow);            // Set a color
		    //g2.fill(sp);                   // Fill the shape with it
		    g2.setColor(Color.gray);             // Switch to black
		    g2.draw(sp);                   // Outline the shape with it
			g2.setPaint(new GradientPaint((int)(b/2+0.5), (int)(b/2+0.5),Color.WHITE,d.width - (int) (b * 1.5), d.height - (int) (b * 1.5),new Color(186, 210, 45) ));
			g2.fill(sp);
			
			try {
				//setBorder(new JGraphShadowBorder());
				setBorder(null);
				setOpaque(false);
				selected = false;
				
				super.paint(g);
			} finally {
				selected = tmp;
			}
		}
	}

}

⌨️ 快捷键说明

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