@Override public void doFillShape(ResizablePolygonAttribute polygonAttr, Graphics graphics, Rectangle bounds) throws IllegalActionException { PointList pList = getPolygonPoints(polygonAttr); Dimension dim = bounds.getSize(); Point tlp = bounds.getTopLeft(); Dimension rawDim = getRawBounds(pList).getSize(); // Ptolemy scales x and y potentially differently, depending on the ratios // of dim width & height and rawDim width & height respectively. double scaleWidth = dim.preciseWidth() / rawDim.preciseWidth(); double scaleHeight = dim.preciseHeight() / rawDim.preciseHeight(); Transform transform = new Transform(); transform.setScale(scaleWidth, scaleHeight); pList = getTransformedPolygon(transform, pList); pList.translate(tlp); graphics.fillPolygon(pList); }
@Override public void doDrawBorder(ResizablePolygonAttribute polygonAttr, Graphics graphics, Rectangle bounds) throws IllegalActionException { PointList pList = getPolygonPoints(polygonAttr); Dimension dim = bounds.getSize(); Point tlp = bounds.getTopLeft(); Dimension rawDim = getRawBounds(pList).getSize(); // Ptolemy scales x and y potentially differently, depending on the ratios // of dim width & height and rawDim width & height respectively. double scaleWidth = dim.preciseWidth() / rawDim.preciseWidth(); double scaleHeight = dim.preciseHeight() / rawDim.preciseHeight(); Transform transform = new Transform(); transform.setScale(scaleWidth, scaleHeight); pList = getTransformedPolygon(transform, pList); pList.translate(tlp); graphics.drawPolygon(pList); }
@Override public void draw(ArrowAttribute arrowAttr, Graphics graphics, ResourceManager resourceManager) { Color fgColor = graphics.getForegroundColor(); Color bgColor = graphics.getBackgroundColor(); Color rgb = getSwtColor(arrowAttr.lineColor, resourceManager); if (rgb != null) { graphics.setForegroundColor(rgb); graphics.setBackgroundColor(rgb); } try { float lineWidth = (float) ((DoubleToken) arrowAttr.lineWidth.getToken()).doubleValue(); graphics.setLineWidthFloat(lineWidth); int x = (int) ((DoubleToken) arrowAttr.x.getToken()).doubleValue(); int y = (int) ((DoubleToken) arrowAttr.y.getToken()).doubleValue(); int width = (int) ((DoubleToken) arrowAttr.arrowWidth.getToken()).doubleValue(); int length = (int) ((DoubleToken) arrowAttr.arrowLength.getToken()).doubleValue(); int halfWidth = width/2; Point tlp = getTopLeftLocation(arrowAttr); Transform transform = new Transform(); transform.setRotation(Math.atan2(y, x)); PointList pList = new PointList(); pList.addPoint(0, halfWidth); pList.addPoint(length + 3, 0); pList.addPoint(length, halfWidth); pList.addPoint((int) Math.sqrt(x*x + y*y), halfWidth); pList.addPoint(length, halfWidth); pList.addPoint(length + 3, width); pList = getTransformedPolygon(transform, pList); pList.translate(tlp); graphics.fillPolygon(pList); graphics.drawPolygon(pList); } catch (IllegalActionException e) { LOGGER.error("Error reading dimensions for " + arrowAttr.getFullName(), e); } graphics.setForegroundColor(fgColor); graphics.setBackgroundColor(bgColor); }
/** * Applies the given transformation to all points in the pointList. * (really just filling a gap in the PointList API) * @param trf * @param pointList * @return the list of transformed points */ protected final PointList getTransformedPolygon(Transform trf, PointList pointList) { PointList result = new PointList(pointList.size()); for (int i=0; i<pointList.size(); ++i ) { result.addPoint(trf.getTransformed(pointList.getPoint(i))); } return result; }
/** * For a given angle, rotate the point list. * * @param angle * @param points * @return */ protected PointList rotatePoints(double angle, PointList points) { Transform t = new Transform(); t.setRotation(angle); Point center = new Point(getPreferredSize().width / 2, getPreferredSize().height / 2); PointList newEdges = TransformationHelper.rotatePoints(angle, edges, center); if (offset) newEdges.translate(t.getTransformed(new PrecisionPoint(DEFAULT_WIDTH * (RESIZEFACTOR - 1) / 2, -DEFAULT_WIDTH * (RESIZEFACTOR - 1) / 2))); return newEdges; }
/** * Apply a rotation to the edges PointList and set the rotated point list as the mainFigure's PointList. * * Rotation takes into consideration the fact that the center moves when the number of branches changes. */ public void rotate(double angle) { if (this.angle != angle) { this.angle = angle; // build the transformation Transform t = new Transform(); t.setRotation(angle); // build the new point list PointList newEdges = new PointList(); // this is the center of the figure in the edges pointlist. Point center = new Point(DEFAULT_WIDTH / 2, DEFAULT_HEIGHT * branchCount / 2); // this is the center of the figure in the rotated, real size, point list. Point centerScaledRotated = new Point(getPreferredSize().width / 2, getPreferredSize().height / 2); // rotate edges points around center to generate newEdges for (int i = 0; i < edges.size(); i++) { Point newPoint = t.getTransformed(new Point(edges.getPoint(i).x - center.x, edges.getPoint(i).y - center.y)); newEdges.addPoint(newPoint); } // move them to the center of the rotated figure newEdges.translate(centerScaledRotated.x, centerScaledRotated.y); mainFigure.setPoints(newEdges); // inform the edit part that we've rotated via anchor listeners. ((ChopboxAnchor) outgoingAnchor).ancestorMoved(this); } }
public void rotate(double angle) { // make it always point towards bottom if (Math.cos(angle)>0) angle -= Math.PI; Transform t = new Transform(); t.setRotation(angle); Point center = new Point(getPreferredSize().width / 2, getPreferredSize().height / 2); for (int j = 0; j < lines.size(); j++) { PointList points = (PointList) lines.get(j); Polyline line = (Polyline) rects.get(j); PointList newPoints = new PointList(); for (int i = 0; i < points.size(); i++) { Point newPoint = t.getTransformed(new Point(points.getPoint(i).x - center.x, points.getPoint(i).y - center.y)); //Point pt = new Point(newPoint.x - center.x, newPoint.y - center.y); newPoints.addPoint(newPoint); } newPoints.translate(center.x, center.y); line.setPoints(newPoints); } ((EllipseAnchor)outgoingAnchor).ancestorMoved(this); }
public static PointList rotatePoints(double angle, PointList points, Point center) { Transform t = new Transform(); t.setRotation(angle); PointList newEdges = new PointList(); for (int i = 0; i < points.size(); i++) { Point newPoint = t.getTransformed(new Point(points.getPoint(i).x - center.x, points.getPoint(i).y - center.y)); Point pt = new Point(center.x - newPoint.x, center.y - newPoint.y); newEdges.addPoint(pt); } return newEdges; }