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

📄 graphvertexrender.java

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

import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.geom.Point2D;
import java.io.ByteArrayOutputStream;

import org.jgraph.JGraph;
import org.jgraph.graph.CellView;
import org.jgraph.graph.GraphConstants;
import org.jgraph.graph.VertexRenderer;
import org.jgraph.graph.VertexView;

import flow.graph.gui.graph.encoder.XMLEncoder;

/**
 * @author Administrator
 *
 */
public class GraphVertexRender extends VertexRenderer{
	static int wordlength = 10;
	static int lineview = 20;
	
    public void fillGradient(Component c, Graphics g, Color start, Color end) {
		Rectangle bounds = this.getBounds();  // How big is the component?
		// Get the red, green, and blue components of the start and end
		// colors as floats between 0.0 and 1.0.  Note that the Color class
		// also works with int values between 0 and 255
		float r1 = start.getRed()/255.0f;
		float g1 = start.getGreen()/255.0f;
		float b1 = start.getBlue()/255.0f;
		float r2 = end.getRed()/255.0f;
		float g2 = end.getGreen()/255.0f;
		float b2 = end.getBlue()/255.0f;
		// Figure out how much each component should change at each y value
		float dr = (r2-r1)/bounds.height;
		float dg = (g2-g1)/bounds.height;
		float db = (b2-b1)/bounds.height;

		// Now loop once for each row of pixels in the component
		for(int y = 0; y < bounds.height; y++) {
		    g.setColor(new Color(r1, g1, b1));    // Set the color of the row
		    g.drawLine(0, y, bounds.width-1, y);  // Draw the row
		    r1 += dr; g1 += dg; b1 += db;         // Increment color components
		}
	}

	
	public Component getRendererComponent(JGraph graph, CellView view,
			boolean sel, boolean focus, boolean preview) {
		gridColor = graph.getGridColor();
		highlightColor = graph.getHighlightColor();
		lockedHandleColor = graph.getLockedHandleColor();
		isDoubleBuffered = graph.isDoubleBuffered();
		if (view instanceof VertexView) {
			this.view = (VertexView) view;
			int width = (int)view.getBounds().getWidth();
			int height = (int)view.getBounds().getHeight();
			int viewline = height / lineview;
			//System.out.println("height="+height);
			//System.out.println("width="+width);
			setComponentOrientation(graph.getComponentOrientation());
			if (graph.getEditingCell() != view.getCell()) {
				Object label = graph.convertValueToString(view);
				if (label != null){
					/*
					
					//System.out.println("text="+label.toString());
					String text = "<html>";
					//显示的行数
					int line = label.toString().length() / (width/wordlength);
					//每行字数
					int words = width / wordlength;
					System.out.println("line="+line+",viewline="+viewline);
					if(line > 0){
						for(int i=0;i<line;i++){
							if((i+1)<line){
								//System.out.println("label.length="+label.toString().length()+","+i*width+","+(i+1)*width);
								text += label.toString().substring(i*words, (i+1)*words);
								System.out.println("i="+i+",text="+text);
								if((i+1) >= viewline){
									text += "...";
									System.out.println(text);
									break;
								}
								else
									text += "<br>";
							}
							else
								text += label.toString().substring(i*words); 
						}
					}
					else{
						text = label.toString();
					}
					//System.out.println(text);
					setText(text);
					*/
					setText(label.toString());
				}
				else
					setText(null);
			} else
				setText(null);
			this.hasFocus = focus;
			this.childrenSelected = graph.getSelectionModel().isChildrenSelected(view.getCell());
			this.selected = sel;
			this.preview = preview;
			if (this.view.isLeaf() || GraphConstants.isGroupOpaque(view.getAllAttributes()))
				installAttributes(view);
			else
				resetAttributes();
			//System.out.println("position="+GraphConstants.getVerticalTextPosition(view.getAllAttributes()));
			//Point2D point = new Point2D.Float((int)view.getBounds().getX(), (int)(view.getBounds().getY()+view.getBounds().getHeight()));
			//GraphConstants.setLabelPosition(view.getAllAttributes(), point);
			//System.out.println("position="+GraphConstants.getBounds(view.getAttributes()).getX()+","+GraphConstants.getBounds(view.getAttributes()).getY());
			//PrintGraphCell(view);
			return this;
		}
		return null;
	}
	private void PrintGraphCell(Object cell){
		System.out.println("PrintGraphCell..........");
		ByteArrayOutputStream bout = new ByteArrayOutputStream();
		XMLEncoder enc = new XMLEncoder(bout);
		enc.writeObject(cell);
		enc.close();
		System.out.println(bout);
	}
}

⌨️ 快捷键说明

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