📄 mxgraphics2dcanvas.java
字号:
} } /** * Draws a path for the given parameters. * * @param path Path object to be drawn. * @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 drawPath(GeneralPath path, 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.fill(path); g.translate(-mxConstants.SHADOW_OFFSETX, -mxConstants.SHADOW_OFFSETY); } if (fillPaint != null) { g.setPaint(fillPaint); } else { g.setColor(fillColor); } g.fill(path); } if (penColor != null) { g.setColor(penColor); g.draw(path); } } /** * Draws a rectangle for the given parameters. * * @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 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. * @param rounded Boolean indicating if the rectangle is rounded. */ protected void drawRect(int x, int y, int w, int h, Color fillColor, Paint fillPaint, Color penColor, boolean shadow, boolean rounded) { int radius = (rounded) ? getArcSize(w, h) : 0; if (fillColor != null || fillPaint != null) { if (shadow) { g.setColor(mxConstants.SHADOW_COLOR); if (rounded) { g.fillRoundRect(x + mxConstants.SHADOW_OFFSETX, y + mxConstants.SHADOW_OFFSETY, w, h, radius, radius); } else { g.fillRect(x + mxConstants.SHADOW_OFFSETX, y + mxConstants.SHADOW_OFFSETY, w, h); } } if (fillPaint != null) { g.setPaint(fillPaint); } else { g.setColor(fillColor); } if (rounded) { g.fillRoundRect(x, y, w, h, radius, radius); } else { // Only draws the filled region within the clipping bounds if (g.getClipBounds() != null) { Rectangle rect = new Rectangle(x, y, w, h); g.fill(rect.intersection(g.getClipBounds())); } else { g.fillRect(x, y, w, h); } } } if (penColor != null) { g.setColor(penColor); if (rounded) { g.drawRoundRect(x, y, w, h, radius, radius); } else { g.drawRect(x, y, w, h); } } } /** * Draws an image for the given parameters. * * @param x X-coordinate of the image. * @param y Y-coordinate of the image. * @param w Width of the image. * @param h Height of the image. * @param image URL of the image. */ protected void drawImage(int x, int y, int w, int h, String image) { Image img = loadImage(image); if (img != null) { g.drawImage(img, x, y, w, h, null); } } /** * Draws an oval for the given parameters. * * @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 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 drawOval(int x, int y, int w, int h, Color fillColor, Paint fillPaint, Color penColor, boolean shadow) { if (fillColor != null || fillPaint != null) { if (shadow) { g.setColor(mxConstants.SHADOW_COLOR); g.fillOval(x + mxConstants.SHADOW_OFFSETX, y + mxConstants.SHADOW_OFFSETY, w, h); } if (fillPaint != null) { g.setPaint(fillPaint); } else { g.setColor(fillColor); } g.fillOval(x, y, w, h); } if (penColor != null) { g.setColor(penColor); g.drawOval(x, y, w, h); } } /** * Draws a rhombus (aka. diamond) for the given parameters. * * @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 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 drawRhombus(int x, int y, int w, int h, Color fillColor, Paint fillPaint, Color penColor, boolean shadow) { int halfWidth = w / 2; int halfHeight = h / 2; Polygon rhombus = new Polygon(); rhombus.addPoint(x + halfWidth, y); rhombus.addPoint(x + w, y + halfHeight); rhombus.addPoint(x + halfWidth, y + h); rhombus.addPoint(x, y + halfHeight); drawPolygon(rhombus, fillColor, fillPaint, penColor, shadow); } /** * Draws a cylinder for the given parameters. * * @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 fillColor Optional fill color of the shape. * @param fillPaint Optional paint of the shape. * @param penColor Optional stroke color. * @param isShadow Boolean indicating if a shadow should be painted. */ protected void drawCylinder(int x, int y, int w, int h, Color fillColor, Paint fillPaint, Color penColor, boolean isShadow) { int h4 = h / 4; int r = w - 1; if (fillColor != null || fillPaint != null) { Area area = new Area(new Rectangle(x, y + h4 / 2, r, h - h4)); area.add(new Area(new Rectangle(x, y + h4 / 2, r, h - h4))); area.add(new Area(new Ellipse2D.Double(x, y, r, h4))); area.add(new Area(new Ellipse2D.Double(x, y + h - h4, r, h4))); if (isShadow) { g.setColor(mxConstants.SHADOW_COLOR); g.translate(mxConstants.SHADOW_OFFSETX, mxConstants.SHADOW_OFFSETY); g.fill(area); g.translate(-mxConstants.SHADOW_OFFSETX, -mxConstants.SHADOW_OFFSETY); } if (fillPaint != null) { g.setPaint(fillPaint); } else { g.setColor(fillColor); } g.fill(area); } if (penColor != null) { g.setColor(penColor); int h2 = h4 / 2; g.drawOval(x, y, r, h4); g.drawLine(x, y + h2, x, y + h - h2); g.drawLine(x + w - 1, y + h2, x + w - 1, y + h - h2); g.drawArc(x, y + h - h4, r, h4, 0, -180); } } /** * Draws an actor shape for the given parameters. * * @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 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 drawActor(int x, int y, int w, int h, Color fillColor, Paint fillPaint, Color penColor, boolean shadow) { float width = w * 2 / 6; GeneralPath path = new GeneralPath(); path.moveTo(x, y + h); path.curveTo(x, y + 3 * h / 5, x, y + 2 * h / 5, x + w / 2, y + 2 * h / 5); path.curveTo(x + w / 2 - width, y + 2 * h / 5, x + w / 2 - width, y, x + w / 2, y); path.curveTo(x + w / 2 + width, y, x + w / 2 + width, y + 2 * h / 5, x + w / 2, y + 2 * h / 5); path.curveTo(x + w, y + 2 * h / 5, x + w, y + 3 * h / 5, x + w, y + h); path.closePath(); drawPath(path, fillColor, fillPaint, penColor, shadow); } /** * Draws a cloud shape for the given parameters. * * @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 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 drawCloud(int x, int y, int w, int h, Color fillColor, Paint fillPaint, Color penColor, boolean shadow) { GeneralPath path = new GeneralPath(); path.moveTo((float) (x + 0.25 * w), (float) (y + 0.25 * h)); path.curveTo((float) (x + 0.05 * w), (float) (y + 0.25 * h), (float) x, (float) (y + 0.5 * h), (float) (x + 0.16 * w), (float) (y + 0.55 * h)); path.curveTo((float) x, (float) (y + 0.66 * h), (float) (x + 0.18 * w), (float) (y + 0.9 * h), (float) (x + 0.31 * w), (float) (y + 0.8 * h)); path.curveTo((float) (x + 0.4 * w), (float) (y + h), (float) (x + 0.7 * w), (float) (y + h), (float) (x + 0.8 * w), (float) (y + 0.8 * h)); path.curveTo((float) (x + w), (float) (y + 0.8 * h), (float) (x + w), (float) (y + 0.6 * h), (float) (x + 0.875 * w), (float) (y + 0.5 * h)); path.curveTo((float) (x + w), (float) (y + 0.3 * h), (float) (x + 0.8 * w), (float) (y + 0.1 * h), (float) (x + 0.625 * w), (float) (y + 0.2 * h)); path.curveTo((float) (x + 0.5 * w), (float) (y + 0.05 * h), (float) (x + 0.3 * w), (float) (y + 0.05 * h), (float) (x + 0.25 * w), (float) (y + 0.25 * h)); path.closePath(); drawPath(path, fillColor, fillPaint, penColor, shadow); } /** * Draws a triangle shape for the given parameters. * * @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 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 drawTriangle(int x, int y, int w, int h, Color fillColor, Paint fillPaint, Color penColor, boolean shadow, String direction) { Polygon triangle = new Polygon(); if (direction.equals(mxConstants.DIRECTION_NORTH)) { triangle.addPoint(x, y + h); triangle.addPoint(x + w / 2, y); triangle.addPoint(x + w, y + h); } else if (direction.equals(mxConstants.DIRECTION_SOUTH)) { triangle.addPoint(x, y); triangle.addPoint(x + w / 2, y + h); triangle.addPoint(x + w, y); } else if (direction.equals(mxConstants.DIRECTION_WEST)) { triangle.addPoint(x + w, y); triangle.addPoint(x, y + h / 2); triangle.addPoint(x + w, y + h); } else // east { triangle.addPoint(x, y); triangle.addPoint(x + w, y + h / 2); triangle.addPoint(x, y + h); } drawPolygon(triangle, fillColor, fillPaint, penColor, shadow); } /** * Draws a hexagon shape for the given parameters. * * @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 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 drawHexagon(int x, int y, int w, int h, Color fillColor, Paint fillPaint, Color penColor, boolean shadow, String direction) { Polygon hexagon = new Polygon(); if (direction.equals(mxConstants.DIRECTION_NORTH) || direction.equals(mxConstants.DIRECTION_SOUTH)) { hexagon.addPoint(x + (int) (0.5 * w), y); hexagon.addPoint(x + w, y + (int) (0.25 * h)); hexagon.addPoint(x + w, y + (int) (0.75 * h)); hexagon.addPoint(x + (int) (0.5 * w), y + h); hexagon.addPoint(x, y + (int) (0.75 * h)); hexagon.addPoint(x, y + (int) (0.25 * h)); } else { hexagon.addPoint(x + (int) (0.25 * w), y); hexagon.addPoint(x + (int) (0.75 * w), y); hexagon.addPoint(x + w, y + (int) (0.5 * h)); hexagon.addPoint(x + (int) (0.75 * w), y + h); hexagon.addPoint(x + (int) (0.25 * w), y + h); hexagon.addPoint(x, y + (int) (0.5 * h)); } drawPolygon(hexagon, fillColor, fillPaint, penColor, shadow); } /** * Computes the arc size for the given dimension. * * @param w Width of the rectangle. * @param h Height of the rectangle. * @return Returns the arc size for the given dimension. */ public static int getArcSize(int w, int h) { int arcSize; if (w <= h) { arcSize = h / 5; if (arcSize > (w / 2)) { arcSize = w / 2; } } else { arcSize = w / 5; if (arcSize > (h / 2)) { arcSize = h / 2; } } return arcSize; } /** * * @param p0 * @param p1 * @param scale * @return */ protected Polygon createArrow(mxPoint p0, mxPoint pe) { // Geometry of arrow double spacing = mxConstants.ARROW_SPACING * scale; double width = mxConstants.ARROW_WIDTH * scale; double arrow = mxConstants.ARROW_SIZE * scale; double dx = pe.getX() - p0.getX(); double dy = pe.getY() - p0.getY(); double dist = Math.sqrt(dx * dx + dy * dy); double length = dist - 2 * spacing - arrow; // Computes the norm and the inverse norm double nx = dx / dist; double ny = dy / dist; double basex = length * nx; double basey = length * ny; double floorx = width * ny / 3; double floory = -width * nx / 3; // Computes points double p0x = p0.getX() - floorx / 2 + spacing * nx; double p0y = p0.getY() - floory / 2 + spacing * ny; double p1x = p0x + floorx; double p1y = p0y + floory; double p2x = p1x + basex; double p2y = p1y + basey; double p3x = p2x + floorx; double p3y = p2y + floory; // p4 not required double p5x = p3x - 3 * floorx; double p5y = p3y - 3 * floory; Polygon poly = new Polygon(); poly.addPoint((int) Math.round(p0x), (int) Math.round(p0y)); poly.addPoint((int) Math.round(p1x), (int) Math.round(p1y)); poly.addPoint((int) Math.round(p2x), (int) Math.round(p2y)); poly.addPoint((int) Math.round(p3x), (int) Math.round(p3y)); poly.addPoint((int) Math.round(pe.getX() - spacing * nx), (int) Math .round(pe.getY() - spacing * ny)); poly.addPoint((int) Math.round(p5x), (int) Math.round(p5y)); poly.addPoint((int) Math.round(p5x + floorx), (int) Math.round(p5y + floory)); return poly; } /** * Draws the given arrow shape. * * @param pts List of points that define the line. */ protected void drawArrow(List pts, Color fillColor, Paint fillPaint, Color penColor, boolean shadow) { // Base vector (between end points) mxPoint p0 = (mxPoint) pts.get(0); mxPoint pe = (mxPoint) pts.get(pts.size() - 1); Polygon poly = createArrow(p0, pe); if (g.getClipBounds() == null || g.getClipBounds().intersects(poly.getBounds())) { drawPolygon(poly, fillColor, fillPaint, penColor, shadow); } } /** * Draws the given connector shape. *
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -