/** * 获得图片的矩形大小 * * @return */ private Rectangle getCanvasDimension(JpdlModel jpdlModel) { Rectangle rectangle = new Rectangle(); Rectangle rect; for (Node node : jpdlModel.getNodes().values()) { rect = node.getRectangle(); if (rect.getMaxX() > rectangle.getMaxX()) { rectangle.width = (int) rect.getMaxX(); } if (rect.getMaxY() > rectangle.getMaxY()) { rectangle.height = (int) rect.getMaxY(); } for (Transition transition : node.getTransitions()) { List<Point> trace = transition.getLineTrace(); for (Point point : trace) { if (rectangle.getMaxX() < point.x) { rectangle.width = point.x; } if (rectangle.getMaxY() < point.y) { rectangle.height = point.y; } } } } rectangle.width += 60; rectangle.height += 20; return rectangle; }
/** * @param g2 * @throws IOException */ private void drawTransition(Map<String, Node> nodes, Graphics2D g2) throws IOException { g2.setStroke(DEFAULT_LINE_STROKE); g2.setColor(DEFAULT_LINE_STROKE_COLOR); for (Node node : nodes.values()) { for (Transition transition : node.getTransitions()) { String to = transition.getTo(); Node toNode = nodes.get(to); List<Point> trace = new LinkedList<Point>(transition.getLineTrace()); int len = trace.size() + 2; trace.add(0, new Point(node.getCenterX(), node.getCenterY())); trace.add(new Point(toNode.getCenterX(), toNode.getCenterY())); int[] xPoints = new int[len]; int[] yPoints = new int[len]; for (int i = 0; i < len; i++) { xPoints[i] = trace.get(i).x; yPoints[i] = trace.get(i).y; } final int taskGrow = 4; final int smallGrow = -2; int grow = 0; if (nodeInfos.get(node.getType()) != null) { grow = smallGrow; } else { grow = taskGrow; } Point p = GeometryUtils.getRectangleLineCrossPoint(node.getRectangle(), new Point(xPoints[1], yPoints[1]), grow); if (p != null) { xPoints[0] = p.x; yPoints[0] = p.y; } if (nodeInfos.get(toNode.getType()) != null) { grow = smallGrow; } else { grow = taskGrow; } p = GeometryUtils.getRectangleLineCrossPoint(toNode.getRectangle(), new Point(xPoints[len - 2], yPoints[len - 2]), grow); if (p != null) { xPoints[len - 1] = p.x; yPoints[len - 1] = p.y; } g2.drawPolyline(xPoints, yPoints, len); drawArrow(g2, xPoints[len - 2], yPoints[len - 2], xPoints[len - 1], yPoints[len - 1]); String label = transition.getLabel(); if (label != null && label.length() > 0) { int cx, cy; if (len % 2 == 0) { cx = (xPoints[len / 2 - 1] + xPoints[len / 2]) / 2; cy = (yPoints[len / 2 - 1] + yPoints[len / 2]) / 2; } else { cx = xPoints[len / 2]; cy = yPoints[len / 2]; } Point labelPoint = transition.getLabelPosition(); if (labelPoint != null) { cx += labelPoint.x; cy += labelPoint.y; } cy -= RECT_OFFSET_Y + RECT_OFFSET_Y / 2; g2.drawString(label, cx, cy); } } } }