Java Code Examples for org.apache.batik.gvt.GraphicsNode#paint()
The following examples show how to use
org.apache.batik.gvt.GraphicsNode#paint() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SwingUniversalImageSvg.java From hop with Apache License 2.0 | 6 votes |
public static void render( Graphics2D gc, GraphicsNode svgGraphicsNode, Dimension2D svgGraphicsSize, int centerX, int centerY, int width, int height, double angleRadians ) { double scaleX = width / svgGraphicsSize.getWidth(); double scaleY = height / svgGraphicsSize.getHeight(); AffineTransform affineTransform = new AffineTransform(); if ( centerX != 0 || centerY != 0 ) { affineTransform.translate( centerX, centerY ); } affineTransform.scale( scaleX, scaleY ); if ( angleRadians != 0 ) { affineTransform.rotate( angleRadians ); } affineTransform.translate( -svgGraphicsSize.getWidth() / 2, -svgGraphicsSize.getHeight() / 2 ); svgGraphicsNode.setTransform( affineTransform ); svgGraphicsNode.paint( gc ); }
Example 2
Source File: SVGIcon.java From netbeans with Apache License 2.0 | 6 votes |
@Override protected Image createAndPaintImage( Component c, ColorModel colorModel, int deviceWidth, int deviceHeight, double scale) { BufferedImage img = createBufferedImage(colorModel, deviceWidth, deviceHeight); /* Use Batik's createGraphics method to improve performance and avoid the "Graphics2D from BufferedImage lacks BUFFERED_IMAGE hint" warning. */ final Graphics2D g = GraphicsUtil.createGraphics(img); try { g.scale(scale, scale); try { GraphicsNode graphicsNode = getGraphicsNode(); g.addRenderingHints(createHints()); graphicsNode.paint(g); } catch (IOException e) { LOG.log(Level.WARNING, "Unexpected exception while re-loading an SVG file that previously loaded successfully", e); } } finally { g.dispose(); } return img; }
Example 3
Source File: SwingUniversalImageSvg.java From pentaho-kettle with Apache License 2.0 | 6 votes |
public static void render( Graphics2D gc, GraphicsNode svgGraphicsNode, Dimension2D svgGraphicsSize, int centerX, int centerY, int width, int height, double angleRadians ) { double scaleX = width / svgGraphicsSize.getWidth(); double scaleY = height / svgGraphicsSize.getHeight(); AffineTransform affineTransform = new AffineTransform(); if ( centerX != 0 || centerY != 0 ) { affineTransform.translate( centerX, centerY ); } affineTransform.scale( scaleX, scaleY ); if ( angleRadians != 0 ) { affineTransform.rotate( angleRadians ); } affineTransform.translate( -svgGraphicsSize.getWidth() / 2, -svgGraphicsSize.getHeight() / 2 ); svgGraphicsNode.setTransform( affineTransform ); svgGraphicsNode.paint( gc ); }
Example 4
Source File: AbstractSvgDataToGraphics2DRenderer.java From jasperreports with GNU Lesser General Public License v3.0 | 5 votes |
@Override public void render(JasperReportsContext jasperReportsContext, Graphics2D grx, Rectangle2D rectangle) throws JRException { GraphicsNode rootNode = getRootNode(jasperReportsContext); AffineTransform transform = ViewBox.getPreserveAspectRatioTransform( new float[]{0, 0, (float) documentSize.getWidth(), (float) documentSize.getHeight()}, SVGPreserveAspectRatio.SVG_PRESERVEASPECTRATIO_NONE, true, (float) rectangle.getWidth(), (float) rectangle.getHeight() ); Graphics2D graphics = (Graphics2D) grx.create(); try { graphics.translate(rectangle.getX(), rectangle.getY()); graphics.transform(transform); // CompositeGraphicsNode not thread safe synchronized (rootNode) { rootNode.paint(graphics); } } finally { graphics.dispose(); } }
Example 5
Source File: VehicleMapLayer.java From mars-sim with GNU General Public License v3.0 | 5 votes |
/** * Creates a buffered image from a SVG graphics node. * @param svg the SVG graphics node. * @param width the width of the produced image. * @param length the length of the produced image. * @return the created buffered image. */ private BufferedImage createBufferedImage(GraphicsNode svg, double width, double length) { int imageWidth = (int) (width * scale); if (imageWidth <= 0) { imageWidth = 1; } int imageLength = (int) (length * scale); if (imageLength <= 0) { imageLength = 1; } BufferedImage bufferedImage = new BufferedImage( imageWidth, imageLength, BufferedImage.TYPE_INT_ARGB ); // Determine bounds. Rectangle2D bounds = svg.getBounds(); // Determine transform information. double scalingWidth = width / bounds.getWidth() * scale; double scalingLength = length / bounds.getHeight() * scale; // Draw the SVG image on the buffered image. Graphics2D g2d = (Graphics2D) bufferedImage.getGraphics(); g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); svg.setTransform(AffineTransform.getScaleInstance(scalingWidth, scalingLength)); svg.paint(g2d); // Cleanup and return image g2d.dispose(); return bufferedImage; }
Example 6
Source File: WappalyzerJsonParser.java From zap-extensions with Apache License 2.0 | 5 votes |
private static ImageIcon createSvgIcon(URL url) throws Exception { if (url == null) { return null; } String xmlParser = XMLResourceDescriptor.getXMLParserClassName(); SAXSVGDocumentFactory df = new SAXSVGDocumentFactory(xmlParser); SVGDocument doc = null; GraphicsNode svgIcon = null; try { doc = df.createSVGDocument(url.toString()); } catch (RuntimeException re) { // v1 SVGs are unsupported return null; } doc.getRootElement().setAttribute("width", String.valueOf(SIZE)); doc.getRootElement().setAttribute("height", String.valueOf(SIZE)); UserAgent userAgent = new UserAgentAdapter(); DocumentLoader loader = new DocumentLoader(userAgent); GVTBuilder builder = new GVTBuilder(); try { svgIcon = builder.build(new BridgeContext(userAgent, loader), doc); } catch (BridgeException | StringIndexOutOfBoundsException ex) { logger.debug("Failed to parse SVG. " + ex.getMessage()); return null; } AffineTransform transform = new AffineTransform(1, 0.0, 0.0, 1, 0, 0); BufferedImage image = new BufferedImage(SIZE, SIZE, BufferedImage.TYPE_INT_ARGB); Graphics2D g2d = addRenderingHints(image); svgIcon.setTransform(transform); svgIcon.paint(g2d); g2d.dispose(); return new ImageIcon(image); }
Example 7
Source File: GuiUtils.java From workcraft with MIT License | 4 votes |
public static ImageIcon createIconFromSVG(String path, int height, int width, Color background) { try { System.setProperty("org.apache.batik.warn_destination", "false"); Document document; String parser = XMLResourceDescriptor.getXMLParserClassName(); SAXSVGDocumentFactory f = new SAXSVGDocumentFactory(parser); document = f.createDocument(ClassLoader.getSystemResource(path).toString()); UserAgentAdapter userAgentAdapter = new UserAgentAdapter(); BridgeContext bridgeContext = new BridgeContext(userAgentAdapter); GVTBuilder builder = new GVTBuilder(); GraphicsNode graphicsNode = builder.build(bridgeContext, document); double sizeY = bridgeContext.getDocumentSize().getHeight(); double sizeX = bridgeContext.getDocumentSize().getWidth(); BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); Graphics2D g2d = (Graphics2D) bufferedImage.getGraphics(); if (background != null) { g2d.setColor(background); g2d.fillRect(0, 0, width, height); } g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); g2d.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON); g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); g2d.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE); g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); double scaleX = (width - 1) / sizeX; double scaleY = (height - 1) / sizeY; double scale = Math.min(scaleX, scaleY); g2d.scale(scale, scale); g2d.translate(0.5, 0.5); graphicsNode.paint(g2d); g2d.dispose(); return new ImageIcon(bufferedImage); } catch (Throwable e) { System.err.println("Failed to load SVG file " + path); System.err.println(e); return null; } }
Example 8
Source File: StructureMapLayer.java From mars-sim with GNU General Public License v3.0 | 4 votes |
/** * Creates a buffered image from a SVG graphics node. * @param svg the SVG graphics node. * @param width the structure width (meters). * @param length the structure length (meters). * @param patternSVG the pattern SVG graphics node (null if no pattern). * @return the created buffered image. */ private BufferedImage createBufferedImage(GraphicsNode svg, double width, double length, GraphicsNode patternSVG) { int imageWidth = (int) (width * scale); if (imageWidth <= 0) { imageWidth = 1; } int imageLength = (int) (length * scale); if (imageLength <= 0) { imageLength = 1; } BufferedImage bufferedImage = new BufferedImage( imageWidth, imageLength, BufferedImage.TYPE_INT_ARGB ); // Determine bounds. Rectangle2D bounds = svg.getBounds(); // Determine transform information. double scalingWidth = width / bounds.getWidth() * scale; double scalingLength = length / bounds.getHeight() * scale; // Draw the SVG image on the buffered image. Graphics2D g2d = (Graphics2D) bufferedImage.getGraphics(); g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); svg.setTransform(AffineTransform.getScaleInstance(scalingWidth, scalingLength)); svg.paint(g2d); // Draw repeating pattern SVG image on the buffered image. if (patternSVG != null) { double patternScaling = 0D; double patternWidth = 0D; double patternLength = 0D; double originalProportions = bounds.getWidth() / bounds.getHeight(); double finalProportions = width / length; Rectangle2D patternBounds = patternSVG.getBounds(); if ((finalProportions / originalProportions) >= 1D) { patternScaling = scalingLength; patternLength = length * (patternBounds.getHeight() / bounds.getHeight()); patternWidth = patternLength * (patternBounds.getWidth() / patternBounds.getHeight()); } else { patternScaling = scalingWidth; patternWidth = width * (patternBounds.getWidth() / bounds.getWidth()); patternLength = patternWidth * (patternBounds.getHeight() / patternBounds.getWidth()); } AffineTransform patternTransform = new AffineTransform(); patternTransform.scale(patternScaling, patternScaling); for (double x = 0D; x < length; x += patternLength) { patternTransform.translate(0D, x * bounds.getHeight()); double y = 0D; for (; y < width; y += patternWidth) { patternTransform.translate(y * bounds.getWidth(), 0D); patternSVG.setTransform(patternTransform); patternSVG.paint(g2d); patternTransform.translate(y * bounds.getWidth() * -1D, 0D); } patternTransform.translate(0D, x * bounds.getHeight() * -1D); } } // Cleanup and return image g2d.dispose(); return bufferedImage; }