/** * Writes the <code>shape</code>, <code>coords</code>, <code>href</code>, * <code>nohref</code> Attribute for the specified figure and shape. * * @return Returns true, if the polygon is inside of the image bounds. */ private boolean writePolyAttributes(IXMLElement elem, SVGFigure f, Shape shape) { AffineTransform t = TRANSFORM.getClone(f); if (t == null) { t = drawingTransform; } else { t.preConcatenate(drawingTransform); } StringBuilder buf = new StringBuilder(); float[] coords = new float[6]; GeneralPath path = new GeneralPath(); for (PathIterator i = shape.getPathIterator(t, 1.5f); ! i.isDone(); i.next()) { switch (i.currentSegment(coords)) { case PathIterator.SEG_MOVETO : if (buf.length() != 0) { throw new IllegalArgumentException("Illegal shape "+shape); } if (buf.length() != 0) { buf.append(','); } buf.append((int) coords[0]); buf.append(','); buf.append((int) coords[1]); path.moveTo(coords[0], coords[1]); break; case PathIterator.SEG_LINETO : if (buf.length() != 0) { buf.append(','); } buf.append((int) coords[0]); buf.append(','); buf.append((int) coords[1]); path.lineTo(coords[0], coords[1]); break; case PathIterator.SEG_CLOSE : path.closePath(); break; default : throw new InternalError("Illegal segment type "+i.currentSegment(coords)); } } elem.setAttribute("shape", "poly"); elem.setAttribute("coords", buf.toString()); writeHrefAttribute(elem, f); return path.intersects(new Rectangle2D.Float(bounds.x, bounds.y, bounds.width, bounds.height)); }
/** * Build a shape for the entire subtree by joining together the shapes for each of its edges. * Vertices included since needed for FullTextPanel. */ public Shape constructInternalShape(DiagramBase diagram, boolean includeVertices) { GeneralPath shape = new GeneralPath(); Enumeration edges = m_edgeList.elements(); while (edges.hasMoreElements()) { TreeEdge edge = (TreeEdge) edges.nextElement(); Shape edgeShape = edge.getSchemeShape(diagram); PathIterator path = edgeShape.getPathIterator(null); shape.append(path, false); if (includeVertices) { Shape vertexShape; if (!edge.getSourceVertex().isVirtual()) { vertexShape = edge.getSourceVertex().getShape(diagram); path = vertexShape.getPathIterator(null); shape.append(path, false); } if (!edge.getDestVertex().isVirtual()) { vertexShape = edge.getDestVertex().getShape(diagram); path = vertexShape.getPathIterator(null); shape.append(path, false); } } } BasicStroke stroke = new BasicStroke( diagram.getSubtreeLineWidth() - DiagramBase.EDGE_OUTLINE_WIDTH + 1, BasicStroke.CAP_ROUND, BasicStroke.JOIN_MITER); internalShapeTable.put(diagram, stroke.createStrokedShape(shape)); return (Shape) internalShapeTable.get(diagram); }
public Shape getShape() { GeneralPath gp = new GeneralPath(); gp.moveTo((float) d.getPosition().getX(), (float) d.getPosition().getY()); for (Node n : Arrays.asList(dt, tv, v)) { gp.lineTo((float) n.getPosition().getX(), (float) n.getPosition().getY()); } gp.closePath(); return gp; }
void draw(Graphics2D g) { // toX/toY is tip of arrow and fx/fy is a point on the line - // fx/fy is used to determine direction & angle AffineTransform at = AffineTransform.getTranslateInstance(toX, toY); int b = 9; double theta = Math.toRadians(20); // The idea of using a GeneralPath is so we can // create the (three lines that make up the) arrow // (only) one time and then use AffineTransform to // place it anywhere we want. GeneralPath path = new GeneralPath(); // distance between line and the arrow mark <** not ** // Start a new line segment from the position of (0,0). path.moveTo(0, 0); // Create one of the two arrow head lines. int x = (int) (-b * Math.cos(theta)); int y = (int) (b * Math.sin(theta)); path.lineTo(x, y); // distance between line and the arrow mark <** not ** // Make the other arrow head line. int x2 = (int) (-b * Math.cos(-theta)); int y2 = (int) (b * Math.sin(-theta)); // path.moveTo(0,0); path.lineTo(x2, y2); path.closePath(); // theta is in radians double s, t; s = toY - fy; // calculate slopes. t = toX - fx; if (t != 0) { s = s / t; theta = Math.atan(s); if (t < 0) theta += Math.PI; } else if (s < 0) theta = -(Math.PI / 2); else theta = Math.PI / 2; at.rotate(theta); // at.rotate(theta,toX,toY); Shape shape = at.createTransformedShape(path); if (checkStatus == Status.UNCHECKED) g.setColor(Color.BLACK); else if (checkStatus == Status.COMPATIBLE) g.setColor(FOREST_GREEN); else g.setColor(ORANGE_RED); g.fill(shape); g.draw(shape); }
void drawCrosses() { GeneralPath path = new GeneralPath(); float arm = 5; for (int h = 0; h < linesV; h++) { for (int v = 0; v < linesH; v++) { float x = (float) (xstart + h * tileWidth); float y = (float) (ystart + v * tileHeight); path.moveTo(x - arm, y); path.lineTo(x + arm, y); path.moveTo(x, y - arm); path.lineTo(x, y + arm); } } showGrid(path); }
void drawLines() { GeneralPath path = new GeneralPath(); int width = imp.getWidth(); int height = imp.getHeight(); for (int i = 0; i < linesV; i++) { float xoff = (float) (xstart + i * tileWidth); path.moveTo(xoff, 0f); path.lineTo(xoff, height); } for (int i = 0; i < linesH; i++) { float yoff = (float) (ystart + i * tileHeight); path.moveTo(0f, yoff); path.lineTo(width, yoff); } showGrid(path); }
public void setAdvance(float adv) { advance = adv; advp = new GeneralPath(); advp.moveTo(-2, -2); advp.lineTo(2, 2); advp.moveTo(-2, 2); advp.lineTo(2, -2); advp.moveTo(adv - 2, -2); advp.lineTo(adv, 0); advp.lineTo(adv + 2, -2); advp.moveTo(adv, 0); advp.lineTo(adv, -8); }
public void curveTo(float x1, float y1, float x2, float y2, float x3, float y3) { gp.curveTo(x1, y1, x2, y2, x3, y3); this.x = x3; this.y = y3; points.add(new GlyphPoint(x1, y1, true)); points.add(new GlyphPoint(x2, y2, true)); points.add(new GlyphPoint(x3, y3, false)); }
/** Build a shape for the entire subtree by joining together the shapes for each of its edges. */ public Shape constructShape(DiagramBase diagram) { GeneralPath shape = new GeneralPath(); Enumeration edges = m_edgeList.elements(); while (edges.hasMoreElements()) { TreeEdge edge = (TreeEdge) edges.nextElement(); if (!edge.visible) { continue; } Shape edgeShape = edge.getSchemeShape(diagram); PathIterator path = edgeShape.getPathIterator(null); shape.append(path, false); } BasicStroke stroke = new BasicStroke( diagram.getSubtreeLineWidth(), BasicStroke.CAP_ROUND, BasicStroke.JOIN_MITER); shapeTable.put(diagram, stroke.createStrokedShape(shape)); return (Shape) shapeTable.get(diagram); }
/** * converts this glyph into Shape. It could be called for root's preview mode or by include * invoke. Pushing either this GlyphFile or DIncludeInvoke should be handled before this. */ public Shape toShape(AffineTransform a_trans) { int ppem = k_defaultPixelSize; GeneralPath retval = new GeneralPath(); Iterator i = createIterator(); while (i.hasNext()) { GlyphObject object = (GlyphObject) i.next(); if (object instanceof EContourPoint || object instanceof EHint) { continue; } // if retval.append(object.toShape(a_trans, ppem), false); } // if return retval; }
/** * method to draw the tail of the balloon * * @param ellipseHeight the height of the ellipse * @param fillColor the color to bill the balloon with * @param outlineColor the color to outline the balloon with * @param g2 the 2d graphics context */ protected void drawTail(int ellipseHeight, Color fillColor, Color outlineColor, Graphics2D g2) { Point tailEnd = getTailEnd(); Point upperLeft = getUpperLeft(); int margin = getMargin(); int halfWidth = getWidth() / 2; int topY = upperLeft.y + ellipseHeight; int startX = upperLeft.x + halfWidth - margin; int endX = upperLeft.x + halfWidth + margin; GeneralPath triangle = new GeneralPath(GeneralPath.WIND_EVEN_ODD, 3); triangle.moveTo(startX, topY); triangle.lineTo(endX, topY); triangle.lineTo(tailEnd.x, tailEnd.y); triangle.lineTo(startX, topY); g2.setColor(fillColor); g2.fill(triangle); g2.setColor(outlineColor); g2.draw(new Line2D.Double(tailEnd.x, tailEnd.y, startX, topY)); g2.draw(new Line2D.Double(tailEnd.x, tailEnd.y, endX, topY)); }
/** Paints the <tt>visualEdge</tt>. No arrowhead is drawn. */ public void paint(VisualGraphComponent component, Graphics2D g2d) { VisualEdge vEdge = (VisualEdge) component; Rectangle fromvertexBounds; Rectangle tovertexBounds; GeneralPath drawPath; VisualVertex visualVertexA = vEdge.getVisualVertexA(); VisualVertex visualVertexB = vEdge.getVisualVertexB(); GraphLayoutManager layoutmanager = vEdge.getVisualGraph().getGraphLayoutManager(); drawPath = vEdge.getGeneralPath(); // If there is no layoutmanager or there is one but the layout has not // been initialised, by default, let us route edges as straight lines. if (layoutmanager == null || (layoutmanager != null && !layoutmanager.isInitialized())) { fromvertexBounds = visualVertexA.getBounds(); tovertexBounds = visualVertexB.getBounds(); // Make sure to clear the GeneralPath() first. Otherwise, the edge's previous // path will be redrawn as well. drawPath.reset(); // Start the line from the center of the vEdgertex drawPath.moveTo((float) fromvertexBounds.getCenterX(), (float) fromvertexBounds.getCenterY()); drawPath.lineTo((float) tovertexBounds.getCenterX(), (float) tovertexBounds.getCenterY()); } else { // Let the layout manager determine how the edge will be routed. layoutmanager.routeEdge(g2d, vEdge); } // Draw the line g2d.setColor(vEdge.getOutlinecolor()); g2d.draw(drawPath); // Draw the edge label this.paintText(vEdge, g2d); }
void drawPoints() { int one = 1; int two = 2; GeneralPath path = new GeneralPath(); for (int h = 0; h < linesV; h++) { for (int v = 0; v < linesH; v++) { float x = (float) (xstart + h * tileWidth); float y = (float) (ystart + v * tileHeight); path.moveTo(x - two, y - one); path.lineTo(x - two, y + one); path.moveTo(x + two, y - one); path.lineTo(x + two, y + one); path.moveTo(x - one, y - two); path.lineTo(x + one, y - two); path.moveTo(x - one, y + two); path.lineTo(x + one, y + two); } } showGrid(path); }
/** Build a shape for the entire subtree by joining together the shapes for each of its edges. */ public Shape constructFullTextShape(DiagramBase diagram) { GeneralPath shape = new GeneralPath(); Enumeration edges = m_edgeList.elements(); while (edges.hasMoreElements()) { TreeEdge edge = (TreeEdge) edges.nextElement(); if (!edge.visible) { continue; } Shape edgeShape = edge.getSchemeShape(diagram); PathIterator path = edgeShape.getPathIterator(null); shape.append(path, false); TreeVertex vertex = edge.getSourceVertex(); if (!vertex.isVirtual()) { shape.append(vertex.getShape(diagram).getPathIterator(null), false); } vertex = edge.getDestVertex(); if (!vertex.isVirtual()) { shape.append(vertex.getShape(diagram).getPathIterator(null), false); } } BasicStroke stroke = new BasicStroke(20, BasicStroke.CAP_ROUND, BasicStroke.JOIN_MITER); shapeTable.put(diagram, stroke.createStrokedShape(shape)); return (Shape) shapeTable.get(diagram); }
public GlyphPoint(float x, float y, boolean curvectrl) { this.x = x; this.y = y; this.curvecontrol = curvectrl; gp = new GeneralPath(); if (curvectrl) { gp.moveTo(x - 4, y - 4); gp.lineTo(x + 4, y + 4); gp.moveTo(x - 4, y + 4); gp.lineTo(x + 4, y - 4); } else { gp.moveTo(x - 4, y - 4); gp.lineTo(x - 4, y + 4); gp.lineTo(x + 4, y + 4); gp.lineTo(x + 4, y - 4); gp.closePath(); } }
public Shape getShape() { int R = Integer.parseInt(hexcolor.substring(0, 2), 16); int G = Integer.parseInt(hexcolor.substring(2, 4), 16); int B = Integer.parseInt(hexcolor.substring(4, 6), 16); color = new Color(R, G, B); Shape geom; if (shape.equals("circle")) { geom = new Ellipse2D.Float(-0.05f * size, -0.05f * size, 1.1f * size, 1.1f * size); center = new Point2D.Float(size / 2, size / 2); } else if (shape.equals("square")) { geom = new Rectangle2D.Float(0, 0, size, size); center = new Point2D.Float(size / 2, size / 2); } else if (shape.equals("roundsquare")) { geom = new RoundRectangle2D.Float(0, 0, size, size, size / 2, size / 2); center = new Point2D.Float(size / 2, size / 2); } else if (shape.equals("triangle")) { GeneralPath path = new GeneralPath(GeneralPath.WIND_EVEN_ODD, 2); float height = (float) (Math.sqrt(3) * size / 2); path.moveTo(size / 2, size - height); path.lineTo(size, size); path.lineTo(0, size); path.closePath(); geom = path; center = new Point2D.Float(size / 2, size - height / 2); } else if (shape.equals("star")) { GeneralPath path = new GeneralPath(GeneralPath.WIND_EVEN_ODD, 2); path.moveTo(size / 2, 0); path.lineTo(7 * size / 10, 3 * size / 10); path.lineTo(size, size / 2); path.lineTo(7 * size / 10, 7 * size / 10); path.lineTo(size / 2, size); path.lineTo(3 * size / 10, 7 * size / 10); path.lineTo(0, size / 2); path.lineTo(3 * size / 10, 3 * size / 10); path.closePath(); AffineTransform trans = AffineTransform.getRotateInstance(Math.PI / 4, size / 2, size / 2); Shape shape = trans.createTransformedShape(path); Area area = new Area(path); area.add(new Area(shape)); trans = AffineTransform.getScaleInstance(1.2, 1.2); shape = trans.createTransformedShape(area); trans = AffineTransform.getTranslateInstance(-0.1 * size, -0.1 * size); shape = trans.createTransformedShape(shape); geom = shape; center = new Point2D.Float(size / 2, size / 2); } else if (shape.equals("octagon")) { GeneralPath path = new GeneralPath(GeneralPath.WIND_EVEN_ODD, 2); path.moveTo(size / 4, 0); path.lineTo(3 * size / 4, 0); path.lineTo(size, size / 4); path.lineTo(size, 3 * size / 4); path.lineTo(3 * size / 4, size); path.lineTo(size / 4, size); path.lineTo(0, 3 * size / 4); path.lineTo(0, size / 4); path.closePath(); geom = path; path.closePath(); geom = path; center = new Point2D.Float(size / 2, size / 2); } else if (shape.equals("ellipse")) { geom = new Ellipse2D.Float(0, 0, 3 * size / 4, size); center = new Point2D.Float(3 * size / 8, size / 2); } else if (shape.equals("cross")) { GeneralPath path = new GeneralPath(GeneralPath.WIND_EVEN_ODD, 2); path.moveTo(size / 4, 0); path.lineTo(3 * size / 4, 0); path.lineTo(3 * size / 4, size / 4); path.lineTo(size, size / 4); path.lineTo(size, 3 * size / 4); path.lineTo(3 * size / 4, 3 * size / 4); path.lineTo(3 * size / 4, size); path.lineTo(size / 4, size); path.lineTo(size / 4, 3 * size / 4); path.lineTo(0, 3 * size / 4); path.lineTo(0, size / 4); path.lineTo(size / 4, size / 4); path.closePath(); geom = path; center = new Point2D.Float(size / 2, size / 2); } else if (shape.equals("pentagon")) { GeneralPath path = new GeneralPath(GeneralPath.WIND_EVEN_ODD, 2); path.moveTo(size / 2, size); float x, y, a; for (int i = 1; i < 5; i++) { a = (float) ((2 * Math.PI / 5) * i); x = (float) (size / 2 + size / 2 * Math.sin(a)); y = (float) (size / 2 + size / 2 * Math.cos(a)); path.lineTo(x, y); } path.closePath(); AffineTransform trans = AffineTransform.getScaleInstance(1.1, 1.1); Shape shape = trans.createTransformedShape(path); trans = AffineTransform.getTranslateInstance(-0.05 * size, -0.05 * size); shape = trans.createTransformedShape(shape); geom = shape; center = new Point2D.Float(size / 2, size / 2); } else if (shape.equals("hexagon")) { GeneralPath path = new GeneralPath(GeneralPath.WIND_EVEN_ODD, 2); path.moveTo(size / 2, size); float x, y, a; for (int i = 1; i < 6; i++) { a = (float) ((2 * Math.PI / 6) * i); x = (float) (size / 2 + size / 2 * Math.sin(a)); y = (float) (size / 2 + size / 2 * Math.cos(a)); path.lineTo(x, y); } path.closePath(); geom = path; center = new Point2D.Float(size / 2, size / 2); } else { geom = new Ellipse2D.Float(0, 0, size, size); center = new Point2D.Float(size / 2, size / 2); } return geom; }
/** * Wrapper method around the <tt>paintText()</tt> method of the <tt>VisualEdgePainter</tt> * interface. This method performs the calculation to determine the position where the text will * be drawn. */ private void paintText(VisualEdge vEdge, Graphics2D g2d) { Point fromPoint = new Point(); Point toPoint = new Point(); GeneralPath gPath = vEdge.getGeneralPath(); PathIterator iterator = gPath.getPathIterator(null); FontMetrics fontMetrics; float edgeSegment[] = new float[6]; double currentLength = 0; float cumulativeLength = 0; float x1 = 0, y1 = 0, x2 = 0, y2 = 0; int segmentType; boolean firstPointInitialized = false; // Get the total length of the edge float edgeLength = vEdge.getEdgeLength(vEdge, fromPoint, toPoint); while (!iterator.isDone()) { segmentType = iterator.currentSegment(edgeSegment); switch (segmentType) { case PathIterator.SEG_LINETO: case PathIterator.SEG_MOVETO: x2 = edgeSegment[0]; y2 = edgeSegment[1]; break; case PathIterator.SEG_QUADTO: x2 = edgeSegment[2]; y2 = edgeSegment[3]; break; case PathIterator.SEG_CUBICTO: x2 = edgeSegment[4]; y2 = edgeSegment[5]; } if (firstPointInitialized) { currentLength = Point2D.distance(x1, y1, x2, y2); cumulativeLength += currentLength; } iterator.next(); // If we are halfway through the length of the edge, // then paint the text if (cumulativeLength >= (edgeLength / 2) || cumulativeLength >= edgeLength) { // Ratio of the remaining half-length over the length of the current edge double ratio = ((edgeLength / 2) - (cumulativeLength - currentLength)) / currentLength; fontMetrics = vEdge.getFontMetrics(); // Take into account the text's length this.paintText( g2d, vEdge.getFont(), vEdge.getFontcolor(), vEdge.getLabel(), (float) (fromPoint.getX() < toPoint.getX() ? (x1 + (Math.abs(x2 - x1) * ratio)) : (x1 - (Math.abs(x2 - x1) * ratio))) - fontMetrics.stringWidth(vEdge.getLabel()) / 2, (float) (fromPoint.getY() < toPoint.getY() ? (y1 + (Math.abs(y2 - y1) * ratio)) : (y1 - (Math.abs(y2 - y1) * ratio)))); break; } x1 = x2; y1 = y2; if (!firstPointInitialized) { firstPointInitialized = true; } } }
public Shape createStrokedShape(Shape shape) { GeneralPath result = new GeneralPath(); PathIterator it = new FlatteningPathIterator(shape.getPathIterator(null), FLATNESS); float points[] = new float[6]; float moveX = 0, moveY = 0; float lastX = 0, lastY = 0; float thisX = 0, thisY = 0; int type = 0; boolean first = false; float next = 0; int phase = 0; float factor = 1; while (!it.isDone()) { type = it.currentSegment(points); switch (type) { case PathIterator.SEG_MOVETO: moveX = lastX = points[0]; moveY = lastY = points[1]; result.moveTo(moveX, moveY); first = true; next = wavelength / 2; break; case PathIterator.SEG_CLOSE: points[0] = moveX; points[1] = moveY; // Fall into.... case PathIterator.SEG_LINETO: thisX = points[0]; thisY = points[1]; float dx = thisX - lastX; float dy = thisY - lastY; float distance = (float) Math.sqrt(dx * dx + dy * dy); if (distance >= next) { float r = 1.0f / distance; float angle = (float) Math.atan2(dy, dx); while (distance >= next) { float x = lastX + next * dx * r; float y = lastY + next * dy * r; float tx = amplitude * dy * r; float ty = amplitude * dx * r; if ((phase & 1) == 0) result.lineTo(x + amplitude * dy * r, y - amplitude * dx * r); else result.lineTo(x - amplitude * dy * r, y + amplitude * dx * r); next += wavelength; phase++; } } next -= distance; first = false; lastX = thisX; lastY = thisY; if (type == PathIterator.SEG_CLOSE) result.closePath(); break; } it.next(); } // return stroke.createStrokedShape( result ); return result; }
public void closePath() { gp.closePath(); }
public void moveTo(float x, float y) { gp.moveTo(x, y); this.x = x; this.y = y; points.add(new GlyphPoint(x, y, false)); }