Example usage for java.awt Graphics2D transform

List of usage examples for java.awt Graphics2D transform

Introduction

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

Prototype

public abstract void transform(AffineTransform Tx);

Source Link

Document

Composes an AffineTransform object with the Transform in this Graphics2D according to the rule last-specified-first-applied.

Usage

From source file:edu.jhuapl.graphs.jfreechart.utils.HighResChartUtil.java

/**
 * Returns a high resolution BufferedImage of the chart. Uses the default DPI_FILE_RESOLUTION.
 *
 * @param resolution The resolution, in dots per inch, of the image to generate.
 * @return the buffered image./*from w ww. j  av a  2s. c o  m*/
 */
public static BufferedImage getHighResChartImage(ChartPanel chartPanel, int resolution) {
    int screenResolution = Toolkit.getDefaultToolkit().getScreenResolution();
    double scaleRatio = resolution / screenResolution;
    int width = chartPanel.getWidth();
    int height = chartPanel.getHeight();
    int rasterWidth = (int) (width * scaleRatio);
    int rasterHeight = (int) (height * scaleRatio);

    BufferedImage image = new BufferedImage(rasterWidth, rasterHeight, BufferedImage.TYPE_INT_RGB);
    Graphics2D g2 = image.createGraphics();

    g2.transform(AffineTransform.getScaleInstance(scaleRatio, scaleRatio));
    chartPanel.getChart().draw(g2, new Rectangle2D.Double(0, 0, width, height), null);
    g2.dispose();

    return image;
}

From source file:Main.java

/**
 * Draws a shape with the specified rotation about <code>(x, y)</code>.
 *
 * @param g2  the graphics device (<code>null</code> not permitted).
 * @param shape  the shape (<code>null</code> not permitted).
 * @param angle  the angle (in radians).
 * @param x  the x coordinate for the rotation point.
 * @param y  the y coordinate for the rotation point.
 *///  w  w  w.  j a v  a2 s .  co m
public static void drawRotatedShape(final Graphics2D g2, final Shape shape, final double angle, final float x,
        final float y) {

    final AffineTransform saved = g2.getTransform();
    final AffineTransform rotate = AffineTransform.getRotateInstance(angle, x, y);
    g2.transform(rotate);
    g2.draw(shape);
    g2.setTransform(saved);

}

From source file:Main.java

static void drawArrow(Graphics g1, int x1, int y1, int x2, int y2) {
    // x1 and y1 are coordinates of circle or rectangle
    // x2 and y2 are coordinates of circle or rectangle, to this point is directed the arrow
    Graphics2D g = (Graphics2D) g1.create();
    double dx = x2 - x1;
    double dy = y2 - y1;
    double angle = Math.atan2(dy, dx);
    int len = (int) Math.sqrt(dx * dx + dy * dy);
    AffineTransform t = AffineTransform.getTranslateInstance(x1, y1);
    t.concatenate(AffineTransform.getRotateInstance(angle));
    g.transform(t);
    g.drawLine(0, 0, len, 0);//w w w. j a va 2  s. c  om
    int basePosition = len - ARROW_HEAD_SIZE.width;
    int height = ARROW_HEAD_SIZE.height;
    g.fillPolygon(new int[] { len, basePosition, basePosition, len }, new int[] { 0, -height, height, 0 }, 4);
}

From source file:net.sf.mcf2pdf.mcfelements.util.ImageUtil.java

/**
 * Loads the given CLP or SVG file and creates a BufferedImage with the given dimensions. As CLP files contain Vector images,
 * they can be scaled to every size needed. The contents are scaled to the given width and height, <b>not</b> preserving any
 * ratio of the image.//ww  w  .  jav  a2 s.  c  om
 *
 * @param clpFile CLP or SVG file.
 * @param widthPixel The width, in pixels, the resulting image shall have.
 * @param heightPixel The height, in pixels, the resulting image shall have.
 *
 * @return An image displaying the contents of the loaded CLP file.
 *
 * @throws IOException If any I/O related problem occurs reading the file.
 */
