Home J2me Image Icon Using Canvas Example


Share on Google+Share on Google+

Image Icon Using Canvas Example

Image Icon Using Canvas Example

Image Icon Using Canvas Example


This example is used to create the Image on different location using Canvas class. In this example to create the image we are using following code:

And for draw the image we are using following code of lines:

private Image image1, image2, image3; 
image1 = Image.createImage("/cartoon.png"); 
image2 = Image.createImage("/girl-8-icon.png"); 
image3 = Image.createImage("/girl-icon.png");
g.drawImage(image1, 0, 0, Graphics.TOP | Graphics.LEFT);
g.drawImage(image2, getWidth() / 2, getHeight() / 2, Graphics.HCENTER | Graphics.VCENTER);
g.drawImage(image3, getWidth(), getHeight(), Graphics.BOTTOM | Graphics.RIGHT);

 The application is as follows:

Source Code of ImageItemExample2.java

import java.io.IOException;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

public class ImageItemExample2 extends MIDlet{
  private Display display;

  public void startApp(){
  display = Display.getDisplay(this);
  display.setCurrent(new ImageCanvas()); 

  public void pauseApp(){}

  public void destroyApp(boolean unconditional){

class ImageCanvas extends Canvas{
  private Image image1, image2, image3;

  public ImageCanvas(){
  image1 = Image.createImage("/cartoon.png");
  image2 = Image.createImage("/girl-8-icon.png");
  image3 = Image.createImage("/girl-icon.png");
  }catch (IOException e){
  throw new RuntimeException("Unable to load Image: "+e);

  public void paint(Graphics g){
  g.fillRect(0, 0, getWidth(), getHeight());
  g.drawImage(image1, 0, 0, Graphics.TOP | Graphics.LEFT);
  g.drawImage(image2, getWidth() / 2, getHeight() / 2, Graphics.HCENTER
 | Graphics.VCENTER);
  g.drawImage(image3, getWidth(), getHeight(), Graphics.BOTTOM
 | Graphics.RIGHT);

Download Source Code


If you enjoyed this post then why not add us on Google+? Add us to your Circles

Liked it!  Share this Tutorial

Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: April 18, 2011

Related Tutorials

Ask Questions?    Discuss: Image Icon Using Canvas Example  

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image