Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries. Thanks for all your past and future contributions.

Rotating images in Java ME

From Wiki
Jump to: navigation, search

This code snippet demonstrates how to rotate images in Java ME applications.

Article Metadata
Code ExampleTested with
Devices(s): Nokia 6131, Nokia E70, Nokia C3-01, Nokia Asha 306, Nokia E7-00
Created: dekudin (27 Nov 2008)
Last edited: hamishwillee (30 Jul 2013)



To rotate an image to a specified angle, a new image must be created by calling Image.createImage(Image sourceImage, int x, int y, int width, int height, int transform) method with source image passed as the sourceImage parameter and angle value passed as the transform parameter.

Some possible values of transform parameter are:

  1. Sprite.TRANS_NONE - causes the specified image region to be copied unchanged
  2. Sprite.TRANS_ROT90 - causes the specified image region to be rotated clockwise by 90 degrees.
  3. Sprite.TRANS_ROT180 - causes the specified image region to be rotated clockwise by 180 degrees.
  4. Sprite.TRANS_ROT270 - causes the specified image region to be rotated clockwise by 270 degrees.

Source file:

import javax.microedition.midlet.MIDlet;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Image;
public class ImageRotating extends MIDlet implements CommandListener {
private Display display;
// Form where image is placed
private Form form;
// Command for rotating source image.
private Command cmdRotate0;
private Command cmdRotate90;
private Command cmdRotate180;
private Command cmdRotate270;
// Command for exiting from midlet.
private Command cmdExit;
private Image sourceImage;
private Image destImage;
* Constructor

public ImageRotating() {
* Initializes components of midlet

private void initializeComponents() {
// Get display
display = Display.getDisplay(this);
// Load image
try {
sourceImage = Image.createImage("/image.jpg");
} catch (IOException e) {
// Create form
form = new Form("Image rotating");
cmdRotate0 = new Command("Rotate 0", Command.SCREEN, 0);
cmdRotate90 = new Command("Rotate 90", Command.SCREEN, 0);
cmdRotate180 = new Command("Rotate 180", Command.SCREEN, 0);
cmdRotate270 = new Command("Rotate 270", Command.SCREEN, 0);
cmdExit = new Command("Exit", Command.EXIT, 0);
* Rotates image and display it in the form.
* @param rotateType - angle of rotation.

private void rotateImage(int transform) {
destImage = Image.createImage(sourceImage, 0, 0,
sourceImage.getWidth(), sourceImage.getHeight(), transform);
* From MIDlet.
* Signals the MIDlet that it has entered the Active state.

public void startApp() {
* From MIDlet.
* Signals the MIDlet to enter the Paused state.

public void pauseApp() {
// No implementation required
* From MIDlet.
* Signals the MIDlet to terminate and enter the Destroyed state.

public void destroyApp(boolean unconditional) {
// No implementation required
* Performs exit from midlet.

private void exitMIDlet() {
* From CommandListener.
* Indicates that a command event has occurred on Displayable d.
* @param cmd - a Command object identifying the command.
* @param d - the Displayable on which this event has occurred.

public void commandAction(Command cmd, Displayable d) {
if(cmd == cmdRotate0) {
} else if(cmd == cmdRotate90) {
} else if(cmd == cmdRotate180) {
} else if(cmd == cmdRotate270) {
} else if(cmd == cmdExit) {


After loading the snippet image, now rotated, is shown on display.

By selecting the menu commands "Rotate 0" - "Rotate 270" the user can rotate the source image to a specific angle. After this, the rotated image will be shown on display.

Supplementary material

Executables and source files can be found here:

This page was last modified on 30 July 2013, at 08:17.
77 page views in the last 30 days.