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:PaginationExample.java

public int print(Graphics g, PageFormat pf, int pageIndex) throws PrinterException {

    Font font = new Font("Serif", Font.PLAIN, 10);
    FontMetrics metrics = g.getFontMetrics(font);
    int lineHeight = metrics.getHeight();

    if (pageBreaks == null) {
        initTextLines();//from   w  w  w  .j a  v a 2 s. co m
        int linesPerPage = (int) (pf.getImageableHeight() / lineHeight);
        int numBreaks = (textLines.length - 1) / linesPerPage;
        pageBreaks = new int[numBreaks];
        for (int b = 0; b < numBreaks; b++) {
            pageBreaks[b] = (b + 1) * linesPerPage;
        }
    }

    if (pageIndex > pageBreaks.length) {
        return NO_SUCH_PAGE;
    }

    /*
     * User (0,0) is typically outside the imageable area, so we must translate
     * by the X and Y values in the PageFormat to avoid clipping Since we are
     * drawing text we
     */
    Graphics2D g2d = (Graphics2D) g;
    g2d.translate(pf.getImageableX(), pf.getImageableY());

    /*
     * Draw each line that is on this page. Increment 'y' position by lineHeight
     * for each line.
     */
    int y = 0;
    int start = (pageIndex == 0) ? 0 : pageBreaks[pageIndex - 1];
    int end = (pageIndex == pageBreaks.length) ? textLines.length : pageBreaks[pageIndex];
    for (int line = start; line < end; line++) {
        y += lineHeight;
        g.drawString(textLines[line], 0, y);
    }

    /* tell the caller that this page is part of the printed document */
    return PAGE_EXISTS;
}

From source file:HitTestSample.java

public void paintComponent(Graphics g) {
    super.paintComponent(g);
    setBackground(Color.white);// w ww.j a v  a 2s  . co m
    Graphics2D graphics2D = (Graphics2D) g;

    Point2D origin = computeLayoutOrigin();

    graphics2D.translate(origin.getX(), origin.getY());

    // Draw textLayout.
    textLayout.draw(graphics2D, 0, 0);

    // Retrieve caret Shapes for insertionIndex.
    Shape[] carets = textLayout.getCaretShapes(insertionIndex);

    graphics2D.setColor(STRONG_CARET_COLOR);
    graphics2D.draw(carets[0]);

    if (carets[1] != null) {
        graphics2D.setColor(WEAK_CARET_COLOR);
        graphics2D.draw(carets[1]);
    }
}

From source file:org.apache.fop.util.BitmapImageUtilTestCase.java

private BufferedImage createTestImage() {
    BufferedImage buf = new BufferedImage(640, 480, BufferedImage.TYPE_INT_RGB);
    Graphics2D g2d = buf.createGraphics();
    g2d.setBackground(Color.WHITE);
    g2d.clearRect(0, 0, buf.getWidth(), buf.getHeight());

    //A few rectangles rotated and with different color
    Graphics2D copy = (Graphics2D) g2d.create();
    copy.translate(170, 170);
    int c = 12;//from w w  w. j a va 2 s.c  om
    for (int i = 0; i < c; i++) {
        float f = ((i + 1) / (float) c);
        Color col = new Color(0.0f, 1 - f, 0.0f);
        copy.setColor(col);
        copy.fillRect(0, 0, 120, 120);
        copy.rotate(-2 * Math.PI / c);
    }
    copy.dispose();

    //the same in gray scales
    copy = (Graphics2D) g2d.create();
    copy.translate(470, 310);
    c = 12;
    for (int i = 0; i < c; i++) {
        float f = ((i + 1) / (float) c);
        Color col = new Color(f, f, f);
        copy.setColor(col);
        copy.fillRect(0, 0, 120, 120);
        copy.rotate(-2 * Math.PI / c);
    }
    copy.dispose();
    return buf;
}

From source file:RotatedText.java

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

    String s = "111111111111111111111111111111";

    Font font = new Font("Courier", Font.PLAIN, 12);
    g2d.translate(20, 20);

    FontRenderContext frc = g2d.getFontRenderContext();

    GlyphVector gv = font.createGlyphVector(frc, s);
    int length = gv.getNumGlyphs();
    System.out.println(length);/*from w  w w .  java  2  s.  c o  m*/

}

From source file:de.romankreisel.faktotum.beans.BundesbruderBean.java

public void rotateProfilePictureClockwise(BundesbruderEntity bundesbruder, double angle) throws IOException {
    BufferedImage image = this.getImageFromByteArray(bundesbruder.getPictureOriginal());
    double sin = Math.abs(Math.sin(angle)), cos = Math.abs(Math.cos(angle));
    int w = image.getWidth(), h = image.getHeight();
    int neww = (int) Math.floor(w * cos + h * sin), newh = (int) Math.floor(h * cos + w * sin);
    GraphicsConfiguration gc = image.createGraphics().getDeviceConfiguration();
    BufferedImage result = gc.createCompatibleImage(neww, newh, Transparency.TRANSLUCENT);
    Graphics2D g = result.createGraphics();
    g.translate((neww - w) / 2, (newh - h) / 2);
    g.rotate(angle, w / 2, h / 2);/*ww  w .ja va 2s .  co m*/
    g.drawRenderedImage(image, null);
    g.dispose();
    this.setProfilePicture(bundesbruder, this.storeImageToByteArray(image));
}

From source file:SwingTimerBasedAnimationScaleRotate.java

public void paint(Graphics g) {
    int h = getHeight();
    int w = getWidth();

    Graphics2D g2d = (Graphics2D) g;

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

    g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);

    g2d.translate(w / 2, h / 2);
    g2d.rotate(angle);//from  w w w. j  ava2s.c  o m
    g2d.scale(scale, scale);

    g2d.fill(r);
}

