List of usage examples for java.awt.geom AffineTransform getScaleY
public double getScaleY()
From source file:Main.java
public void paint(Graphics g) { Shape shape = new Rectangle2D.Float(100, 50, 80, 80); Graphics2D g2 = (Graphics2D) g; AffineTransform at = new AffineTransform(); at.setToQuadrantRotation(2, 0.5, 0.5); System.out.println(at.getScaleY()); g2.setTransform(at);/* w w w . j a v a2s .c om*/ g2.draw(shape); }
From source file:Matrix.java
/** * Set the values of the matrix from the AffineTransform. * //from w w w . j a v a2 s. c o m * @param af The transform to get the values from. */ public void setFromAffineTransform(AffineTransform af) { single[0] = (float) af.getScaleX(); single[1] = (float) af.getShearY(); single[3] = (float) af.getShearX(); single[4] = (float) af.getScaleY(); single[6] = (float) af.getTranslateX(); single[7] = (float) af.getTranslateY(); }
From source file:es.ucm.fdi.edd.ui.views.utils.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>. *///from w w w .j a va 2 s . 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((int) (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((int) (getClientArea().width)); ScrollBar vertical = getVerticalBar(); vertical.setIncrement((int) (getClientArea().height / 100)); vertical.setPageIncrement((int) (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((int) (getClientArea().height)); /* update transform. */ af = AffineTransform.getScaleInstance(sx, sy); af.preConcatenate(AffineTransform.getTranslateInstance(tx, ty)); transform = af; redraw(); }
From source file:fr.ign.cogit.geoxygene.appli.layer.LayerViewAwtPanel.java
public void saveAsImage(String fileName, int width, int height, boolean doSaveWorldFile, boolean drawOverlay) { Color bg = this.getBackground(); BufferedImage outImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); Graphics2D graphics = outImage.createGraphics(); // TEMP/*from w w w . j a va 2 s .c o m*/ graphics.setColor(bg); graphics.fillRect(0, 0, width, height); int tmpw = this.getWidth(); int tmph = this.getHeight(); // We save the old extent to force the Viewport to keep the old // window in world coordinates. IEnvelope env = this.getViewport().getEnvelopeInModelCoordinates(); // Artificially resize the canvas to the image dimensions. this.setSize(width, height); try { // We zoom to the old extent in the resized canvas. this.getViewport().zoom(env); } catch (NoninvertibleTransformException e2) { logger.error("In Image Export : failed to zoom in the correct extent."); e2.printStackTrace(); } this.renderingManager.renderAll(); long time = System.currentTimeMillis(); long twaited = 0; while (this.renderingManager.isRendering() && twaited < 15000) { // Wait for the rendering to end for a maximum of 15s. If the // rendering is not finished after this delay, // we give up. twaited = System.currentTimeMillis() - time; } if (this.renderingManager.isRendering()) { logger.error("Export to image : waited 15s but the rendering is still not finished. Abort."); return; } // We have to impose a bbox !!! this.getRenderingManager().copyTo(graphics); if (drawOverlay) this.paintOverlays(graphics); graphics.dispose(); try { ImgUtil.saveImage(outImage, fileName); } catch (IOException e1) { e1.printStackTrace(); } if (doSaveWorldFile) { String wld = FilenameUtils.removeExtension(fileName) + ".wld"; try { AffineTransform t = this.getViewport().getModelToViewTransform(); fr.ign.cogit.geoxygene.util.conversion.WorldFileWriter.write(new File(wld), t.getScaleX(), t.getScaleY(), this.getViewport().getViewOrigin().getX(), this.getViewport().getViewOrigin().getY(), this.getHeight()); } catch (NoninvertibleTransformException e) { logger.error("Failed to save the world file associated with the image file " + fileName); e.printStackTrace(); } } // Finally, rollback the canvas to its original size. this.setSize(tmpw, tmph); try { // Zoom back to the "normal" extent this.getViewport().zoom(env); } catch (NoninvertibleTransformException e2) { logger.error("In Image Export : failed to zoom back to the original LayerViewPanel extent."); e2.printStackTrace(); return; } }
From source file:org.apache.fop.afp.AFPGraphics2D.java
/** * Converts a length value to an absolute value. * Please note that this only uses the "ScaleY" factor, so this will result * in a bad value should "ScaleX" and "ScaleY" be different. * @param length the length//from ww w . j a v a 2s . c o m * @return the absolute length */ public double convertToAbsoluteLength(double length) { AffineTransform current = getTransform(); double mult = getResolution() / (double) UnitConv.IN2PT; double factor = -current.getScaleY() / mult; return length * factor; }
From source file:org.apache.fop.afp.AFPGraphics2D.java
/** {@inheritDoc} */ @Override//from w ww . j av a 2s . c om public void drawRenderedImage(RenderedImage img, AffineTransform xform) { int imgWidth = img.getWidth(); int imgHeight = img.getHeight(); AffineTransform gat = gc.getTransform(); int graphicsObjectHeight = graphicsObj.getObjectEnvironmentGroup().getObjectAreaDescriptor().getHeight(); double toMillipointFactor = UnitConv.IN2PT * 1000 / (double) paintingState.getResolution(); double x = gat.getTranslateX(); double y = -(gat.getTranslateY() - graphicsObjectHeight); x = toMillipointFactor * x; y = toMillipointFactor * y; double w = toMillipointFactor * imgWidth * gat.getScaleX(); double h = toMillipointFactor * imgHeight * -gat.getScaleY(); AFPImageHandlerRenderedImage handler = new AFPImageHandlerRenderedImage(); ImageInfo imageInfo = new ImageInfo(null, null); imageInfo.setSize(new ImageSize(img.getWidth(), img.getHeight(), paintingState.getResolution())); imageInfo.getSize().calcSizeFromPixels(); ImageRendered red = new ImageRendered(imageInfo, img, null); Rectangle targetPos = new Rectangle((int) Math.round(x), (int) Math.round(y), (int) Math.round(w), (int) Math.round(h)); AFPRenderingContext context = new AFPRenderingContext(null, resourceManager, paintingState, fontInfo, null); try { handler.handleImage(context, red, targetPos); } catch (IOException ioe) { handleIOException(ioe); } }
From source file:org.apache.fop.render.pcl.PCLRenderingUtil.java
/** * Determines the print direction based on the given transformation matrix. This method * only detects right angles (0, 90, 180, 270). If any other angle is determined, 0 is * returned.//from w ww. j ava 2 s . c om * @param transform the transformation matrix * @return the angle in degrees of the print direction. */ public static int determinePrintDirection(AffineTransform transform) { int newDir; if (transform.getScaleX() == 0 && transform.getScaleY() == 0 && transform.getShearX() == 1 && transform.getShearY() == -1) { newDir = 90; } else if (transform.getScaleX() == -1 && transform.getScaleY() == -1 && transform.getShearX() == 0 && transform.getShearY() == 0) { newDir = 180; } else if (transform.getScaleX() == 0 && transform.getScaleY() == 0 && transform.getShearX() == -1 && transform.getShearY() == 1) { newDir = 270; } else { newDir = 0; } return newDir; }
From source file:org.apache.fop.render.pdf.pdfbox.PDFBoxImageHandler.java
public void handleImage(RenderingContext context, Image image, Rectangle pos) throws IOException { assert context instanceof PDFRenderingContext; PDFRenderingContext pdfContext = (PDFRenderingContext) context; PDFContentGenerator generator = pdfContext.getGenerator(); assert image instanceof ImagePDF; ImagePDF pdfImage = (ImagePDF) image; float x = (float) pos.getX() / 1000f; float y = (float) pos.getY() / 1000f; // float w = (float)pos.getWidth() / 1000f; float h = (float) pos.getHeight() / 1000f; AffineTransform pageAdjust = new AffineTransform(); AffineTransform at = generator.getAffineTransform(); if (at != null) { pageAdjust.setToTranslation((float) (generator.getState().getTransform().getTranslateX()), (float) (generator.getState().getTransform().getTranslateY() - h - y)); }/* w w w . ja v a2 s . c o m*/ FontInfo fontinfo = (FontInfo) context.getHint("fontinfo"); String stream = createStreamForPDF(pdfImage, pdfContext.getPage(), pdfContext.getUserAgent(), pageAdjust, fontinfo, pos, pdfContext.getPageNumbers(), pdfContext.getPdfLogicalStructureHandler(), pdfContext.getCurrentSessionStructElem()); if (stream == null) { return; } if (pageAdjust.getScaleX() != 0) { pageAdjust.translate(x * (1 / pageAdjust.getScaleX()), -y * (1 / -pageAdjust.getScaleY())); } generator.placeImage(pageAdjust, stream); }
From source file:org.apache.fop.render.pdf.PDFImageHandlerSVG.java
/** {@inheritDoc} */ public void handleImage(RenderingContext context, // CSOK: MethodLength Image image, Rectangle pos) throws IOException { PDFRenderingContext pdfContext = (PDFRenderingContext) context; PDFContentGenerator generator = pdfContext.getGenerator(); ImageXMLDOM imageSVG = (ImageXMLDOM) image; FOUserAgent userAgent = context.getUserAgent(); final float deviceResolution = userAgent.getTargetResolution(); if (log.isDebugEnabled()) { log.debug("Generating SVG at " + deviceResolution + "dpi."); }//w w w . j ava 2s . c o m final float uaResolution = userAgent.getSourceResolution(); SVGUserAgent ua = new SVGUserAgent(userAgent, new AffineTransform()); GVTBuilder builder = new GVTBuilder(); //Controls whether text painted by Batik is generated using text or path operations boolean strokeText = false; //TODO connect with configuration elsewhere. BridgeContext ctx = new PDFBridgeContext(ua, (strokeText ? null : pdfContext.getFontInfo()), userAgent.getFactory().getImageManager(), userAgent.getImageSessionContext(), new AffineTransform()); //Cloning SVG DOM as Batik attaches non-thread-safe facilities (like the CSS engine) //to it. Document clonedDoc = BatikUtil.cloneSVGDocument(imageSVG.getDocument()); GraphicsNode root; try { root = builder.build(ctx, clonedDoc); builder = null; } catch (Exception e) { SVGEventProducer eventProducer = SVGEventProducer.Provider .get(context.getUserAgent().getEventBroadcaster()); eventProducer.svgNotBuilt(this, e, image.getInfo().getOriginalURI()); return; } // get the 'width' and 'height' attributes of the SVG document float w = image.getSize().getWidthMpt(); float h = image.getSize().getHeightMpt(); float sx = pos.width / w; float sy = pos.height / h; //Scaling and translation for the bounding box of the image AffineTransform scaling = new AffineTransform(sx, 0, 0, sy, pos.x / 1000f, pos.y / 1000f); double sourceScale = UnitConv.IN2PT / uaResolution; scaling.scale(sourceScale, sourceScale); //Scale for higher resolution on-the-fly images from Batik AffineTransform resolutionScaling = new AffineTransform(); double targetScale = uaResolution / deviceResolution; resolutionScaling.scale(targetScale, targetScale); resolutionScaling.scale(1.0 / sx, 1.0 / sy); //Transformation matrix that establishes the local coordinate system for the SVG graphic //in relation to the current coordinate system AffineTransform imageTransform = new AffineTransform(); imageTransform.concatenate(scaling); imageTransform.concatenate(resolutionScaling); if (log.isTraceEnabled()) { log.trace("nat size: " + w + "/" + h); log.trace("req size: " + pos.width + "/" + pos.height); log.trace("source res: " + uaResolution + ", targetRes: " + deviceResolution + " --> target scaling: " + targetScale); log.trace(image.getSize()); log.trace("sx: " + sx + ", sy: " + sy); log.trace("scaling: " + scaling); log.trace("resolution scaling: " + resolutionScaling); log.trace("image transform: " + resolutionScaling); } /* * Clip to the svg area. * Note: To have the svg overlay (under) a text area then use * an fo:block-container */ if (log.isTraceEnabled()) { generator.comment("SVG setup"); } generator.saveGraphicsState(); if (context.getUserAgent().isAccessibilityEnabled()) { MarkedContentInfo mci = pdfContext.getMarkedContentInfo(); generator.beginMarkedContentSequence(mci.tag, mci.mcid); } generator.updateColor(Color.black, false, null); generator.updateColor(Color.black, true, null); if (!scaling.isIdentity()) { if (log.isTraceEnabled()) { generator.comment("viewbox"); } generator.add(CTMHelper.toPDFString(scaling, false) + " cm\n"); } //SVGSVGElement svg = ((SVGDocument)doc).getRootElement(); PDFGraphics2D graphics = new PDFGraphics2D(true, pdfContext.getFontInfo(), generator.getDocument(), generator.getResourceContext(), pdfContext.getPage().referencePDF(), "", 0); graphics.setGraphicContext(new org.apache.xmlgraphics.java2d.GraphicContext()); if (!resolutionScaling.isIdentity()) { if (log.isTraceEnabled()) { generator.comment("resolution scaling for " + uaResolution + " -> " + deviceResolution); } generator.add(CTMHelper.toPDFString(resolutionScaling, false) + " cm\n"); graphics.scale(1.0 / resolutionScaling.getScaleX(), 1.0 / resolutionScaling.getScaleY()); } if (log.isTraceEnabled()) { generator.comment("SVG start"); } //Save state and update coordinate system for the SVG image generator.getState().save(); generator.getState().concatenate(imageTransform); //Now that we have the complete transformation matrix for the image, we can update the //transformation matrix for the AElementBridge. PDFAElementBridge aBridge = (PDFAElementBridge) ctx.getBridge(SVGDOMImplementation.SVG_NAMESPACE_URI, SVGConstants.SVG_A_TAG); aBridge.getCurrentTransform().setTransform(generator.getState().getTransform()); graphics.setPaintingState(generator.getState()); graphics.setOutputStream(generator.getOutputStream()); try { root.paint(graphics); ctx.dispose(); generator.add(graphics.getString()); } catch (Exception e) { SVGEventProducer eventProducer = SVGEventProducer.Provider .get(context.getUserAgent().getEventBroadcaster()); eventProducer.svgRenderingError(this, e, image.getInfo().getOriginalURI()); } generator.getState().restore(); if (context.getUserAgent().isAccessibilityEnabled()) { generator.restoreGraphicsStateAccess(); } else { generator.restoreGraphicsState(); } if (log.isTraceEnabled()) { generator.comment("SVG end"); } }
From source file:org.apache.pdfbox.rendering.PageDrawer.java
@Override public void drawImage(PDImage pdImage) throws IOException { Matrix ctm = getGraphicsState().getCurrentTransformationMatrix(); AffineTransform at = ctm.createAffineTransform(); if (!pdImage.getInterpolate()) { boolean isScaledUp = pdImage.getWidth() < Math.round(at.getScaleX()) || pdImage.getHeight() < Math.round(at.getScaleY()); // if the image is scaled down, we use smooth interpolation, eg PDFBOX-2364 // only when scaled up do we use nearest neighbour, eg PDFBOX-2302 / mori-cvpr01.pdf // stencils are excluded from this rule (see survey.pdf) if (isScaledUp || pdImage.isStencil()) { graphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR); }//w w w.jav a 2s . co m } if (pdImage.isStencil()) { // fill the image with paint BufferedImage image = pdImage.getStencilImage(getNonStrokingPaint()); // draw the image drawBufferedImage(image, at); } else { // draw the image drawBufferedImage(pdImage.getImage(), at); } if (!pdImage.getInterpolate()) { // JDK 1.7 has a bug where rendering hints are reset by the above call to // the setRenderingHint method, so we re-set all hints, see PDFBOX-2302 setRenderingHints(); } }