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

📄 mxgraphics2dcanvas.java

📁 经典的java图像处理程序源码
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/** * $Id: mxGraphics2DCanvas.java,v 1.142 2009/02/15 10:01:06 gaudenz Exp $ * Copyright (c) 2007, Gaudenz Alder */package com.mxgraph.canvas;import java.awt.AlphaComposite;import java.awt.BasicStroke;import java.awt.Color;import java.awt.Composite;import java.awt.Font;import java.awt.FontMetrics;import java.awt.GradientPaint;import java.awt.Graphics2D;import java.awt.Image;import java.awt.Paint;import java.awt.Polygon;import java.awt.Rectangle;import java.awt.Stroke;import java.awt.geom.AffineTransform;import java.awt.geom.Area;import java.awt.geom.Ellipse2D;import java.awt.geom.GeneralPath;import java.awt.geom.Line2D;import java.awt.geom.QuadCurve2D;import java.util.Hashtable;import java.util.List;import javax.swing.CellRendererPane;import com.mxgraph.util.mxConstants;import com.mxgraph.util.mxLightweightTextPane;import com.mxgraph.util.mxPoint;import com.mxgraph.util.mxUtils;/** * An implementation of a canvas that uses Graphics2D for painting. */public class mxGraphics2DCanvas extends mxBasicCanvas{	/**	 * Cache for images.	 */	protected Hashtable imageCache = new Hashtable();	/**	 * Optional renderer pane to be used for HTML label rendering.	 */	protected CellRendererPane rendererPane;	/**	 * Global graphics handle to the image.	 */	protected Graphics2D g;	/**	 * Constructs a new graphics canvas with an empty graphics object.	 */	public mxGraphics2DCanvas()	{		this(null);	}	/**	 * Constructs a new graphics canvas for the given graphics object.	 */	public mxGraphics2DCanvas(Graphics2D g)	{		this.g = g;		// Initializes the cell renderer pane for drawing HTML markup		try		{			rendererPane = new CellRendererPane();		}		catch (Exception e)		{			// ignore		}	}	/**	 * Returns the graphics object for this canvas.	 */	public Graphics2D getGraphics()	{		return g;	}	/**	 * Sets the graphics object for this canvas.	 */	public void setGraphics(Graphics2D g)	{		this.g = g;	}	/**	 * Returns an image instance for the given URL. If the URL has	 * been loaded before than an instance of the same instance is	 * returned as in the previous call.	 */	protected Image loadImage(String image)	{		Image img = (Image) imageCache.get(image);		if (img == null)		{			img = mxUtils.loadImage(image);			if (img != null)			{				imageCache.put(image, img);			}		}		return img;	}	/*	 * (non-Javadoc)	 * @see com.mxgraph.canvas.mxICanvas#drawVertex(int, int, int, int, java.util.Hashtable)	 */	public Object drawVertex(int x, int y, int w, int h, Hashtable style)	{		if (g != null)		{			x += translate.x;			y += translate.y;			// Applies the rotation on the graphics object and stores			// the previous transform so that it can be restored			AffineTransform transform = null;			double rotation = mxUtils.getDouble(style,					mxConstants.STYLE_ROTATION, 0);			if (rotation != 0)			{				transform = g.getTransform();				g.rotate(Math.toRadians(rotation), x + w / 2, y + h / 2);			}			Composite composite = null;			float opacity = mxUtils.getFloat(style, mxConstants.STYLE_OPACITY,					100);			// Applies the opacity to the graphics object			if (opacity != 100)			{				composite = g.getComposite();				g.setComposite(AlphaComposite.getInstance(						AlphaComposite.SRC_OVER, opacity / 100));			}			// Saves the stroke			Stroke stroke = g.getStroke();			// Draws a swimlane if start is > 0			int start = mxUtils.getInt(style, mxConstants.STYLE_STARTSIZE);			if (start == 0)			{				drawShape(x, y, w, h, style);			}			else			{				start = (int) Math.round(start * scale);				// Removes some styles to draw the content area				Hashtable cloned = new Hashtable(style);				cloned.remove(mxConstants.STYLE_FILLCOLOR);				cloned.remove(mxConstants.STYLE_ROUNDED);				if (mxUtils.isTrue(style, mxConstants.STYLE_HORIZONTAL, true))				{					drawShape(x, y, w, start, style);					drawShape(x, y + start, w, h - start, cloned);				}				else				{					drawShape(x, y, start, h, style);					drawShape(x + start, y, w - start, h, cloned);				}			}			// Restores the stroke			g.setStroke(stroke);			// Restores the composite rule on the graphics object			if (composite != null)			{				g.setComposite(composite);			}			// Restores the affine transformation			if (transform != null)			{				g.setTransform(transform);			}		}		return null;	}	/*	 * (non-Javadoc)	 * @see com.mxgraph.canvas.mxICanvas#drawEdge(java.util.List, java.util.Hashtable)	 */	public Object drawEdge(List pts, Hashtable style)	{		if (g != null)		{			// Transpose all points by cloning into a new array			pts = mxUtils.translatePoints(pts, translate.x, translate.y);			float opacity = mxUtils.getFloat(style, mxConstants.STYLE_OPACITY,					100);			Composite composite = null;			// Applies the opacity to the graphics object			if (opacity != 100)			{				composite = g.getComposite();				g.setComposite(AlphaComposite.getInstance(						AlphaComposite.SRC_OVER, opacity / 100));			}			// Saves the stroke			Stroke stroke = g.getStroke();			drawLine(pts, style);			// Restores the stroke			g.setStroke(stroke);			// Resets the composite rule on the graphics object			if (composite != null)			{				g.setComposite(composite);			}		}		return null;	}	/*	 * (non-Javadoc)	 * @see com.mxgraph.canvas.mxICanvas#drawLabel(java.lang.String, int, int, int, int, java.util.Hashtable, boolean)	 */	public Object drawLabel(String label, int x, int y, int w, int h,			Hashtable style, boolean isHtml)	{		if (g != null && drawLabels)		{			x += translate.x;			y += translate.y;			if (label != null && label.length() > 0)			{				Composite composite = null;				float opacity = mxUtils.getFloat(style,						mxConstants.STYLE_TEXT_OPACITY, 100);				// Applies the opacity to the graphics object				if (opacity != 100)				{					composite = g.getComposite();					g.setComposite(AlphaComposite.getInstance(							AlphaComposite.SRC_OVER, opacity / 100));				}				// Draws the label background and border				Color bg = mxUtils.getColor(style,						mxConstants.STYLE_LABEL_BACKGROUNDCOLOR);				Color border = mxUtils.getColor(style,						mxConstants.STYLE_LABEL_BORDERCOLOR);				// Draws the label background				if (bg != null)				{					g.setColor(bg);					g.fillRect(x, y, w, h);				}				// Draws the label border				if (border != null)				{					g.setColor(border);					g.drawRect(x, y, w, h);				}				if (isHtml)				{					drawHtmlText(mxUtils.getBodyMarkup(label, true), x, y, w,							h, style);				}				else				{					Font font = mxUtils.getFont(style, scale);					if (font.getSize() > 0)					{						g.setFont(font);						drawPlainText(label, x, y, w, h, style);					}				}				if (composite != null)				{					g.setComposite(composite);				}			}		}		return null;	}	/**	 * Draws the shape specified with the STYLE_SHAPE key in the given style.	 * 	 * @param x X-coordinate of the shape.	 * @param y Y-coordinate of the shape.	 * @param w Width of the shape.	 * @param h Height of the shape.	 * @param style Style of the the shape.	 */	public void drawShape(int x, int y, int w, int h, Hashtable style)	{		Color penColor = mxUtils.getColor(style, mxConstants.STYLE_STROKECOLOR);		float penWidth = mxUtils.getFloat(style, mxConstants.STYLE_STROKEWIDTH,				1);		int pw = (int) Math.ceil(penWidth * scale);		if (g.hitClip(x - pw, y - pw, w + 2 * pw, h + 2 * pw))		{			// Prepares the background			boolean shadow = mxUtils.isTrue(style, mxConstants.STYLE_SHADOW,					false);			Color fillColor = mxUtils.getColor(style,					mxConstants.STYLE_FILLCOLOR);			Paint fillPaint = getFillPaint(new Rectangle(x, y, w, h),					fillColor, style);			if (penWidth > 0)			{				if (mxUtils.isTrue(style, mxConstants.STYLE_DASHED, false))				{					g.setStroke(new BasicStroke((float) (penWidth * scale),							BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER,							10.0f, new float[] { (float) (3 * scale),									(float) (3 * scale) }, 0.0f));				}				else				{					g.setStroke(new BasicStroke((float) (penWidth * scale)));				}			}			// Draws the shape			String shape = mxUtils					.getString(style, mxConstants.STYLE_SHAPE, "");			if (shape.equals(mxConstants.SHAPE_IMAGE))			{				String img = getImageForStyle(style);				if (img != null)				{					drawImage(x, y, w, h, img);				}			}			else if (shape.equals(mxConstants.SHAPE_LINE))			{				if (penColor != null)				{					g.setColor(penColor);					String direction = mxUtils.getString(style,							mxConstants.STYLE_DIRECTION,							mxConstants.DIRECTION_EAST);					if (direction.equals(mxConstants.DIRECTION_EAST)							|| direction.equals(mxConstants.DIRECTION_WEST))					{						int mid = (int) (y + h / 2);						drawLine(x, mid, x + w, mid);					}					else					{						int mid = (int) (x + w / 2);						drawLine(mid, y, mid, y + h);					}				}			}			else if (shape.equals(mxConstants.SHAPE_ELLIPSE))			{				drawOval(x, y, w, h, fillColor, fillPaint, penColor, shadow);			}			else if (shape.equals(mxConstants.SHAPE_DOUBLE_ELLIPSE))			{				drawOval(x, y, w, h, fillColor, fillPaint, penColor, shadow);				int inset = (int) ((3 + penWidth) * scale);				x += inset;				y += inset;				w -= 2 * inset;				h -= 2 * inset;				drawOval(x, y, w, h, null, null, penColor, false);			}			else if (shape.equals(mxConstants.SHAPE_RHOMBUS))			{				drawRhombus(x, y, w, h, fillColor, fillPaint, penColor, shadow);			}			else if (shape.equals(mxConstants.SHAPE_CYLINDER))			{				drawCylinder(x, y, w, h, fillColor, fillPaint, penColor, shadow);			}			else if (shape.equals(mxConstants.SHAPE_ACTOR))			{				drawActor(x, y, w, h, fillColor, fillPaint, penColor, shadow);			}			else if (shape.equals(mxConstants.SHAPE_CLOUD))			{				drawCloud(x, y, w, h, fillColor, fillPaint, penColor, shadow);			}			else if (shape.equals(mxConstants.SHAPE_TRIANGLE))			{				String direction = mxUtils.getString(style,						mxConstants.STYLE_DIRECTION, "");				drawTriangle(x, y, w, h, fillColor, fillPaint, penColor,						shadow, direction);			}			else if (shape.equals(mxConstants.SHAPE_HEXAGON))			{				String direction = mxUtils.getString(style,						mxConstants.STYLE_DIRECTION, "");				drawHexagon(x, y, w, h, fillColor, fillPaint, penColor, shadow,						direction);			}			else			{				drawRect(x, y, w, h, fillColor, fillPaint, penColor, shadow,						mxUtils.isTrue(style, mxConstants.STYLE_ROUNDED));				// Draws the image inside the label shape				if (shape.equals(mxConstants.SHAPE_LABEL))				{					String img = getImageForStyle(style);					if (img != null)					{						String imgAlign = mxUtils.getString(style,								mxConstants.STYLE_IMAGE_ALIGN,								mxConstants.ALIGN_CENTER);						String imgValign = mxUtils.getString(style,								mxConstants.STYLE_IMAGE_VERTICAL_ALIGN,								mxConstants.ALIGN_MIDDLE);						int imgWidth = (int) (mxUtils.getInt(style,								mxConstants.STYLE_IMAGE_WIDTH,								mxConstants.DEFAULT_IMAGESIZE) * scale);						int imgHeight = (int) (mxUtils.getInt(style,								mxConstants.STYLE_IMAGE_HEIGHT,								mxConstants.DEFAULT_IMAGESIZE) * scale);						int spacing = (int) (mxUtils.getInt(style,								mxConstants.STYLE_SPACING, 2) * scale);						int imgX = x;						if (imgAlign.equals(mxConstants.ALIGN_RIGHT))						{							imgX += w - imgWidth - spacing;						}						else if (imgAlign.equals(mxConstants.ALIGN_CENTER))						{							imgX += (w - imgWidth) / 2;						}						else						{							imgX += spacing;						}						int imgY = y;						if (imgValign.equals(mxConstants.ALIGN_TOP))						{							imgY += spacing;						}						else if (imgValign.equals(mxConstants.ALIGN_BOTTOM))						{							imgY += h - imgHeight - spacing;						}						else						{							imgY += (h - imgHeight) / 2;						}						drawImage(imgX, imgY, imgWidth, imgHeight, img);					}				}			}		}	}	/**	 * Draws a a polygon for the given parameters.	 * 	 * @param polygon Points of the polygon.	 * @param fillColor Optional fill color of the shape.	 * @param fillPaint Optional paint of the shape.	 * @param penColor Optional stroke color.	 * @param shadow Boolean indicating if a shadow should be painted.	 */	protected void drawPolygon(Polygon polygon, Color fillColor,			Paint fillPaint, Color penColor, boolean shadow)	{		if (fillColor != null || fillPaint != null)		{			if (shadow)			{				g.setColor(mxConstants.SHADOW_COLOR);				g.translate(mxConstants.SHADOW_OFFSETX,						mxConstants.SHADOW_OFFSETY);				g.fillPolygon(polygon);				g.translate(-mxConstants.SHADOW_OFFSETX,						-mxConstants.SHADOW_OFFSETY);			}			if (fillPaint != null)			{				g.setPaint(fillPaint);			}			else			{				g.setColor(fillColor);			}			g.fillPolygon(polygon);		}		if (penColor != null)		{			g.setColor(penColor);			g.drawPolygon(polygon);

⌨️ 快捷键说明

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