public static BufferedImage loadClpFile(File clpFile, int widthPixel, int heightPixel) throws IOException {
    FileInputStream fis = new FileInputStream(clpFile);
    ClpInputStream cis = null;
    InputStream in = clpFile.getName().toLowerCase().endsWith(".clp") ? (cis = new ClpInputStream(fis)) : fis;

    UserAgentAdapter userAgentAdapter = new UserAgentAdapter();
    BridgeContext bridgeContext = new BridgeContext(userAgentAdapter);

    SVGDocument svgDocument;
    GraphicsNode rootSvgNode;
    try {
        String parser = XMLResourceDescriptor.getXMLParserClassName();
        SAXSVGDocumentFactory factory = new SAXSVGDocumentFactory(parser);
        svgDocument = (SVGDocument) factory.createDocument(clpFile.toURI().toString(),
                new InputStreamReader(in, "ISO-8859-1"));
        rootSvgNode = getRootNode(svgDocument, bridgeContext);
    } finally {
        IOUtils.closeQuietly(cis);
        IOUtils.closeQuietly(fis);
    }

    float[] vb = ViewBox.parseViewBoxAttribute(svgDocument.getRootElement(),
            svgDocument.getRootElement().getAttribute("viewBox"), bridgeContext);

    AffineTransform usr2dev = ViewBox.getPreserveAspectRatioTransform(vb,
            SVGPreserveAspectRatio.SVG_PRESERVEASPECTRATIO_NONE, true, widthPixel, heightPixel);

    BufferedImage img = new BufferedImage(widthPixel, heightPixel, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2d = img.createGraphics();

    g2d.setColor(new Color(0.0f, 0.0f, 0.0f, 0.0f));
    g2d.fillRect(0, 0, widthPixel, heightPixel);
    g2d.transform(usr2dev);

    // fixes "Graphics2D from BufferedImage lacks BUFFERED_IMAGE hint" - part 1
    final Object oldBufferedImage = g2d.getRenderingHint(RenderingHintsKeyExt.KEY_BUFFERED_IMAGE);
    g2d.setRenderingHint(RenderingHintsKeyExt.KEY_BUFFERED_IMAGE, new WeakReference<BufferedImage>(img));
    rootSvgNode.paint(g2d);
    // fixes "Graphics2D from BufferedImage lacks BUFFERED_IMAGE hint" - part 2
    if (oldBufferedImage != null)
        g2d.setRenderingHint(RenderingHintsKeyExt.KEY_BUFFERED_IMAGE, oldBufferedImage);
    else
        g2d.getRenderingHints().remove(RenderingHintsKeyExt.KEY_BUFFERED_IMAGE);

    g2d.dispose();
    return img;
}

From source file:net.sf.mzmine.chartbasics.graphicsexport.ChartExportUtil.java

/**
 * Paints a chart with scaling options/*from  ww w .jav  a 2 s  . c om*/
 * 
 * @param chart
 * @param info
 * @param out
 * @param width
 * @param height
 * @param resolution
 * @return BufferedImage of a given chart with scaling to resolution
 * @throws IOException
 */
private static BufferedImage paintScaledChartToBufferedImage(JFreeChart chart, ChartRenderingInfo info,
        OutputStream out, int width, int height, int resolution, int bufferedIType) throws IOException {
    Args.nullNotPermitted(out, "out");
    Args.nullNotPermitted(chart, "chart");

    double scaleX = resolution / 72.0;
    double scaleY = resolution / 72.0;

    double desiredWidth = width * scaleX;
    double desiredHeight = height * scaleY;
    double defaultWidth = width;
    double defaultHeight = height;
    boolean scale = false;

    // get desired width and height from somewhere then...
    if ((scaleX != 1) || (scaleY != 1)) {
        scale = true;
    }

    BufferedImage image = new BufferedImage((int) desiredWidth, (int) desiredHeight, bufferedIType);
    Graphics2D g2 = image.createGraphics();

    if (scale) {
        AffineTransform saved = g2.getTransform();
        g2.transform(AffineTransform.getScaleInstance(scaleX, scaleY));
        chart.draw(g2, new Rectangle2D.Double(0, 0, defaultWidth, defaultHeight), info);
        g2.setTransform(saved);
        g2.dispose();
    } else {
        chart.draw(g2, new Rectangle2D.Double(0, 0, defaultWidth, defaultHeight), info);
    }
    return image;
}

From source file:XORRectangles.java

public void paint(Graphics g) {
    Graphics2D g2 = (Graphics2D) g;

    // using white as the XOR color.
    g2.setXORMode(Color.white);// ww w . ja v a 2  s  . co  m
    // Paint a red rectangle.
    Rectangle2D r = new Rectangle2D.Double(50, 50, 150, 100);
    g2.setPaint(Color.red);
    g2.fill(r);
    g2.transform(AffineTransform.getTranslateInstance(25, 25));
    // Draw a blue rectangle.
    g2.setPaint(Color.blue);
    g2.fill(r);
}

From source file:MainClass.java

public void paint(Graphics g) {
    Graphics2D g2 = (Graphics2D) g;

    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

    g2.setPaint(Color.black);/*w  ww  . j av a2  s  .  c o  m*/
    g2.draw(shape);

    AffineTransform at = AffineTransform.getTranslateInstance(75, 75);
    g2.transform(at);

    g2.setPaint(Color.red);
    g2.draw(shape);

}

From source file:TextRendering.java

public void paint(Graphics g) {
    Graphics2D g2 = (Graphics2D) g;
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

    Dimension d = getSize();/*from  ww  w.jav a2  s.c o m*/
    AffineTransform ct = AffineTransform.getTranslateInstance(d.width / 2, d.height * 3 / 4);
    g2.transform(ct);

    String s = "www.java2s.com";
    Font f = new Font("Serif", Font.PLAIN, 128);
    g2.setFont(f);

    int count = 6;
    for (int i = 1; i <= count; i++) {
        AffineTransform oldTransform = g2.getTransform();

        float ratio = (float) i / (float) count;
        g2.transform(AffineTransform.getRotateInstance(Math.PI * (ratio - 1.0f)));
        float alpha = ((i == count) ? 1.0f : ratio / 3);
        g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha));
        g2.drawString(s, 0, 0);

        g2.setTransform(oldTransform);
    }
}