From source file:RotatedText.java

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

    String s = "111111111111111111111111111111";

    Font font = new Font("Courier", Font.PLAIN, 12);
    g2d.translate(20, 20);

    FontRenderContext frc = g2d.getFontRenderContext();

    GlyphVector gv = font.createGlyphVector(frc, s);
    int length = gv.getNumGlyphs();
    for (int i = 0; i < length; i++) {
        Point2D p = gv.getGlyphPosition(i);
        AffineTransform at = AffineTransform.getTranslateInstance(p.getX(), p.getY());
        at.rotate((double) i / (double) (length - 1) * Math.PI / 3);

        Shape glyph = gv.getGlyphOutline(i);
        Shape transformedGlyph = at.createTransformedShape(glyph);
        g2d.fill(transformedGlyph);//from  w  w w  .  j  a  v a  2  s .  c  om
    }
}

From source file:Main.java

public void paint(Graphics g, JComponent c) {

    JLabel label = (JLabel) c;
    String text = label.getText();
    Icon icon = (label.isEnabled()) ? label.getIcon() : label.getDisabledIcon();

    if ((icon == null) && (text == null)) {
        return;/*w w w  .  ja  v a2 s . c  om*/
    }

    FontMetrics fm = g.getFontMetrics();
    paintViewInsets = c.getInsets(paintViewInsets);

    paintViewR.x = paintViewInsets.left;
    paintViewR.y = paintViewInsets.top;

    // Use inverted height & width
    paintViewR.height = c.getWidth() - (paintViewInsets.left + paintViewInsets.right);
    paintViewR.width = c.getHeight() - (paintViewInsets.top + paintViewInsets.bottom);

    paintIconR.x = paintIconR.y = paintIconR.width = paintIconR.height = 0;
    paintTextR.x = paintTextR.y = paintTextR.width = paintTextR.height = 0;

    String clippedText = layoutCL(label, fm, text, icon, paintViewR, paintIconR, paintTextR);

    Graphics2D g2 = (Graphics2D) g;
    AffineTransform tr = g2.getTransform();
    if (clockwise) {
        g2.rotate(Math.PI / 2);
        g2.translate(0, -c.getWidth());
    } else {
        g2.rotate(-Math.PI / 2);
        g2.translate(-c.getHeight(), 0);
    }

    if (icon != null) {
        icon.paintIcon(c, g, paintIconR.x, paintIconR.y);
    }

    if (text != null) {
        int textX = paintTextR.x;
        int textY = paintTextR.y + fm.getAscent();

        if (label.isEnabled()) {
            paintEnabledText(label, g, clippedText, textX, textY);
        } else {
            paintDisabledText(label, g, clippedText, textX, textY);
        }
    }

    g2.setTransform(tr);
}

From source file:Main.java

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

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

    int cx = getSize().width / 2;
    int cy = getSize().height / 2;

    g2.translate(cx, cy);
    g2.rotate(theta * Math.PI / 180);

    Shape oldClip = g2.getClip();
    Shape e = new Ellipse2D.Float(-cx, -cy, cx * 2, cy * 2);
    g2.clip(e);//from  ww w .  j  a v  a  2  s .com

    Shape c = new Ellipse2D.Float(-cx, -cy, cx * 3 / 4, cy * 2);
    g2.setPaint(new GradientPaint(40, 40, Color.blue, 60, 50, Color.white, true));
    g2.fill(c);

    g2.setPaint(Color.yellow);
    g2.fillOval(cx / 4, 0, cx, cy);

    g2.setClip(oldClip);

    g2.setFont(new Font("Times New Roman", Font.PLAIN, 64));
    g2.setPaint(new GradientPaint(-cx, 0, Color.red, cx, 0, Color.black, false));
    g2.drawString("Hello, 2D!", -cx * 3 / 4, cy / 4);

    AlphaComposite ac = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, (float) .75);
    g2.setComposite(ac);

    Shape r = new RoundRectangle2D.Float(0, -cy * 3 / 4, cx * 3 / 4, cy * 3 / 4, 20, 20);
    g2.setStroke(new BasicStroke(4));
    g2.setPaint(Color.magenta);
    g2.fill(r);
    g2.setPaint(Color.green);
    g2.draw(r);

    g2.drawImage(image, -cx / 2, -cy / 2, this);
}

From source file:org.shredzone.commons.captcha.impl.DefaultCaptchaGenerator.java

/**
 * Draws a single character./*from   w w  w .  j ava 2  s  .c o  m*/
 *
 * @param g2d
 *            {@link Graphics2D} context
 * @param ch
 *            character to draw
 * @param x
 *            left x position of the character
 * @param boxWidth
 *            width of the box
 */
private void drawCharacter(Graphics2D g2d, char ch, int x, int boxWidth) {
    double degree = (rnd.nextDouble() * rotationAmplitude * 2) - rotationAmplitude;
    double scale = 1 - (rnd.nextDouble() * scaleAmplitude / 100);

    Graphics2D cg2d = (Graphics2D) g2d.create();
    cg2d.setFont(font.deriveFont(fontSize));

    cg2d.translate(x + (boxWidth / 2), height / 2);
    cg2d.rotate(degree * PI / 90);
    cg2d.scale(scale, scale);

    FontMetrics fm = cg2d.getFontMetrics();
    int charWidth = fm.charWidth(ch);
    int charHeight = fm.getAscent() + fm.getDescent();

    cg2d.drawString(String.valueOf(ch), -(charWidth / 2), fm.getAscent() - (charHeight / 2));

    cg2d.dispose();
}