When we develop, we may need to make a text scroll. For example, a line of words scroll from the lower part of the screen to the middle of the screen, in J2ME we can use the Canvas class and the Timer class to complete similar results.
In the J2ME's advanced graphical user interface API does not provide a similar scroll, so we need to implement itself through the Canvas class, we should draw the text we want to scroll in the Paint () method of the Canvas class, the basic code is as follows Description: protected void Paint (graphics arg0) {Int OldColor = arg0.getColor (); arg0.setcolor (255, 255, 255); arg0.fillRect (0, 0, width, height); arg0.setColor (OldColor) Arg0.drawstring (Welcome, Left, I, Graphics.Left | graphics.top);
} When we initialize a Sub-class of a Canvas, the Paint () method will be called, we need to scroll back up, so we can use the Timer and Timertask classes, we will implement public void run in the Timertask class. Methods to reduce a certain value for the Y coordinate of the text. Stop the reduction and stop Timer when the text arrives at the screen. Here I have implemented an internal class, of course, you can also achieve an anonymous internal class. Private class scrolltask eXtends Timertask {public void run () {if (i> height / 2) {i = i - fontheight; repaint ();} else {going = false; timancel ();}
}} Our WelcomeCanvas class extends CANVAS and implements its Paint () method, in the WelcomeCanvas constructor we pass the target to him and the specific size of the specific size in Welcome and the next Displayable. public WelcomeCanvas (Display display, Displayable disp) {super (); Font font = Font.getDefaultFont (); left = (width - font.stringWidth (welcome)) / 2; fontHeight = font.getHeight (); this.display = Display; this.next = 4; scrolltask st = new scrolltask (); time.schedule (ST, 100, 100);} When the text scrolls to the middle, the user can continue to operate by pressing any key. So we need a keypressed () method. If you are a touch screen, you can use the PointerPressed () method. Public void keypressed (int keycode) {if (! Going) DISPLAY.SETCURRENT (NEXT);} The picture below is the result of the program run and gives the source code. In fact, many similar effects can be implemented in this way. Import javax.microedition.lcdui.display; import javax.microedition.lcdui.form; import javax.microedition.midlet.midlet; import javax.microedition.midlet.midletStateChangeException;
Public Class Guessnumber Extends Midlet {Private Display Display; Private Welcomecanvas WelcomeCanvas; Private form mainform;
Protected void Startapp () throws midletStateChangeException {initmidlet ();
} Private void initMIDlet () {display = Display.getDisplay (this); mainForm = new Form ( "Guess Number"); mainForm.append ( "this is a game"); welcomeCanvas = new WelcomeCanvas (display, mainForm); display .SETCURRENT (Welcomecanvas);
protected void pauseapp () {}
Protected Void DestroyApp (Boolean Arg0) throws MidletStateChangeException {}
} / * * Created on 2004-9-24 * * Todo to change the Template for this generated file go to * 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 WelcomeCanvas extends Canvas {public static final String welcome = "Press any key to start"; private Display display; private Displayable next ; private Timer timer = new Timer (); private int width = getWidth (); private int height = getHeight (); private int i = height; private int left; private int fontHeight; private boolean going = true;
public WelcomeCanvas (Display display, Displayable disp) {super (); Font font = Font.getDefaultFont (); left = (width - font.stringWidth (welcome)) / 2; fontHeight = font.getHeight (); this.display = Display; this.next = disp; scrolltask st = new scrolltask (); timer.schedule (ST, 100, 100);}
Protected Void Paint (Graphics Arg0) {Int OldColor = arg0.getColor (); arg0.setcolor (255, 255, 255); arg0.FillRect (0, 0, width, height); arg0.setcolor (OldColor); Arg0. DrawString (Welcome, Left, I, Graphics.Left | graphics.top);
} Public void keypressed (int keycode) {if (! Going) display.setcurrent (next);}
Private class scrolltask extends Timertask {public void () {if (i> height / 2) {i = i - fontheight; repaint ();} else {going = false; timer.cancel ();}}}}