From source file:Main.java

public void paint(Graphics g) {
    Graphics2D g2 = (Graphics2D) g;

    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

    g2.setPaint(Color.black);/* ww w  . j  a  va 2 s .  c  o  m*/
    g2.draw(new Rectangle2D.Float(10, 20, 30, 40));

    AffineTransform at = AffineTransform.getTranslateInstance(75, 75);
    g2.transform(at);

    g2.setPaint(Color.red);
    g2.draw(new Rectangle2D.Float(10, 20, 30, 40));
}

From source file:TransformTranslation.java

public void paint(Graphics g) {
    Graphics2D g2 = (Graphics2D) g;

    // Use antialiasing.
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

    // Move the origin to 75, 75.
    AffineTransform at = AffineTransform.getTranslateInstance(75, 75);
    g2.transform(at);

    // Draw the shapes in their original locations.
    g2.setPaint(Color.black);/* w  w  w  . ja  va  2s  .  c o m*/
    g2.draw(axes);
    g2.draw(shape);

    // Transform the Graphics2D.
    g2.transform(AffineTransform.getTranslateInstance(150, 0));

    // Draw the "new" shapes in dashed.
    Stroke stroke = new BasicStroke(1, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL, 0, new float[] { 3, 1 },
            0);
    g2.setStroke(stroke);
    g2.draw(axes);
    g2.draw(shape);
}