Home » Desktop Java » awt » image » Scaling Shearing Translating and Rotating a Buffered Image

About Ilias Tsagklis

Avatar photo
Ilias is a software developer turned online entrepreneur. He is co-founder and Executive Editor at Java Code Geeks.

Scaling Shearing Translating and Rotating a Buffered Image

With this example we shall show you four fundamental graphical transformations that you should know when you try to construct you own custom graphical environment or develop simple graphics.

In short in order to perform Scaling, Shearing, Translation and Rotation to a Buffered Image one should take the following steps:

  • Load an image from a source using Toolkit.getDefaultToolkit().getImage method
  • Use an ImageObserver to monitor the loading of the image. When the image is fully load the user will be notified
  • Create a buffed image from the source image with a format more close to the custom display environment using GraphicsEnvironmentGraphicsDevice and GraphicsConfiguration to perform several image configurations
  • Use AffineTransform class and its methods scale, translate, shear, rotate to set up the transformations
  • Use AffineTransformOp class an its method filter to perform this transformations to the buffered Image
  • And simply paint the buffered image in a new Frame
Now let’s take a look at the code:
package com.javacodegeeks.snippets.desktop;

import java.awt.Component;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.Transparency;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.awt.image.ImageObserver;

public class BufferedImageTransformations {

    static BufferedImage image;
    static boolean imageLoaded = false;

    public static void main(String[] args) {

  // The ImageObserver implementation to observe loading of the image

  ImageObserver myImageObserver = new ImageObserver() {

    public boolean imageUpdate(Image image, int flags, int x, int y, int width, int height) {

if ((flags & ALLBITS) != 0) {

  imageLoaded = true;

  System.out.println("Image loading finished!");

  return false;


return true;



  // The image URL - change to where your image file is located!

  String imageURL = "image.png";


   * This call returns immediately and pixels are loaded in the background

   * We use an ImageObserver to be notified when the loading of the image

   * is complete


  Image sourceImage = Toolkit.getDefaultToolkit().getImage(imageURL);


  // We wait until the image is fully loaded

  while (!imageLoaded) {

try {


} catch (InterruptedException e) {



  // Create a buffered image from the source image with a format that's compatible with the screen

  GraphicsEnvironment graphicsEnvironment = GraphicsEnvironment.getLocalGraphicsEnvironment();

  GraphicsDevice graphicsDevice = graphicsEnvironment.getDefaultScreenDevice();

  GraphicsConfiguration graphicsConfiguration = graphicsDevice.getDefaultConfiguration();

  // If the source image has no alpha info use Transparency.OPAQUE instead

  image = graphicsConfiguration.createCompatibleImage(sourceImage.getWidth(null), sourceImage.getHeight(null), Transparency.BITMASK);

  // Copy image to buffered image

  Graphics graphics = image.createGraphics();

  // Paint the image onto the buffered image

  graphics.drawImage(sourceImage, 0, 0, null);


  // Image transformations

  double scalex = 1.2;

  double scaley = 0.8;

  double shiftx = 1.5;

  double shifty = 1.8;

  double x = 2.2;

  double y = 1.2;

  double radians = 0.22;

  AffineTransform tx = new AffineTransform();

  tx.scale(scalex, scaley);

  tx.shear(shiftx, shifty);

  tx.translate(x, y);

  tx.rotate(radians, image.getWidth()/2, image.getHeight()/2);

  AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_BILINEAR);

  image = op.filter(image, null);

  // Create frame with specific title

  Frame frame = new Frame("Example Frame");

  // Add a component with a custom paint method

  frame.add(new CustomPaintComponent());

  // Display the frame

  int frameWidth = 300;

  int frameHeight = 300;

  frame.setSize(frameWidth, frameHeight);



     * To draw on the screen, it is first necessary to subclass a Component and
     * override its paint() method. The paint() method is automatically called
     * by the windowing system whenever component's area needs to be repainted.
    static class CustomPaintComponent extends Component {

  public void paint(Graphics g) {

// Retrieve the graphics context; this object is used to paint

// shapes

Graphics2D g2d = (Graphics2D) g;


 * Draw an Image object The coordinate system of a graphics context

 * is such that the origin is at the northwest corner and x-axis

 * increases toward the right while the y-axis increases toward the

 * bottom.


int x = 0;

int y = 0;

g2d.drawImage(image, x, y, this);




This was an example on how to Scale, Shear, translate and rotate a Buffered image.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!


1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design


and many more ....


Receive Java & Developer job alerts in your Area

I have read and agree to the terms & conditions


Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Inline Feedbacks
View all comments