List of usage examples for java.awt Graphics2D setTransform
public abstract void setTransform(AffineTransform Tx);
From source file:org.eclipse.swt.snippets.Snippet361.java
public static void main(String[] args) { final Display display = new Display(); final Shell shell = new Shell(display); shell.setText("Snippet 361"); shell.setText("Translate and Rotate an AWT Image in an SWT GUI"); shell.setLayout(new GridLayout(8, false)); Button fileButton = new Button(shell, SWT.PUSH); fileButton.setText("&Open Image File"); fileButton.addSelectionListener(widgetSelectedAdapter(e -> { String filename = new FileDialog(shell).open(); if (filename != null) { image = Toolkit.getDefaultToolkit().getImage(filename); canvas.repaint();/*from w w w .j a va2 s . c o m*/ } })); new Label(shell, SWT.NONE).setText("Translate &X by:"); final Combo translateXCombo = new Combo(shell, SWT.NONE); translateXCombo.setItems("0", "image width", "image height", "100", "200"); translateXCombo.select(0); translateXCombo.addModifyListener(e -> { translateX = numericValue(translateXCombo); canvas.repaint(); }); new Label(shell, SWT.NONE).setText("Translate &Y by:"); final Combo translateYCombo = new Combo(shell, SWT.NONE); translateYCombo.setItems("0", "image width", "image height", "100", "200"); translateYCombo.select(0); translateYCombo.addModifyListener(e -> { translateY = numericValue(translateYCombo); canvas.repaint(); }); new Label(shell, SWT.NONE).setText("&Rotate by:"); final Combo rotateCombo = new Combo(shell, SWT.NONE); rotateCombo.setItems("0", "Pi", "Pi/2", "Pi/4", "Pi/8"); rotateCombo.select(0); rotateCombo.addModifyListener(e -> { rotate = numericValue(rotateCombo); canvas.repaint(); }); Button printButton = new Button(shell, SWT.PUSH); printButton.setText("&Print Image"); printButton.addSelectionListener(widgetSelectedAdapter(e -> { performPrintAction(display, shell); })); composite = new Composite(shell, SWT.EMBEDDED | SWT.BORDER); GridData data = new GridData(SWT.FILL, SWT.FILL, true, true, 8, 1); data.widthHint = 640; data.heightHint = 480; composite.setLayoutData(data); Frame frame = SWT_AWT.new_Frame(composite); canvas = new Canvas() { @Override public void paint(Graphics g) { if (image != null) { g.setColor(Color.WHITE); g.fillRect(0, 0, canvas.getWidth(), canvas.getHeight()); /* Use Java2D here to modify the image as desired. */ Graphics2D g2d = (Graphics2D) g; AffineTransform t = new AffineTransform(); t.translate(translateX, translateY); t.rotate(rotate); g2d.setTransform(t); /*------------*/ g.drawImage(image, 0, 0, this); } } }; frame.add(canvas); composite.getAccessible().addAccessibleListener(new AccessibleAdapter() { @Override public void getName(AccessibleEvent e) { e.result = "Image drawn in AWT Canvas"; } }); shell.pack(); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); }
From source file:Main.java
public static BufferedImage getFlippedImage(BufferedImage bi) { BufferedImage flipped = new BufferedImage(bi.getWidth(), bi.getHeight(), bi.getType()); AffineTransform tran = AffineTransform.getTranslateInstance(0, bi.getHeight()); AffineTransform flip = AffineTransform.getScaleInstance(1d, -1d); tran.concatenate(flip);/*from w ww . ja va 2 s .c om*/ Graphics2D g = flipped.createGraphics(); g.setTransform(tran); g.drawImage(bi, 0, 0, null); g.dispose(); return flipped; }
From source file:Java2DUtils.java
public static void resetTransform(Graphics2D g2) { if (transforms.empty()) { return;//from w ww .j a va2 s . c om } g2.setTransform((AffineTransform) transforms.pop()); }
From source file:Java2DUtils.java
@SuppressWarnings("unchecked") public static void setTransform(Graphics2D g2, AffineTransform transform) { AffineTransform current;/*from ww w. j ava2 s .c o m*/ current = g2.getTransform(); transforms.push(current); g2.setTransform(transform); }
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. *///from w w w.ja v a 2 s. c o 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
private static BufferedImage renderRotatedObject(Object src, double angle, int width, int height, double tx, double ty) { BufferedImage dest = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); Graphics2D g2d = (Graphics2D) dest.getGraphics(); g2d.setColor(Color.black);/* w w w .j av a2 s . c o m*/ g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); AffineTransform at = AffineTransform.getRotateInstance(angle); at.translate(tx, ty); g2d.setTransform(at); if (src instanceof TextLayout) { TextLayout tl = (TextLayout) src; tl.draw(g2d, 0, tl.getAscent()); } else if (src instanceof Image) { g2d.drawImage((Image) src, 0, 0, null); } g2d.dispose(); return dest; }
From source file:com.alvermont.terraj.stargen.ui.UIUtils.java
/** * Scale an image to a particular X and Y size in pixels * /* w w w.jav a 2s .c om*/ * @param image The image to be scaled * @param pixelsX The desired horizontal size in pixels * @param pixelsY The desired vertical size in pixels * @return A new image scaled to the requested dimensions */ public static BufferedImage scaleImage(BufferedImage image, int pixelsX, int pixelsY) { int actualWidth = image.getWidth(); int actualHeight = image.getHeight(); // work out the scale factor double sx = (float) (pixelsX) / actualWidth; double sy = (float) (pixelsY) / actualHeight; BufferedImage nbi = new BufferedImage(pixelsX, pixelsY, image.getType()); Graphics2D g2 = nbi.createGraphics(); AffineTransform at = AffineTransform.getScaleInstance(sx, sy); g2.setTransform(at); g2.drawImage(image, 0, 0, null); return nbi; }
From source file:net.sf.mcf2pdf.mcfelements.util.ImageUtil.java
/** * Rotates the given buffered image by the given angle, and returns a newly * created image, containing the rotated image. * * @param img Image to rotate.//from w w w .ja va 2 s . c om * @param angle Angle, in radians, by which to rotate the image. * @param drawOffset Receives the offset which is required to draw the image, * relative to the original (0,0) corner, so that the center of the image is * still on the same position. * * @return A newly created image containing the rotated image. */ public static BufferedImage rotateImage(BufferedImage img, float angle, Point drawOffset) { int w = img.getWidth(); int h = img.getHeight(); AffineTransform tf = AffineTransform.getRotateInstance(angle, w / 2.0, h / 2.0); // get coordinates for all corners to determine real image size Point2D[] ptSrc = new Point2D[4]; ptSrc[0] = new Point(0, 0); ptSrc[1] = new Point(w, 0); ptSrc[2] = new Point(w, h); ptSrc[3] = new Point(0, h); Point2D[] ptTgt = new Point2D[4]; tf.transform(ptSrc, 0, ptTgt, 0, ptSrc.length); Rectangle rc = new Rectangle(0, 0, w, h); for (Point2D p : ptTgt) { if (p.getX() < rc.x) { rc.width += rc.x - p.getX(); rc.x = (int) p.getX(); } if (p.getY() < rc.y) { rc.height += rc.y - p.getY(); rc.y = (int) p.getY(); } if (p.getX() > rc.x + rc.width) rc.width = (int) (p.getX() - rc.x); if (p.getY() > rc.y + rc.height) rc.height = (int) (p.getY() - rc.y); } BufferedImage imgTgt = new BufferedImage(rc.width, rc.height, BufferedImage.TYPE_INT_ARGB); Graphics2D g2d = imgTgt.createGraphics(); // create a NEW rotation transformation around new center tf = AffineTransform.getRotateInstance(angle, rc.getWidth() / 2, rc.getHeight() / 2); g2d.setTransform(tf); g2d.drawImage(img, -rc.x, -rc.y, null); g2d.dispose(); drawOffset.x += rc.x; drawOffset.y += rc.y; return imgTgt; }
From source file:net.sf.mzmine.chartbasics.graphicsexport.ChartExportUtil.java
/** * Paints a chart with scaling options/*from w ww . ja v a2 s. com*/ * * @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:com.liusoft.dlog4j.util.ImageUtils.java
/** * ???/* w ww .java 2 s .co m*/ * ????? * 3: 180 * 6: 90 * 8: 27090 * @param img_fn * @param orient * @throws IOException */ public static boolean rotateImage(String img_fn, int orient, String dest_fn) throws IOException { double radian = 0; switch (orient) { case 3: radian = 180.0; break; case 6: radian = 90.0; break; case 8: radian = 270.0; break; default: return false; } BufferedImage old_img = (BufferedImage) ImageIO.read(new File(img_fn)); int width = old_img.getWidth(); int height = old_img.getHeight(); BufferedImage new_img = new BufferedImage(height, width, BufferedImage.TYPE_INT_RGB); Graphics2D g2d = new_img.createGraphics(); AffineTransform origXform = g2d.getTransform(); AffineTransform newXform = (AffineTransform) (origXform.clone()); // center of rotation is center of the panel double xRot = 0; double yRot = 0; switch (orient) { case 3: xRot = width / 2.0; yRot = height / 2.0; case 6: xRot = height / 2.0; yRot = xRot; break; case 8: xRot = width / 2.0; yRot = xRot; break; default: return false; } newXform.rotate(Math.toRadians(radian), xRot, yRot); g2d.setTransform(newXform); // draw image centered in panel g2d.drawImage(old_img, 0, 0, null); // Reset to Original g2d.setTransform(origXform); FileOutputStream out = new FileOutputStream(dest_fn); try { ImageIO.write(new_img, "JPG", out); } finally { out.close(); } return true; }