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

public static void main(String[] args) {
    final Graphics2DRenderer renderer = new Graphics2DRenderer();
    Shell shell = new Shell();
    shell.setSize(350, 350);// w  ww .  j  a  v a 2  s.  c o m

    shell.open();
    shell.setText("Draw2d Hello World");
    LightweightSystem lws = new LightweightSystem(shell);
    IFigure figure = new Figure() {
        protected void paintClientArea(org.eclipse.draw2d.Graphics graphics) {
            Dimension controlSize = getSize();

            renderer.prepareRendering(graphics);
            // prepares the Graphics2D renderer

            // gets the Graphics2D context and switch on the antialiasing
            Graphics2D g2d = renderer.getGraphics2D();
            g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);

            // paints the background with a color gradient
            g2d.setPaint(new GradientPaint(0.0f, 0.0f, java.awt.Color.yellow, (float) controlSize.width,
                    (float) controlSize.width, java.awt.Color.white));
            g2d.fillRect(0, 0, controlSize.width, controlSize.width);

            // draws rotated text
            g2d.setFont(new java.awt.Font("SansSerif", java.awt.Font.BOLD, 16));
            g2d.setColor(java.awt.Color.blue);

            g2d.translate(controlSize.width / 2, controlSize.width / 2);
            int nbOfSlices = 18;
            for (int i = 0; i < nbOfSlices; i++) {
                g2d.drawString("Angle = " + (i * 360 / nbOfSlices) + "\u00B0", 30, 0);
                g2d.rotate(-2 * Math.PI / nbOfSlices);
            }

            // now that we are done with Java2D, renders Graphics2D
            // operation
            // on the SWT graphics context
            renderer.render(graphics);

            // now we can continue with pure SWT paint operations
            graphics.drawOval(0, 0, controlSize.width, controlSize.width);
        }
    };
    lws.setContents(figure);
    Display display = Display.getDefault();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    renderer.dispose();
}

From source file:SWTTest.java

public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setSize(350, 350);/*from   www . jav a2  s.  c o  m*/
    shell.setLayout(new GridLayout());
    Canvas canvas = new Canvas(shell, SWT.NO_BACKGROUND);
    GridData data = new GridData(GridData.FILL_BOTH);
    canvas.setLayoutData(data);

    final Graphics2DRenderer renderer = new Graphics2DRenderer();

    canvas.addPaintListener(new PaintListener() {
        public void paintControl(PaintEvent e) {
            Point controlSize = ((Control) e.getSource()).getSize();

            GC gc = e.gc; // gets the SWT graphics context from the event

            renderer.prepareRendering(gc); // prepares the Graphics2D
            // renderer

            // gets the Graphics2D context and switch on the antialiasing
            Graphics2D g2d = renderer.getGraphics2D();
            g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);

            // paints the background with a color gradient
            g2d.setPaint(new GradientPaint(0.0f, 0.0f, java.awt.Color.yellow, (float) controlSize.x,
                    (float) controlSize.y, java.awt.Color.white));
            g2d.fillRect(0, 0, controlSize.x, controlSize.y);

            // draws rotated text
            g2d.setFont(new java.awt.Font("SansSerif", java.awt.Font.BOLD, 16));
            g2d.setColor(java.awt.Color.blue);

            g2d.translate(controlSize.x / 2, controlSize.y / 2);
            int nbOfSlices = 18;
            for (int i = 0; i < nbOfSlices; i++) {
                g2d.drawString("Angle = " + (i * 360 / nbOfSlices) + "\u00B0", 30, 0);
                g2d.rotate(-2 * Math.PI / nbOfSlices);
            }

            // now that we are done with Java2D, renders Graphics2D
            // operation
            // on the SWT graphics context
            renderer.render(gc);

            // now we can continue with pure SWT paint operations
            gc.drawOval(0, 0, controlSize.x, controlSize.y);

        }
    });

    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
    renderer.dispose();
    System.exit(0);
}

From source file:Main.java

public static BufferedImage create(BufferedImage image, double angle, GraphicsConfiguration gc) {
    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);
    int transparency = image.getColorModel().getTransparency();
    BufferedImage result = gc.createCompatibleImage(neww, newh, transparency);
    Graphics2D g = result.createGraphics();
    g.translate((neww - w) / 2, (newh - h) / 2);
    g.rotate(angle, w / 2, h / 2);//from   w w w . j av  a  2 s .  co m
    g.drawRenderedImage(image, null);
    return result;
}

From source file:Main.java

public static BufferedImage createImage(JTable table) {
    JTableHeader tableHeaderComp = table.getTableHeader();
    int totalWidth = tableHeaderComp.getWidth() + table.getWidth();
    int totalHeight = tableHeaderComp.getHeight() + table.getHeight();
    BufferedImage tableImage = new BufferedImage(totalWidth, totalHeight, BufferedImage.TYPE_INT_RGB);
    Graphics2D g2D = (Graphics2D) tableImage.getGraphics();
    tableHeaderComp.paint(g2D);//from ww  w .  j  ava 2  s.  c om
    g2D.translate(0, tableHeaderComp.getHeight());
    table.paint(g2D);
    return tableImage;
}

From source file:org.jfree.graphics2d.demo.ImageTest.java

