Example usage for java.awt Graphics2D getClass

List of usage examples for java.awt Graphics2D getClass

Introduction

In this page you can find the example usage for java.awt Graphics2D getClass.

Prototype

@HotSpotIntrinsicCandidate
public final native Class<?> getClass();

Source Link

Document

Returns the runtime class of this Object .

Usage

From source file:ec.util.chart.swing.Charts.java

private static String generateSVG(JFreeChart chart, int width, int height) throws IOException {
    try {/* w ww.  j a  v a 2s  .  c  om*/
        Class<?> svgGraphics2d = Class.forName("org.jfree.graphics2d.svg.SVGGraphics2D");
        Graphics2D g2 = (Graphics2D) svgGraphics2d.getConstructor(int.class, int.class).newInstance(width,
                height);
        // we suppress shadow generation, because SVG is a vector format and
        // the shadow effect is applied via bitmap effects...
        g2.setRenderingHint(JFreeChart.KEY_SUPPRESS_SHADOW_GENERATION, true);
        chart.draw(g2, new Rectangle2D.Double(0, 0, width, height));
        return (String) g2.getClass().getMethod("getSVGElement").invoke(g2);
    } catch (ClassNotFoundException | NoSuchMethodException | SecurityException | InstantiationException
            | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
        throw new IOException("Cannot generate SVG", ex);
    }
}

From source file:org.moeaframework.analysis.plot.Plot.java

/**
 * Generates a string containing a rendering of the chart in SVG format.
 * This feature is only supported if the JFreeSVG library is included on 
 * the classpath.//from   ww w .  j  a v a  2 s .c  o  m
 * 
 * This is copied from JFreeChart's ChartPanel class (version 1.0.19).
 * 
 * @return A string containing an SVG element for the current chart, or 
 *     <code>null</code> if there is a problem with the method invocation
 *     by reflection.
 */
private String generateSVG(int width, int height) {
    Graphics2D g2 = createSVGGraphics2D(width, height);

    if (g2 == null) {
        throw new IllegalStateException("JFreeSVG library is not present.");
    }

    // we suppress shadow generation, because SVG is a vector format and
    // the shadow effect is applied via bitmap effects...
    g2.setRenderingHint(new RenderingHints.Key(0) {
        @Override
        public boolean isCompatibleValue(Object val) {
            return val instanceof Boolean;
        }
    }, true);

    String svg = null;
    Rectangle2D drawArea = new Rectangle2D.Double(0, 0, width, height);
    chart.draw(g2, drawArea);

    try {
        Method m = g2.getClass().getMethod("getSVGElement");
        svg = (String) m.invoke(g2);
    } catch (NoSuchMethodException e) {
        // null will be returned
    } catch (SecurityException e) {
        // null will be returned
    } catch (IllegalAccessException e) {
        // null will be returned
    } catch (IllegalArgumentException e) {
        // null will be returned
    } catch (InvocationTargetException e) {
        // null will be returned
    }

    return svg;
}