Home J2me User Define Alert Example
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

User Define Alert Example

Advertisement
Creating a user defined alert message, In the given example we have created a image that can be fill with some colors and text.

User Define Alert Example

     

Creating a user defined alert message, In the given example we have created a image that can be fill with some colors and text. The following source code shows how to create the image, how to set the color and how to draw string. On clicking Alert option, a alert message will be displayed with image on the same screen.

im = Image.createImage(240220);
Graphics graphics = im.getGraphics();
Font font = Font.getFont(Font.FACE_SYSTEM, 
Font.STYLE_PLAIN, Font.SIZE_MEDIUM);

graphics.setFont(font);
graphics.setColor(196248192);
graphics.fillRoundRect(0,0, im.getWidth()-1
im.getHeight()-
12020); 
graphics.setColor(000); 
graphics.drawString(message, (im.getWidth()/2
- (font.stringWidth(message)/
2),
0, Graphics.TOP | Graphics.LEFT);

 

The Application is as follows:

 

 

 

Source Code of AlertExample.java

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

public class AlertExample extends MIDlet{
  private Display  display; 
  private ImageCanvas canvas;
  
  public AlertExample(){
  display = Display.getDisplay(this);
  canvas  = new ImageCanvas(this);
  }

  protected void startApp(){
  display.setCurrent( canvas );
  }

  protected void pauseApp(){ }

  protected void destroyApp(boolean unconditional){ }

  public void exitMIDlet(){
  destroyApp(true);
  notifyDestroyed();
  }
}

class ImageCanvas extends Canvas implements CommandListener{
  private Command cmExit;  
  private AlertExample midlet;
  private Image im = null;
  private String message = "This is alert message";
  
  public ImageCanvas(AlertExample midlet){
  this.midlet = midlet;

  cmExit = new Command("Exit", Command.EXIT, 1);
  addCommand(cmExit);
  setCommandListener(this);

  try{
  im = Image.createImage(240, 220);
  Graphics graphics = im.getGraphics();
  Font font = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_MEDIUM);
  graphics.setFont(font);
  graphics.setColor(196, 248, 192);
  graphics.fillRoundRect(0,0, im.getWidth()-1, im.getHeight()-1, 20, 20); 
  graphics.setColor(0, 0, 0); 
  graphics.drawString(message, (im.getWidth()/2) - (font.stringWidth(message)/2),
 0, Graphics.TOP | Graphics.LEFT);
  }
  catch (Exception e){
  System.err.println("Error during image creation");
  }  
  } 
  
  protected void paint(Graphics g){
  if (im != null)
  g.drawImage(im, getWidth()/2, getHeight()/2, Graphics.VCENTER | Graphics.HCENTER);
  }

  public void commandAction(Command c, Displayable d){
  if (c == cmExit)
  midlet.exitMIDlet();
  }
} 

Download Source Code

Advertisements

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: November 5, 2008

Ask Questions?    Discuss: User Define Alert Example  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
Nicolecai Kim DC Zamora
February 23, 2012
something's wrong

I couldn't make it work on my own. It can launch but it doesn't have the Alert effect. The image and the text is already there without even clicking the word Midlet. How can I make the alert work?
DMCA.com