Example usage for java.awt Graphics2D translate

List of usage examples for java.awt Graphics2D translate

Introduction

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

Prototype

public abstract void translate(double tx, double ty);

Source Link

Document

Concatenates the current Graphics2D Transform with a translation transform.

Usage

From source file:D20140128.ApacheXMLGraphicsTest.TilingPatternExample.java

private void paintText(Graphics2D g2d) {
    g2d.setPaint(paint);//  ww  w  .  jav  a  2 s  . co m
    Font font = new Font("serif", Font.BOLD, 80);
    GlyphVector gv = font.createGlyphVector(g2d.getFontRenderContext(), "Java");
    g2d.translate(100, 180);
    g2d.fill(gv.getOutline());
}

From source file:RollingText.java

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

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

    String s = "Java Source and Support.";
    Font font = new Font("Serif", Font.PLAIN, 24);
    FontRenderContext frc = g2.getFontRenderContext();
    g2.translate(40, 80);

    GlyphVector gv = font.createGlyphVector(frc, s);
    int length = gv.getNumGlyphs();
    for (int i = 0; i < length; i++) {
        Point2D p = gv.getGlyphPosition(i);
        double theta = (double) i / (double) (length - 1) * Math.PI / 4;
        AffineTransform at = AffineTransform.getTranslateInstance(p.getX(), p.getY());
        at.rotate(theta);//from  w ww .  j ava  2 s.c  om
        Shape glyph = gv.getGlyphOutline(i);
        Shape transformedGlyph = at.createTransformedShape(glyph);
        g2.fill(transformedGlyph);
    }
}

From source file:Main.java

@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2d = (Graphics2D) g.create();
    if (myImage != null) {
        int x = (getWidth() - myImage.getWidth()) / 2;
        int y = (getHeight() - myImage.getHeight()) / 2;
        g2d.drawImage(myImage, x, y, this);

        g2d.setColor(Color.RED);//w ww . j  av a 2  s  .  co m
        g2d.translate(x, y);
        g2d.draw(myOffice);
    }
    g2d.dispose();
}

From source file:Main.java

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

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

    String s = "www.java2s.com www.java2s.com";
    Font font = new Font("Serif", Font.PLAIN, 24);
    FontRenderContext frc = g2.getFontRenderContext();
    g2.translate(40, 80);

    GlyphVector gv = font.createGlyphVector(frc, s);
    int length = gv.getNumGlyphs();
    for (int i = 0; i < length; i++) {
        Point2D p = gv.getGlyphPosition(i);
        double theta = (double) i / (double) (length - 1) * Math.PI / 4;
        AffineTransform at = AffineTransform.getTranslateInstance(p.getX(), p.getY());
        at.rotate(theta);/*from w  ww .  j  av  a  2s  .  co m*/
        Shape glyph = gv.getGlyphOutline(i);
        Shape transformedGlyph = at.createTransformedShape(glyph);
        g2.fill(transformedGlyph);
    }
}

From source file:org.uiautomation.ios.utils.InstrumentsGeneratedImage.java

