List of usage examples for java.awt.geom AffineTransform getTranslateInstance
public static AffineTransform getTranslateInstance(double tx, double ty)
From source file:org.pentaho.reporting.engine.classic.core.modules.output.pageable.pdf.internal.PdfGraphics2D.java
/** * @see Graphics#drawImage(Image, int, int, int, int, int, int, int, int, Color, ImageObserver) *///from ww w. j a va 2 s . c o m @Override public boolean drawImage(final Image img, final int dx1, final int dy1, final int dx2, final int dy2, final int sx1, final int sy1, final int sx2, final int sy2, final Color bgcolor, final ImageObserver observer) { waitForImage(img); final double dwidth = (double) dx2 - dx1; final double dheight = (double) dy2 - dy1; final double swidth = (double) sx2 - sx1; final double sheight = (double) sy2 - sy1; // if either width or height is 0, then there is nothing to draw if (dwidth == 0 || dheight == 0 || swidth == 0 || sheight == 0) { return true; } final double scalex = dwidth / swidth; final double scaley = dheight / sheight; final double transx = sx1 * scalex; final double transy = sy1 * scaley; final AffineTransform tx = AffineTransform.getTranslateInstance(dx1 - transx, dy1 - transy); tx.scale(scalex, scaley); final BufferedImage mask = new BufferedImage(img.getWidth(observer), img.getHeight(observer), BufferedImage.TYPE_BYTE_BINARY); final Graphics g = mask.getGraphics(); g.fillRect(sx1, sy1, (int) swidth, (int) sheight); drawImage(img, mask, tx, null, observer); g.dispose(); return true; }
From source file:org.pentaho.reporting.engine.classic.core.modules.output.pageable.pdf.internal.PdfGraphics2D.java
private AffineTransform normalizeMatrix() { final double[] mx = new double[6]; AffineTransform result = AffineTransform.getTranslateInstance(0, 0); result.getMatrix(mx);//from w ww. jav a 2 s.com mx[3] = -1; mx[5] = height; result = new AffineTransform(mx); result.concatenate(transform); return result; }
From source file:org.pentaho.reporting.engine.classic.core.modules.output.pageable.pdf.internal.PdfGraphics2D.java
public boolean drawPdfImage(final com.lowagie.text.Image image, final Image img, AffineTransform xform, final ImageObserver obs) { if (img == null) { throw new NullPointerException("Image must not be null."); }//from w ww.j a va 2 s .c o m if (image == null) { throw new NullPointerException("Image must not be null."); } if (xform == null) { xform = AffineTransform.getTranslateInstance(0, 0); } xform.translate(0, img.getHeight(obs)); xform.scale(img.getWidth(obs), img.getHeight(obs)); final AffineTransform inverse = this.normalizeMatrix(); final AffineTransform flipper = FLIP_TRANSFORM; inverse.concatenate(xform); inverse.concatenate(flipper); try { final double[] mx = new double[6]; inverse.getMatrix(mx); if (currentFillGState != 255) { PdfGState gs = fillGState[255]; if (gs == null) { gs = new PdfGState(); gs.setFillOpacity(1); fillGState[255] = gs; } cb.setGState(gs); } cb.addImage(image, (float) mx[0], (float) mx[1], (float) mx[2], (float) mx[3], (float) mx[4], (float) mx[5]); } catch (Exception ex) { PdfGraphics2D.logger.error("Failed to draw the image: ", ex); // throw new IllegalArgumentException("Failed to draw the image"); } finally { if (currentFillGState != 255) { final PdfGState gs = fillGState[currentFillGState]; cb.setGState(gs); } } return true; }
From source file:org.rhwlab.dispim.nucleus.NucleusData.java
public Shape getShape(long slice, int dim, int bufW, int bufH) { //System.out.printf("Ellipsoid center = (%d,%d,%d)\n",this.xC,this.yC,this.zC); Ellipse2d e;/*from w w w. ja v a 2s. co m*/ switch (dim) { case 0: e = xPlaneEllipse((double) slice); break; case 1: e = yPlaneEllipse((double) slice); break; default: e = zPlaneEllipse((double) slice); break; } if (e != null) { //System.out.printf("%s dim=%d slice=%d\n",this.getName(),dim,slice); AffineTransform toOrigin = AffineTransform.getTranslateInstance(-e.x, -e.y); AffineTransform back = AffineTransform.getTranslateInstance(e.x, e.y); AffineTransform xform = AffineTransform.getRotateInstance(e.cosine, e.sine); int scrX = SingleSlicePanel.screenX(e.low, dim, bufW); int scrY = SingleSlicePanel.screenY(e.low, dim, bufH); int scrHighX = SingleSlicePanel.screenX(e.high, dim, bufW); int scrHighY = SingleSlicePanel.screenY(e.high, dim, bufH); Shape shape = new Ellipse2D.Double(scrX, scrY, scrHighX - scrX, scrHighY - scrY); shape = toOrigin.createTransformedShape(shape); shape = xform.createTransformedShape(shape); shape = back.createTransformedShape(shape); //System.out.printf("e.a:%f e.b:%f e.x:%f e.y:%f\n",e.a,e.b,e.x,e.y); return shape; } return null; }
From source file:org.viafirma.util.QRCodeUtil.java
/** * Genera un justificante de firma de un fichero pdf de entrada. * //from w w w . j a v a 2s. com * @param input * Fichero pdf de entrada * @param texto * @param textoQR * @param codFirma * @param out * @throws ExcepcionErrorInterno */ public void firmarPDF(InputStream input, String texto, String texto2Line, String textoQR, String codFirma, OutputStream out) throws ExcepcionErrorInterno { // leemos el pdf utilizando iText. try { // Recuperamos el documento original PdfReader reader = new PdfReader(input); // Obtenemos el tamao de la pgina Rectangle pageSize = reader.getPageSize(1); // Creamos un nuevo documento del mismo tamao que el original Document document = new Document(pageSize); // creo una instancia para escritura en el documento PdfWriter writer = PdfWriter.getInstance(document, out); document.open(); // insertamos la portada del documento que estamos firmando. float escala = (pageSize.getHeight() - 350) / pageSize.getHeight(); // Aadimos al documento la imagen de cabecera y de pie addContent(texto, texto2Line, textoQR, codFirma, document, pageSize, true, false); PdfContentByte cb = writer.getDirectContent(); PdfImportedPage portada = writer.getImportedPage(reader, 1); cb.setRGBColorStroke(0xCC, 0xCC, 0xCC); cb.transform(AffineTransform.getTranslateInstance(document.leftMargin(), 210)); cb.transform(AffineTransform.getScaleInstance(escala, escala)); cb.addTemplate(portada, 0, 0);// , escala, 0, 0, escala,dx,dy); document.close(); out.close(); } catch (Exception e) { throw new ExcepcionErrorInterno(CodigoError.ERROR_INTERNO, e); } }
From source file:org.viafirma.util.QRCodeUtil.java
/** * Genera un documento sellado en fichero pdf. * /* ww w .j av a2s . c o m*/ * @param input * Fichero pdf de entrada * @param texto * @param textoQR * @param codFirma * @param out * @throws ExcepcionErrorInterno */ public void firmarPDFSellado(InputStream input, String texto, String texto2Line, String textoQR, String codFirma, OutputStream out) throws ExcepcionErrorInterno { // leemos el pdf utilizando iText. try { // Recuperamos el documento original PdfReader reader = new PdfReader(input); // Obtenemos el tamao de la pgina Rectangle pageSize = reader.getPageSize(1); // Creamos un nuevo documento del mismo tamao que el original Document document = new Document(pageSize); // creo una instancia para escritura en el documento PdfWriter writer = PdfWriter.getInstance(document, out); document.open(); // Aadimos al documento la imagen de cabecera y de pie float altoCabeceraYPie = addContent(texto, texto2Line, textoQR, codFirma, document, pageSize, true, true); altoCabeceraYPie = altoCabeceraYPie + document.topMargin(); // insertamos la portada del documento que estamos firmando. float escala = (pageSize.getHeight() - altoCabeceraYPie) / pageSize.getHeight(); PdfContentByte cb = writer.getDirectContent(); PdfImportedPage portada = writer.getImportedPage(reader, 1); cb.setRGBColorStroke(0xCC, 0xCC, 0xCC); cb.transform(AffineTransform.getScaleInstance(escala, escala)); cb.transform(AffineTransform.getTranslateInstance(document.leftMargin() * 2.5, ALTO_ETIQUETA + document.topMargin())); cb.addTemplate(portada, 0, 0);// , escala, 0, 0, escala,dx,dy); document.close(); out.close(); } catch (Exception e) { throw new ExcepcionErrorInterno(CodigoError.ERROR_INTERNO, e); } }
From source file:statechum.analysis.learning.Visualiser.java
protected static PluggableRenderer constructRenderer(Graph g, final LayoutOptions options) { final LayoutOptions graphLayoutOptions = options != null ? options : new LayoutOptions(); PluggableRenderer r = new PluggableRenderer() { /**/*from ww w . j a v a2s . co m*/ * Draws the edge <code>e</code>, whose endpoints are at <code>(x1,y1)</code> * and <code>(x2,y2)</code>, on the graphics context <code>g</code>. * The <code>Shape</code> provided by the <code>EdgeShapeFunction</code> instance * is scaled in the x-direction so that its width is equal to the distance between * <code>(x1,y1)</code> and <code>(x2,y2)</code>. */ @Override protected void drawSimpleEdge(Graphics2D g2d, Edge e, int x1, int y1, int x2, int y2) { final Vertex v1 = (Vertex) e.getEndpoints().getFirst(); final Vertex v2 = (Vertex) e.getEndpoints().getSecond(); boolean isLoop = v1.equals(v2); final Shape s2 = vertexShapeFunction.getShape(v2); Shape edgeShape = edgeShapeFunction.getShape(e); final double dx = x2 - x1; final double dy = y2 - y1; boolean edgeHit = true; boolean arrowHit = true; Rectangle deviceRectangle = null; if (screenDevice != null) { Dimension d = screenDevice.getSize(); if (d.width <= 0 || d.height <= 0) { d = screenDevice.getPreferredSize(); } deviceRectangle = new Rectangle(0, 0, d.width, d.height); } String label = edgeStringer.getLabel(e); assert (label != null); Component labelComponent = prepareRenderer(graphLabelRenderer, label, isPicked(e), e); Dimension d = labelComponent.getPreferredSize(); Rectangle2D EdgeShapeBoundaries = edgeShape.getBounds2D(); AffineTransform xform = AffineTransform.getTranslateInstance(x1, y1); double yMin = 0, yMax = 0; double thetaRadians = 0; if (isLoop) { // this is a self-loop. scale it is larger than the vertex // it decorates and translate it so that its nadir is // at the center of the vertex. int edgeIndex = ParallelEdgeIndexSingleton.getInstance().getIndex(e); Rectangle2D s2Bounds = s2.getBounds2D(); double scaleBy = 1 + (graphLayoutOptions.scaleLines - 1) * 1. / 3.; double translation = s2Bounds.getHeight() * (1. / 4. + edgeIndex / 4.); xform.translate(0, -scaleBy * translation); xform.scale(scaleBy * s2Bounds.getWidth(), scaleBy * s2Bounds.getHeight()); yMin = scaleBy * (EdgeShapeBoundaries.getMinY() * s2Bounds.getHeight()) - translation; yMax = scaleBy * (EdgeShapeBoundaries.getMaxY() * s2Bounds.getHeight()) - translation; } else { // this is a normal edge. Rotate it to the angle between // vertex endpoints, then scale it to the distance between // the vertices thetaRadians = Math.atan2(dy, dx); double dist = Math.sqrt(dx * dx + dy * dy); xform.rotate(thetaRadians); xform.scale(dist, 1.0); yMin = EdgeShapeBoundaries.getMinY(); yMax = EdgeShapeBoundaries.getMaxY(); } edgeShape = xform.createTransformedShape(edgeShape); // Debug code /* if (!isLoop) { g2d.setPaint(new Color( 250, 250, 0)); AffineTransform rect = AffineTransform.getTranslateInstance(x1, y1+yMin); rect.rotate(thetaRadians); g2d.fill(rect.createTransformedShape( new Rectangle(0,0,(int)Math.sqrt(dx*dx + dy*dy),(int)(yMax-yMin)))); } else { g2d.setPaint(new Color( 100, 250, 0)); AffineTransform rect = AffineTransform.getTranslateInstance(x1-s2.getBounds2D().getWidth()/2, y1+yMin); rect.rotate(thetaRadians); g2d.fill(rect.createTransformedShape( new Rectangle(0,0,(int)s2.getBounds2D().getWidth(),(int)(yMax-yMin)))); }*/ edgeHit = viewTransformer.transform(edgeShape).intersects(deviceRectangle); if (edgeHit == true) { Paint oldPaint = g2d.getPaint(); // get Paints for filling and drawing // (filling is done first so that drawing and label use same Paint) Paint fill_paint = edgePaintFunction.getFillPaint(e); if (fill_paint != null) { g2d.setPaint(fill_paint); g2d.fill(edgeShape); } Paint draw_paint = edgePaintFunction.getDrawPaint(e); if (draw_paint != null) { g2d.setPaint(draw_paint); g2d.draw(edgeShape); } double scalex = g2d.getTransform().getScaleX(); double scaley = g2d.getTransform().getScaleY(); // see if arrows are too small to bother drawing if (scalex < .3 || scaley < .3) return; if (edgeArrowPredicate.evaluate(e)) { Shape destVertexShape = vertexShapeFunction.getShape((Vertex) e.getEndpoints().getSecond()); AffineTransform xf = AffineTransform.getTranslateInstance(x2, y2); destVertexShape = xf.createTransformedShape(destVertexShape); arrowHit = viewTransformer.transform(destVertexShape).intersects(deviceRectangle); if (arrowHit) { AffineTransform at; if (edgeShape instanceof GeneralPath) at = getArrowTransform((GeneralPath) edgeShape, destVertexShape); else at = getArrowTransform(new GeneralPath(edgeShape), destVertexShape); if (at == null) return; Shape arrow = edgeArrowFunction.getArrow(e); arrow = at.createTransformedShape(arrow); // note that arrows implicitly use the edge's draw paint g2d.fill(arrow); } assert !(e instanceof UndirectedEdge); } // For difference visualisation only boolean labelBelow = false; if (graphLayoutOptions.showDIFF && (draw_paint == null || draw_paint instanceof Color && ((Color) draw_paint).equals(Color.BLACK))) labelBelow = true; // Now draw the label. double xLabel = 0, yLabel = 0, xa = 0, ya = 0, rotation = thetaRadians; if (isLoop) { double displacementY = labelBelow ? -yMin + d.height : -yMin + d.height, displacementX = d.width / 2; xa = x1 + dx / 2 + displacementY * Math.sin(thetaRadians); ya = y1 + dy / 2 - displacementY * Math.cos(thetaRadians); xLabel = xa - displacementX * Math.cos(thetaRadians); yLabel = ya - displacementX * Math.sin(thetaRadians); } else if (dx < 0) { double displacementY = labelBelow ? yMax - d.height : (-yMax - d.height), displacementX = d.width / 2; xa = x1 + dx / 2 + displacementY * Math.sin(thetaRadians); ya = y1 + dy / 2 - displacementY * Math.cos(thetaRadians); xLabel = xa + displacementX * Math.cos(thetaRadians); yLabel = ya + displacementX * Math.sin(thetaRadians); rotation = thetaRadians + Math.PI; } else { double displacementY = labelBelow ? yMax : -yMax, displacementX = d.width / 2; xa = x1 + dx / 2 + displacementY * Math.sin(thetaRadians); ya = y1 + dy / 2 - displacementY * Math.cos(thetaRadians); xLabel = xa - displacementX * Math.cos(thetaRadians); yLabel = ya - displacementX * Math.sin(thetaRadians); } AffineTransform old = g2d.getTransform(); AffineTransform labelTransform = new AffineTransform(); // Debug code: //g2d.drawLine((int)(x1+dx/2), (int)(y1+dy/2), (int)(xa), (int)(ya));g2d.drawLine((int)(xa), (int)(ya), (int)(xLabel), (int)(yLabel)); labelTransform.translate(xLabel, yLabel); labelTransform.rotate(rotation); g2d.setTransform(labelTransform); rendererPane.paintComponent(g2d, labelComponent, screenDevice, 0, 0, d.width, d.height, true); g2d.setTransform(old); // restore old paint g2d.setPaint(oldPaint); } // if edgeHit == true } }; r = labelEdges(g, r, graphLayoutOptions); r = labelVertices(r, g, graphLayoutOptions); r.setVertexIncludePredicate(new Predicate() { @Override public boolean evaluate(Object object) { if (!graphLayoutOptions.showIgnored && graphLayoutOptions.ignoredStates != null && graphLayoutOptions.ignoredStates.contains(object.toString())) return false; if (graphLayoutOptions.showNegatives) return true; else return DeterministicDirectedSparseGraph.isAccept((Vertex) object); } }); return r; }
From source file:uky.article.imageviewer.views.SWTImageCanvas.java
/** * Synchronize the scrollbar with the image. If the transform is out * of range, it will correct it. This function considers only following * factors :<b> transform, image size, client area</b>. *//*w w w . j a v a2s. c o m*/ public void syncScrollBars() { if (sourceImage == null) { redraw(); return; } AffineTransform af = transform; double sx = af.getScaleX(), sy = af.getScaleY(); double tx = af.getTranslateX(), ty = af.getTranslateY(); if (tx > 0) tx = 0; if (ty > 0) ty = 0; ScrollBar horizontal = getHorizontalBar(); horizontal.setIncrement(getClientArea().width / 100); horizontal.setPageIncrement(getClientArea().width); Rectangle imageBound = sourceImage.getBounds(); int cw = getClientArea().width, ch = getClientArea().height; if (imageBound.width * sx > cw) { /* image is wider than client area */ horizontal.setMaximum((int) (imageBound.width * sx)); horizontal.setEnabled(true); if (((int) -tx) > horizontal.getMaximum() - cw) tx = -horizontal.getMaximum() + cw; } else { /* image is narrower than client area */ horizontal.setEnabled(false); tx = (cw - imageBound.width * sx) / 2; //center if too small. } horizontal.setSelection((int) (-tx)); horizontal.setThumb((getClientArea().width)); ScrollBar vertical = getVerticalBar(); vertical.setIncrement(getClientArea().height / 100); vertical.setPageIncrement((getClientArea().height)); if (imageBound.height * sy > ch) { /* image is higher than client area */ vertical.setMaximum((int) (imageBound.height * sy)); vertical.setEnabled(true); if (((int) -ty) > vertical.getMaximum() - ch) ty = -vertical.getMaximum() + ch; } else { /* image is less higher than client area */ vertical.setEnabled(false); ty = (ch - imageBound.height * sy) / 2; //center if too small. } vertical.setSelection((int) (-ty)); vertical.setThumb((getClientArea().height)); /* update transform. */ af = AffineTransform.getScaleInstance(sx, sy); af.preConcatenate(AffineTransform.getTranslateInstance(tx, ty)); transform = af; redraw(); }