Welcome interface for making MIDlet using Canvas

zhaozj2021-02-16  80

Typically, we released the application when the program starts, it will show a welcome interface, or some information about the software, this article will tell how you use this welcome interface.

I used ALERT to use Alert, I can use Display.SetCurrent (Alert, next) method. This shows the next interface when the display time of the Alert is over or the user button. This can basically meet our needs, but this effect is not ideal, I don't have a lot of testing on my mobile phone. Here I introduce a way to use the Canvas to make a welcome interface, which will involve some contents about Timer and Timertask, you can refer to how to use these two simple and important classes in the related articles in J2SE.

Our goal is to display a welcome interface to the user, display the next main interface when the user presses any key or setting time. We created a WelcomeCanvas class to inherit the Canvas class provides a way in Paint (Graphics G) to draw pictures of 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. This way we can override ShownNotify (), as follows: protected void shownotify () {time.schedule (new timertask () {dismiss ();}}, displaytime);}

When Canvas is displayed, the system has begun, and the main interface will appear after DisplayTime. Perhaps the user can't wait for so long, then when he presses the key, we should also display the main interface, so we can override the method Keypressed () and PointerPressed () as follows: protected void keypressed (int keycode) {dismiss ();

Protected void Pointerpressed (int y, int x) {dismiss ();

Private void dismiss (); display.cancel (); display.setcurrent (Nextu); this is basically constructed, we write a test MIDlet to see the effect, MIDlet and WelcomeCanvas code content is as follows:

Remarks: This program is running in the Eclipse environment, so you should copy the duke.png file into the RES directory, otherwise you will throw an exception. This image I am looking for 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 to * Window - Preferences - Java - Code Style - Code Templates * /

/ ** * @Author E2412C * * Todo to change Type Comment GO to WINDOW - * Preferences - Java - Code Style - Code Templates * / Public Class Mymidlet Extends MIDlet {

Private Display 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 display; private Displayable nextUI; private Timer Timer = New Timer (); private long displaytime = 3000;

Public Welcomecanvas (Display Disn, Displayable DISP) {this.display = disgund; 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 dispatch) {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 () {dismiss ();}}, displaytime);}, DISPLAYTIME

}

转载请注明原文地址:https://www.9cbs.com/read-12553.html

New Post(0)