private BufferedImage rotate() {
    int rotateDegrees = orientation.getRotationInDegree();
    boolean flip = orientation == Orientation.LANDSCAPE
            || orientation == Orientation.UIA_DEVICE_ORIENTATION_LANDSCAPERIGHT;
    try {/* w w w  .j av a2 s.  c  o  m*/
        final BufferedImage originalImage = ImageIO.read(source);

        // no rotation needed.
        if (rotateDegrees == 0) {
            return originalImage;
        }

        // need to rotate.
        final BufferedImage rotated;
        int width;
        int height;
        if (flip) {
            width = originalImage.getHeight();
            height = originalImage.getWidth();
        } else {
            width = originalImage.getWidth();
            height = originalImage.getHeight();
        }
        rotated = new BufferedImage(width, height, originalImage.getType());

        // Rotate the image and then move it back up to the origin through a translation call, since it'll pivot around
        // the center point which will cause non-square images to offset by the different in height and width.
        final Graphics2D graphics = rotated.createGraphics();
        graphics.rotate(Math.toRadians(rotateDegrees), rotated.getWidth() / 2, rotated.getHeight() / 2);
        graphics.translate((rotated.getWidth() - originalImage.getWidth()) / 2,
                (rotated.getHeight() - originalImage.getHeight()) / 2);
        graphics.drawImage(originalImage, 0, 0, originalImage.getWidth(), originalImage.getHeight(), null);
        return rotated;
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

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

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

    String s = "www.java2s.com www.java2s.com";
    Font font = new Font("Serif", Font.PLAIN, 24);
    FontRenderContext frc = g2.getFontRenderContext();
    g2.translate(40, 80);

    GlyphVector gv = font.createGlyphVector(frc, s);
    System.out.println(gv.getFont());

    int length = gv.getNumGlyphs();
    for (int i = 0; i < length; i++) {
        Point2D p = gv.getGlyphPosition(i);
        double theta = (double) i / (double) (length - 1) * Math.PI / 4;
        AffineTransform at = AffineTransform.getTranslateInstance(p.getX(), p.getY());
        at.rotate(theta);/*from  ww w  .  ja  va 2  s  .  co m*/
        Shape glyph = gv.getGlyphOutline(i);
        Shape transformedGlyph = at.createTransformedShape(glyph);
        g2.fill(transformedGlyph);
    }
}

From source file:Main.java

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

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

    String s = "www.java2s.com www.java2s.com";
    Font font = new Font("Serif", Font.PLAIN, 24);
    FontRenderContext frc = g2.getFontRenderContext();
    g2.translate(40, 80);

    GlyphVector gv = font.createGlyphVector(frc, s);
    System.out.println(gv.getOutline());

    int length = gv.getNumGlyphs();
    for (int i = 0; i < length; i++) {
        Point2D p = gv.getGlyphPosition(i);
        double theta = (double) i / (double) (length - 1) * Math.PI / 4;
        AffineTransform at = AffineTransform.getTranslateInstance(p.getX(), p.getY());
        at.rotate(theta);/*from   w  w  w . java2s  .  c  om*/
        Shape glyph = gv.getGlyphOutline(i);
        Shape transformedGlyph = at.createTransformedShape(glyph);
        g2.fill(transformedGlyph);
    }
}

From source file:Main.java

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

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

    String s = "www.java2s.com www.java2s.com";
    Font font = new Font("Serif", Font.PLAIN, 24);
    FontRenderContext frc = g2.getFontRenderContext();
    g2.translate(40, 80);

    GlyphVector gv = font.createGlyphVector(frc, s);
    System.out.println(gv.getVisualBounds());

    int length = gv.getNumGlyphs();
    for (int i = 0; i < length; i++) {
        Point2D p = gv.getGlyphPosition(i);
        double theta = (double) i / (double) (length - 1) * Math.PI / 4;
        AffineTransform at = AffineTransform.getTranslateInstance(p.getX(), p.getY());
        at.rotate(theta);//from w w w  . j  a  v a 2  s.  com
        Shape glyph = gv.getGlyphOutline(i);
        Shape transformedGlyph = at.createTransformedShape(glyph);
        g2.fill(transformedGlyph);
    }
}

From source file:Main.java

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

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

    String s = "www.java2s.com www.java2s.com";
    Font font = new Font("Serif", Font.PLAIN, 24);
    FontRenderContext frc = g2.getFontRenderContext();
    g2.translate(40, 80);

    GlyphVector gv = font.createGlyphVector(frc, s);
    System.out.println(gv.getLogicalBounds());

    int length = gv.getNumGlyphs();
    for (int i = 0; i < length; i++) {
        Point2D p = gv.getGlyphPosition(i);
        double theta = (double) i / (double) (length - 1) * Math.PI / 4;
        AffineTransform at = AffineTransform.getTranslateInstance(p.getX(), p.getY());
        at.rotate(theta);/* w w w  .j  av  a 2s  .co m*/
        Shape glyph = gv.getGlyphOutline(i);
        Shape transformedGlyph = at.createTransformedShape(glyph);
        g2.fill(transformedGlyph);
    }
}

From source file:PrintableComponent.java

/**
 * This is the "callback" method that the PrinterJob will invoke.
 * This method is defined by the Printable interface.
 *///  ww  w. j a  va 2s. c o m
public int print(Graphics g, PageFormat format, int pagenum) {
    // The PrinterJob will keep trying to print pages until we return
    // this value to tell it that it has reached the end.
    if (pagenum > 0)
        return Printable.NO_SUCH_PAGE;

    // We're passed a Graphics object, but it can always be cast to Graphics2D
    Graphics2D g2 = (Graphics2D) g;

    // Use the top and left margins specified in the PageFormat Note
    // that the PageFormat methods are poorly named.  They specify
    // margins, not the actual imageable area of the printer.
    g2.translate(format.getImageableX(), format.getImageableY());

    // Tell the component to draw itself to the printer by passing in 
    // the Graphics2D object.  This will not work well if the component
    // has double-buffering enabled.
    c.paint(g2);

    // Return this constant to tell the PrinterJob that we printed the page.
    return Printable.PAGE_EXISTS;
}