private static void drawClipTest(Graphics2D g2) {
    g2.translate(10, 20);
    g2.setColor(Color.RED);/*  ww  w .  j  av a  2s.c om*/
    g2.fillRect(10, 10, 100, 100);
    g2.clip(new Rectangle(0, 0, 60, 60));
    g2.setPaint(Color.BLUE);
    g2.fillRect(10, 10, 100, 100);
    g2.setClip(null);
    g2.setPaint(Color.GREEN);
    g2.fillRect(60, 60, 50, 50);
}

From source file:org.mabb.fontverter.opentype.DebugGlyphDrawer.java

public static void drawGlyph(TtfGlyph glyph) throws IOException {
    BufferedImage image = new BufferedImage(650, 650, BufferedImage.TYPE_INT_RGB);
    Graphics2D gfx = image.createGraphics();

    gfx.translate(0, 300);
    gfx.scale(1, -1);/*from w  w w  .  ja  va 2 s .  co m*/

    gfx.setColor(Color.white);
    gfx.fillRect(0, -1000, 2060, 2060);
    gfx.setColor(Color.lightGray);
    gfx.translate(100, 50);
    gfx.fillRect(0, 0, 1000, 1000);
    gfx.setColor(Color.BLACK);

    gfx.scale(.05, .05);

    //        gfx.rotate(Math.toRadians(180));
    //        gfx.translate(-2200, -2200);

    Color[] colors = new Color[] { Color.BLACK, Color.MAGENTA, Color.GREEN, Color.BLUE, Color.cyan };
    java.util.List<Path2D.Double> paths = glyph.getPaths();
    for (int i = 0; i < paths.size(); i++) {
        Path2D pathOn = paths.get(i);
        gfx.setColor(colors[i]);

        gfx.draw(pathOn);
    }

    gfx.dispose();

    //        ImageIO.write(image, "jpg", new File("test.jpg"));
}

From source file:com.ables.pix.utility.PictureOps.java

public static BufferedImage tilt(BufferedImage image, double rotation) {
    double sin = Math.abs(Math.sin(getAngle()));
    double cos = Math.abs(Math.cos(getAngle()));
    int w = image.getWidth(), h = image.getHeight();

    int neww = (int) Math.floor(w * cos + sin * h), newh = (int) Math.floor(h * cos + sin * w);
    GraphicsConfiguration gc = getDefaultConfiguration();
    BufferedImage rotated = gc.createCompatibleImage(neww, newh);
    Graphics2D g = rotated.createGraphics();
    g.translate((neww - w) / 2, (newh - h / 2));
    g.rotate(getAngle(), w / 2, h / 2);// w ww .  j  ava 2 s  . co  m
    g.drawRenderedImage(image, null);
    g.dispose();
    return rotated;
}

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

public static BufferedImage readImage(File imageFile) throws IOException {
    int rotation = getImageRotation(imageFile);
    BufferedImage img = ImageIO.read(imageFile);

    if (rotation == 0) {
        return img;
    }/*from  w  ww  . j  a  va 2 s. c o m*/

    boolean swapXY = rotation != 180;

    BufferedImage rotated = new BufferedImage(swapXY ? img.getHeight() : img.getWidth(),
            swapXY ? img.getWidth() : img.getHeight(), BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2d = rotated.createGraphics();
    g2d.translate((rotated.getWidth() - img.getWidth()) / 2, (rotated.getHeight() - img.getHeight()) / 2);
    g2d.rotate(Math.toRadians(rotation), img.getWidth() / 2, img.getHeight() / 2);

    g2d.drawImage(img, 0, 0, null);
    g2d.dispose();

    return rotated;
}

From source file:table.FrequencyTablePanel.java

public static BufferedImage createImage(JTable table) {
    JTableHeader tableHeaderComp = table.getTableHeader();
    int totalWidth = tableHeaderComp.getWidth();
    int totalHeight = tableHeaderComp.getHeight() + table.getHeight();
    BufferedImage tableImage = new BufferedImage(totalWidth, totalHeight, BufferedImage.TYPE_INT_RGB);
    Graphics2D g2D = (Graphics2D) tableImage.getGraphics();
    tableHeaderComp.paint(g2D);//from w ww.j a v a  2 s . c o  m
    g2D.translate(0, tableHeaderComp.getHeight());
    table.paint(g2D);
    return tableImage;
}

From source file:org.apache.fop.visual.BitmapComparator.java

/**
 * Builds a combined image that places a number of images next to each other for
 * manual, visual comparison.//from ww  w . j a va 2 s  .com
 * @param images the array of bitmaps
 * @return the combined image
 */
public static BufferedImage buildCompareImage(BufferedImage[] images) {
    BufferedImage cmp = new BufferedImage(images[0].getWidth() * images.length, images[0].getHeight(),
            BufferedImage.TYPE_INT_ARGB);

    Graphics2D g = cmp.createGraphics();
    g.setPaint(Color.white);
    g.fillRect(0, 0, cmp.getWidth(), cmp.getHeight());
    int lastWidth = 0;
    for (int i = 0; i < images.length; i++) {
        if (lastWidth > 0) {
            g.translate(lastWidth, 0);
        }
        if (images[i] != null) {
            g.drawImage(images[i], 0, 0, null);
            lastWidth = images[i].getWidth();
        } else {
            lastWidth = 20; //Maybe add a special placeholder image instead later
        }
    }
    g.dispose();

    return cmp;
}