Use canvas to create a welcome interface for the MIDlet

Source: Internet
Author: User

Usually, the application we release will display a welcome page when the program starts, or some instructions about the software, this article describes how to create a welcome page.

I have used alert for environment interfaces before. You can use the display. setcurrent (alert, next) method. In this way, the next interface is displayed when the alert display time ends or when the user presses the key. This basically satisfies our needs, but this effect is not very satisfactory. I have not performed many tests on my mobile phone. Here I will introduce a method for creating a welcome interface using canvas, which involves some content about timer and timertask, you can refer to my related articles in j2se to see how to use these two simple and important classes.

Our goal is to display a welcome interface to the user. When the user presses any key or sets the display time, the next main interface is displayed. We create a welcomecanvas class to inherit the canvas class. In the painting (Graphics g), we provide a method to draw images on our welcome interface. For example:
Protected void paint (Graphics arg0)
{
Int width = This. getwidth ();
Int Height = This. getheight ();
Image displayimage = NULL;
Try
{
Displayimage = image. createimage ("/duke.png ");
} Catch (ioexception E)
{
E. printstacktrace ();
}
Arg0.drawimage (displayimage, width/2, height/2, graphics. hcenter
| Graphics. Bottom );

}
In welcomecanvas, we want to start timing when it is displayed. In this way, we can overwrite the shownotify () method, as shown below:
Protected void shownotify ()
{
Timer. Schedule (New timertask ()
{
Public void run ()
{
Dismiss ();
}
}, Displaytime );
}

In this way, when the canvas is displayed, the system has started timing, and the main interface is displayed after displaytime. The user may not be able to wait for such a long time, so the main interface should be displayed when he presses the button, so we overwrite the keypressed () and pointerpressed () methods as follows:
Protected void keypressed (INT keycode)
{
Dismiss ();
}

Protected void pointerpressed (INT y, int X)
{
Dismiss ();
}

Private void dismiss ()
{
Timer. Cancel ();
Display. setcurrent (nextui );
}
In this way, the welcomecanvas is basically constructed. Let's write a test MIDlet to see the effect. The code of the MIDlet and welcomecanvas is as follows:

Note: This program runs in the eclipseenvironment. You should copy the duke.png file to the res directory. Otherwise, an exception will be thrown. This figure is displayed in the wtk installation directory.

Import javax. microedition. lcdui. Command;
Import javax. microedition. lcdui. commandlistener;
Import javax. microedition. lcdui. display;
Import javax. microedition. lcdui. displayable;
Import javax. microedition. lcdui. form;
Import javax. microedition. MIDlet. MIDlet;
Import javax. microedition. MIDlet. midletstatechangeexception;
/*
* Created on 2004-7-28
*
* Todo to change the template for this generated file go
* Window-preferences-Java-code style-code templates
*/

/**
* @ Author e2412c
*
* Todo to change the template for this generated type comment go to window-
* Preferences-Java-code style-code templates
*/
Public class mymidlet extends MIDlet
{

Private display;
Private form mainform = new form ("main form ");

Protected void Startapp () throws midletstatechangeexception
{
Display = display. getdisplay (this );
Mainform. append ("this is the main form ");
Welcomecanvas welcome = new welcomecanvas (display, mainform );
Welcome. setdisplaytime (6000 );
Display. setcurrent (welcome );
}

Protected void pauseapp ()
{

}

Protected void destroyapp (Boolean arg0) throws midletstatechangeexception
{

}

}

Import java. Io. ioexception;
Import java. util. timer;
Import java. util. timertask;

Import javax. microedition. lcdui. Canvas;
Import javax. microedition. lcdui. display;
Import javax. microedition. lcdui. displayable;
Import javax. microedition. lcdui. graphics;
Import javax. microedition. lcdui. image;

/**
* @ Author e2412c
*
* Todo to change the template for this generated type comment go to window-
* Preferences-Java-code style-code templates
*/
Public class welcomecanvas extends canvas
{

Private display;
Private displayable nextui;
Private timer = new timer ();
Private long displaytime = 3000;

Public welcomecanvas (display DIS, displayable disp ){
This. Display = DIS;
This. nextui = disp;
}

Protected void paint (Graphics arg0)
{
Int width = This. getwidth ();
Int Height = This. getheight ();
Image displayimage = NULL;
Try
{
Displayimage = image. createimage ("/duke.png ");
} Catch (ioexception E)
{
E. printstacktrace ();
}
Arg0.drawimage (displayimage, width/2, height/2, graphics. hcenter
| Graphics. Bottom );

}

Public void setdisplaytime (long disptime)
{
This. displaytime = disptime;
}

Protected void keypressed (INT keycode)
{
Dismiss ();
}

Protected void pointerpressed (INT y, int X)
{
Dismiss ();
}

Private void dismiss ()
{
Timer. Cancel ();
Display. setcurrent (nextui );
}

Protected void shownotify ()
{
Timer. Schedule (New timertask ()
{
Public void run ()
{
Dismiss ();
}
}, Displaytime );
}

}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.