/** Add commands for this glyph to a page */ public Point2D addCommands(PDFPage cmds, AffineTransform transform, int mode) { if (shape != null) { GeneralPath outline = (GeneralPath) shape.createTransformedShape(transform); cmds.addCommand(new PDFShapeCmd(outline, mode)); } else if (page != null) { cmds.addCommands(page, transform); } return advance; }
public static Shape generatePolygon( int sides, int outsideRadius, int insideRadius, boolean normalize) { Shape shape = generatePolygon(sides, outsideRadius, insideRadius); if (normalize) { Rectangle2D bounds = shape.getBounds2D(); GeneralPath path = new GeneralPath(shape); shape = path.createTransformedShape( AffineTransform.getTranslateInstance(-bounds.getX(), -bounds.getY())); } return shape; }
public void paint(Graphics g) { Graphics2D g2d = (Graphics2D) g; g2d.setColor(Color.BLACK); g2d.drawString(name, 10, 10); g2d.setColor(Color.RED); g2d.drawString("Opt", 10, 50); g2d.draw(gp1); g2d.setColor(Color.BLUE); g2d.drawString("Normal", 10, 100); g2d.draw(gp2.createTransformedShape(AffineTransform.getTranslateInstance(2, 2))); }
/** * if the edge is reflexive its painted as a cyclic edge if there are 2 controlpoints the * connection is painted as a straight line from the source to the targetanchor if there are more * as 2 controlpoints the connection path between 2 control points is painted as bezier curve */ @Override protected void paintWidget() { List<Point> contrPoints = this.getControlPoints(); int listSize = contrPoints.size(); Graphics2D gr = getGraphics(); if (listSize <= 2) { if (isReflexive()) { // special case for reflexive connection widgets Widget related = this.getTargetAnchor().getRelatedWidget(); int position = this.edgeBalance(related); Rectangle bounds = related.convertLocalToScene(related.getBounds()); gr.setColor(getLineColor()); Point first = new Point(); Point last = new Point(); double centerX = bounds.getCenterX(); first.x = (int) (centerX + bounds.width / 4); first.y = bounds.y + bounds.height; last.x = first.x; last.y = bounds.y; gr.setStroke(this.getStroke()); double cutDistance = this.getTargetAnchorShape().getCutDistance(); double anchorAngle = Math.PI / -3.0; double cutX = Math.abs(Math.cos(anchorAngle) * cutDistance); double cutY = Math.abs(Math.sin(anchorAngle) * cutDistance); int ydiff = first.y - last.y; int endy = -ydiff; double height = bounds.getHeight(); double cy = height / 4.0; double cx = bounds.getWidth() / 5.0; double dcx = cx * 2; GeneralPath gp = new GeneralPath(); gp.moveTo(0, 0); gp.quadTo(0, cy, cx, cy); gp.quadTo(dcx, cy, dcx, -height / 2.0); gp.quadTo(dcx, endy - cy, cy, -(cy + ydiff)); gp.quadTo(cutX * 1.5, endy - cy, cutX, endy - cutY); AffineTransform af = new AffineTransform(); AnchorShape anchorShape = this.getTargetAnchorShape(); if (position < 0) { first.x = (int) (centerX - bounds.width / 4); af.translate(first.x, first.y); af.scale(-1.0, 1.0); last.x = first.x; } else { af.translate(first.x, first.y); } Shape s = gp.createTransformedShape(af); gr.draw(s); if (last != null) { AffineTransform previousTransform = gr.getTransform(); gr.translate(last.x, last.y); if (position < 0) gr.rotate(Math.PI - anchorAngle); else gr.rotate(anchorAngle); anchorShape.paint(gr, false); gr.setTransform(previousTransform); } } else { super.paintWidget(); } return; } // bezier curve... GeneralPath curvePath = new GeneralPath(); Point lastControlPoint = null; double lastControlPointRotation = 0.0; Point prev = null; for (int i = 0; i < listSize - 1; i++) { Point cur = contrPoints.get(i); Point next = contrPoints.get(i + 1); Point nextnext = null; if (i < listSize - 2) { nextnext = contrPoints.get(i + 2); } double len = cur.distance(next); double scale = len * BEZIER_SCALE; Point bezierFrom = null; // first ControlPoint Point bezierTo = null; // second ControlPoint if (prev == null) { // first point curvePath.moveTo(cur.x, cur.y); // startpoint bezierFrom = cur; } else { bezierFrom = new Point(next.x - prev.x, next.y - prev.y); bezierFrom = scaleVector(bezierFrom, scale); bezierFrom.translate(cur.x, cur.y); } if (nextnext == null) { // next== last point (curve to) lastControlPoint = next; bezierTo = next; // set 2nd intermediate point to endpoint GeneralPath lastseg = this.subdivide(cur, bezierFrom, bezierTo, next); if (lastseg != null) curvePath.append(lastseg, true); break; } else { bezierTo = new Point(cur.x - nextnext.x, cur.y - nextnext.y); bezierTo = scaleVector(bezierTo, scale); bezierTo.translate(next.x, next.y); } curvePath.curveTo( bezierFrom.x, bezierFrom.y, // controlPoint1 bezierTo.x, bezierTo.y, // controlPoint2 next.x, next.y); prev = cur; } Point2D cur = curvePath.getCurrentPoint(); Point next = lastControlPoint; lastControlPointRotation = // anchor anchorAngle Math.atan2(cur.getY() - next.y, cur.getX() - next.x); Color previousColor = gr.getColor(); gr.setColor(getLineColor()); Stroke s = this.getStroke(); gr.setStroke(s); gr.setColor(this.getLineColor()); gr.draw(curvePath); AffineTransform previousTransform = gr.getTransform(); gr.translate(lastControlPoint.x, lastControlPoint.y); gr.rotate(lastControlPointRotation); AnchorShape targetAnchorShape = this.getTargetAnchorShape(); targetAnchorShape.paint(gr, false); gr.setTransform(previousTransform); // paint ControlPoints if enabled if (isPaintControlPoints()) { int last = listSize - 1; for (int index = 0; index <= last; index++) { Point point = contrPoints.get(index); previousTransform = gr.getTransform(); gr.translate(point.x, point.y); if (index == 0 || index == last) getEndPointShape().paint(gr); else getControlPointShape().paint(gr); gr.setTransform(previousTransform); } } gr.setColor(previousColor); }
/** * Draws the needle. * * @param g2 the graphics device. * @param plotArea the plot area. * @param rotate the rotation point. * @param angle the angle. */ protected void drawNeedle(Graphics2D g2, Rectangle2D plotArea, Point2D rotate, double angle) { GeneralPath shape1 = new GeneralPath(); GeneralPath shape2 = new GeneralPath(); GeneralPath shape3 = new GeneralPath(); float minX = (float) plotArea.getMinX(); float minY = (float) plotArea.getMinY(); float maxX = (float) plotArea.getMaxX(); float maxY = (float) plotArea.getMaxY(); // float midX = (float) (minX + (plotArea.getWidth() * getRotateX())); // float midY = (float) (minY + (plotArea.getHeight() * getRotateY())); float midX = (float) (minX + (plotArea.getWidth() * 0.5)); float midY = (float) (minY + (plotArea.getHeight() * 0.8)); float y = maxY - (2 * (maxY - midY)); if (y < minY) { y = minY; } shape1.moveTo(minX, midY); shape1.lineTo(midX, minY); shape1.lineTo(midX, y); shape1.closePath(); shape2.moveTo(maxX, midY); shape2.lineTo(midX, minY); shape2.lineTo(midX, y); shape2.closePath(); shape3.moveTo(minX, midY); shape3.lineTo(midX, maxY); shape3.lineTo(maxX, midY); shape3.lineTo(midX, y); shape3.closePath(); Shape s1 = shape1; Shape s2 = shape2; Shape s3 = shape3; if ((rotate != null) && (angle != 0)) { /// we have rotation huston, please spin me getTransform().setToRotation(angle, rotate.getX(), rotate.getY()); s1 = shape1.createTransformedShape(transform); s2 = shape2.createTransformedShape(transform); s3 = shape3.createTransformedShape(transform); } if (getHighlightPaint() != null) { g2.setPaint(getHighlightPaint()); g2.fill(s3); } if (getFillPaint() != null) { g2.setPaint(getFillPaint()); g2.fill(s1); g2.fill(s2); } if (getOutlinePaint() != null) { g2.setStroke(getOutlineStroke()); g2.setPaint(getOutlinePaint()); g2.draw(s1); g2.draw(s2); g2.draw(s3); } }