Programming Java 2 Micro Edition for Symbian OS 2004 (779882), страница 15
Текст из файла (страница 15)
This image is full of the individualsprite images required to provide the animation for the application. Thisis where the Game API comes into its own. If you pass it an image of acertain height and tell it the height of the individual frames, the Spriteclass is clever enough to create the frames for you. This really saveson development time and allows the developer to concentrate on thegame logic of the application rather than getting bogged down in spritedefinition. Therefore, the other items in the sprite’s constructor are widthand height.The sprite in this case is left to determine its own frame order, basedupon the order in which the frames are laid out in the graphic imagepassed to it at initialization.
Essentially, the frames are sequenced in onedirection and then reversed to provide the opposite effect, forming the animation of the Symbian image rotating to become the Helloworld image.The beauty of this self-determination approach is that we now havea reusable object, as long as the functionality of the object is to remainthe same. If we want to change the appearance of the animation, all wehave to do is create a new set of frames and the sprite will do the rest. Itis, of course, important to remember that we have to maintain the sameinterface with the game canvas which makes the calls to the class to setthe next frame.
It is, however, fair to say that this represents portable codethat can be reused to create a different animation without any change tothe game canvas.import javax.microedition.lcdui.Image;import javax.microedition.lcdui.game.Sprite;public class MySprite extends Sprite {protected final int SLEEP=1000;protected static final int RAW_FRAMES=13;protected static final int HELLOWORLD_FRAME=12;protected static final int SYMBIAN_FRAME=0;protected boolean direction=false;public MySprite(Image image, int width, int height){super (image,width,height);}public void tick(){this.getDirection();if(direction){HELLOWORLD, TURBO EDITION53this.nextFrame();}else{this.prevFrame();}}private void getDirection(){if (this.getFrame() == SYMBIAN_FRAME |this.getFrame()== HELLOWORLD_FRAME){getDelay();if(direction){direction=false;}else{direction=true;}}}private void getDelay(){try{Thread.sleep(SLEEP);}catch(InterruptedException ie){System.out.println(ie.toString());}}}2.2.5Paused Message Class: MyPausedCanvas.javaThis is a very simple class, which requires little explanation.
It is merely aCanvas which is set as the current Displayable when the HelloworldMIDlet is paused (see Figure 2.5).Figure 2.5Helloworld paused state.54GETTING STARTEDimport javax.microedition.lcdui.*;public class MyPauseCanvas extends Canvas {private Font font = Font.getFont(Font.FACE_PROPORTIONAL,Font.STYLE_BOLD, Font.SIZE_LARGE);public void paint(Graphics g) {// show the user a screen with "PAUSED" in the centerg.setColor(0,0,0);g.fillRect(0,0,getWidth(),getHeight());g.setColor(255,255,255);g.setFont(font);g.drawString("PAUSED",getWidth()/2,getHeight()/2,Graphics.TOP|Graphics.HCENTER);}}2.3 Introduction to Tools for MIDP2.3.1 Toolkits2.3.1.1 J2ME Wireless Toolkit 2.1OverviewThe J2ME Wireless Toolkit 2.1 provides basic tools for developers tocreate MIDP 2.0 applications. The Wireless Toolkit (WTK) was createdby Sun to facilitate MIDP development.
At the time of writing, theproduction release can be obtained free of charge from Sun’s website(http://java.sun.com/products/j2mewtoolkit/download-2 1.html).Amongst more advanced features, it provides the developer with theability to compile, pre-verify and package MIDlet suites on the commandline, as well as providing a simple GUI to manage MIDP applicationcreation. The ”Build” button on the KToolbar combines all the commandline functionality described in Section 2.1.1.6, apart from packaging. Thepackaging command in the Projects menu provides this extra step.
Ahandy interface for creating the JAD file is also supplied and accessed viathe Settings button.The J2ME WTK does not, however, provide a text editor or sophisticated debugging facilities, so users may find development a slightlycumbersome process. It does, however, remain a useful tool as it provides developers with device emulators and a development infrastructureand it captures system output and other debug information such as errormessages (see Figure 2.6).Versions of this tool are available for Windows and Unix-based systems.
At the time of writing, the J2ME Wireless Toolkit 2.1 is available inproduction releases for the following operating systems:• Microsoft Windows XP or Microsoft Windows 2000• Microsoft Windows 98/NT (unsupported)INTRODUCTION TO TOOLS FOR MIDP55Figure 2.6 J2ME Wireless Toolkit.• Solaris 8• Red Hat Linux kernel 2.4.2-2. glibc version 2.2.2 (unsupported).Also required for development is Java 2 SDK Standard Edition (J2SESDK) of at least version 1.4 (this is available at the following location:http://java.sun.com/j2se/downloads.html).Although support is only given for the more recent Windows platforms,this still represents a good opportunity for Java developers to test wirelessapplications on their favored development platforms.The toolkit offers the developer support in the following development areas:• OTA emulationThe toolkit takes the developer through the steps a user experienceswhen discovering and downloading an application to the device.
Theemulator displays JAD file information, which allows the end-userto decide whether to install locally or not. The application is thendownloaded and verified by the emulator device and installed. Theapplication is then run ”locally”.• MIDlet signingMIDlet signing is new to the MIDP environment. The toolkit allows adeveloper to browse for a J2SE keystore file (an SKS file) and use it tosign a MIDlet suite• WMA emulation for SMS (and CBS broadcasts)• new skins for QWERTY and media devices56GETTING STARTED• certificate management – an interface for the developer to managesecurity certificate files (CER files) and view the contents of J2MEkeystore files (KS files)• Push Registry emulation – this emulates a MIDlet’s reaction to aninbound network connection or timer-based alarm; the registry is setup using the Push Registry tab within the Project > Settings dialog• access to J2ME Web Services – the user can generate a stub connectorto access J2ME Web Services from the toolbar; the user provides aWeb Service Descriptor Language file (a WDSL file)• monitoring for all protocols – HTTPS, socket, datagram, COMM,SSL, SMS/CBS• compile-time and runtime selection of API extensions (WMA, MMAPI)• switching between MMAPI and MIDP 2.0 Media API – this allows thedeveloper to set the abilities of the underlying API implementationon the emulator device (some devices ship with the full MMAPI,so developers may wish to configure the emulator to reflect morepowerful devices)• new demos – demos such as the Mobile Media MIDlet (mmademo)and SMSDemo MIDlet allow the developer to gain an idea of how thetoolkit handles SMS messaging between emulators within the sameinstance of the toolkit• support for the ProGuard obfuscatorObfuscation provides a level of protection against reverse engineering.It also reduces the final file size of MIDlet suites.
This is very usefulwhen most end-users will be downloading their MIDlets remotely overthe air. Smaller files mean more efficient execution and less spacetaken up in the device. ProGuard provides software that performsthe obfuscation. It is available, under General Public License, athttp://proguard.sourceforge.net.• method profiling (from v1.0)• memory and network monitoring (from v1.0) – this includes messagefiltering, sorting messages and viewing network traffic• device speed emulation (from v1.0).InstallationTo enable installation of the J2ME Wireless Toolkit 2.1, the host PC willneed to have the Java 2 SDK 1.4.1 installed. Installation on the PC canbe carried out as follows.INTRODUCTION TO TOOLS FOR MIDP571.
Execute the file j2me_wireless_toolkit-2_1-windows.exewhich is available from the download areas on the Java Sun website(see above).2. The user will be prompted to confirm the location of the Java RuntimeEnvironment (Figure 2.7). Version 1.4.1 or higher is required. If thisis not present, it should be installed before continuing with theinstallation of the toolkit.3. The destination of the toolkit can then be chosen and confirmed(Figure 2.8). In this case we deviated from the default location.
Notethat, at least for the current Toolkit, this name cannot contain spaces.4. The installation program then prompts for the confirmation of aProgram Folder name (Figure 2.9). On Windows machines, this isthe name of the folder as it appears on the Start menu. It may bedesirable to enter a shorter name.5. A dialog reviewing the installation details is displayed.
Press Next tobegin installation.6. The installer will then display a dialog to tell the user that installationhas been completed (Figure 2.10).Figure 2.7 J2ME Wireless Toolkit setup.58GETTING STARTEDFigure 2.8Confirming location.Figure 2.9 Selecting a program folder.INTRODUCTION TO TOOLS FOR MIDP59Figure 2.10 Installation complete.2.3.1.2 Apache AntOverviewThe Apache Ant project is a part of Apache’s Jakarta project (http://jakarta.apache.org) and is a Java-based building tool distributed under theApache license. It has been developed by the Java community andis described as ”kind of like Make, but without the wrinkles”.
It canbe obtained from the Apache Ant website (http://ant.apache.org/index.html). We are going to use it in conjunction with another tool, Antenna,which provides specific Ant tasks for building MIDP 1.0 and 2.0 applications. This is distributed under the GNU Lesser GPL and can be obtainedfrom the SourceForge website (http://antenna.sourceforge.net).It is a reasonably easy tool to use and, in a similar way to the WirelessToolkit, it automates the compiling, pre-verifying and packaging of aMIDlet suite.
The execution of an emulator can also be added on to theend of the XML configuration file for extra convenience. Whereas theKToolbar provides the developer with adequate tools for MIDlet creation,Ant gives the developer fine-grained control over how the MIDlet suiteshould be put together. The really great thing about Ant is that it comesfully integrated with Borland’s JBuilder, Sun’s ONE Studio and can alsobe integrated with other IDEs such as JCreator (www.jcreator.com), JEdit(www.jedit.org), and Eclipse (www.eclipse.org). However, this sectionwill look at Ant as a standalone tool.60GETTING STARTEDAnt reads an XML configuration file and uses this information to carryout whatever commands are inside it. In this case, it uses the Antenna’sbuilt-in tasks, such as compile, pre-verify and package.