May 15, 2011 ... Development of Mobile Phone Game Based on Java ME. Abstract. Recently,
mobile phones have become more and more widespread in more ...
Yang Liu
Development of Mobile Phone Game Based on Java ME
Bachelor’s Thesis Information Technology
May 2011
DESCRIPTION Date of the bachelor's thesis
15th May, 2011
Author(s)
Degree programme and option
Yang Liu
Information Technology
Name of the bachelor's thesis
Development of Mobile Phone Game Based on Java ME Abstract
Recently, mobile phones have become more and more widespread in more than one aspect. Meanwhile, a large number of advanced features have also been applied into mobile devices. As we know, mobile phone game is one of them. In this final thesis, I develop a Chinese Chess game. Chinese Chess, also called Xiang Qi, is one of the most popular and oldest board games worldwide, which is more or less similar to Western Chess related to the appearance and regulations. In order to spread China culture and make individuals realize how fun and easy this game is, I introduce this Chinese Chess game as the topic in terms of my final thesis. In this final thesis, I use API (JSR 118) to build a user interface so as to set the board and pieces in the first place. Thereafter, some relevant basic rules are drawn up through logical control. This project is designated to be run on Java ME platform and Java SDK simulation software.
Subject headings, (keywords)
Java ME, JDK, Java SDK, MIDP, CLDC, API, Chinese Chess Pages
Language
63 p.+app.28
English
URN
Remarks, notes on appendices
Tutor
Employer of the bachelor's thesis
Matti Koivisto
Mikkeli University of Applied Sciences
ACKNOWLEDGEMENT
In the first place, I would like to represent my greatest appreciation to my supervisor Mr. Matti Koivisto. He gives me plenty of sincere advice related to my final report and the huge project. Without his constant supports, I cannot complete them in such a short time successfully. Meanwhile, I also want to express my gratitude to Ms. Elina Kiviranta, who checks the grammar errors for me.
What is more, I would like to give my thanks to my partner Mr. Shuaiyi Lv in terms of my final thesis during this period. With his help, we can finish this joint project together successfully. More importantly, we always encourage ourselves whenever we feel frustrated and lonely.
In addition, I want to thank my teachers who have taught me in not only Mikkeli University of Applied Sciences but also Beijing University of Technology. I would like to express my appreciation to them to give me an opportunity to study in Finland and make more familiar with European study style and life style.
Last but not least, I would dedicate this final thesis to my dear parents who gave me everything. They are always like my real friends who can always stand by me, give me a listening ear, a warm shoulder, and a helping hand no matter what.
LIST OF ABBREVIATIONS
API
Application Programming Interface
BD-J
Blu-ray Disc Java
CDC
Connected Device Configuration
CLDC
Connected Limited Device Configuration
FP
Foundation Profile
GPS
Global Positioning Systems
IDE
Integrated Development Environment
IMP
Information Module Profile
JAVA EE
Java Enterprise Edition
JAVA ME
Java Micro Edition
JAVA SE
Java Standard Edition
JAR
Java Archive File
JSR
Java Specification Requests
JDK
Java Development Kit
JVM
Java Virtual Machine
MIDP
Mobile Information Device Profile
PBP
Personal Basis Profile
PP
Personal Profile
PDA
Personal Digital Assistant
RDA
Remote Device Access
SDK
Software Development Kit
SMS
Short Message Service
UI
User Interface
WAP
Wireless Application Protocol
LIST OF FIGURES
Figure 2.1: Bluetooth logo.............................................................................................. 4 Figure 2.2: Chinese Chess game .................................................................................... 5 Figure 2.3: Working process of WAP games .................................................................. 6 Figure 2.4: Classic game “Snake” and “Racing” ........................................................... 6 Figure 2.5: Performance of J2ME games ....................................................................... 7 Figure 3.1: Logo of Java................................................................................................. 8 Figure 3.2: Different Java platforms .............................................................................. 9 Figure 3.3: Structure of Connected Device Configuration ........................................... 12 Figure 3.4: Structure of Connected Limited Device Configuration ............................. 13 Figure 3.5: MIDlet‟s life-cycle ..................................................................................... 15 Figure 4.1: Eclipse development platform ................................................................... 17 Figure 4.2: NetBeans development platform ............................................................... 18 Figure 4.3: NetBeans IDE downloading package ........................................................ 19 Figure 4.4: Custom Installation Wizard ....................................................................... 20 Figure 4.5: Process of JDK installation ........................................................................ 20 Figure 4.6: JDK installation is completed .................................................................... 21 Figure 4.7: NetBeans IDE initialization ....................................................................... 22 Figure 4.8: Specifying their routes ............................................................................... 22 Figure 4.9: Process of NetBeans installation................................................................ 23 Figure 4.10: NetBeans installation is completed .......................................................... 24 Figure 5.1: Board of Chinese Chess ............................................................................. 26 Figure 5.2: Movements of Horse in different situations............................................... 27 Figure 5.3: Rules of Cannons movement ..................................................................... 28 Figure 5.4: Pieces of Chinese Chess in their starting positions.................................... 29 Figure 5.5: Flow chart of whole project ....................................................................... 30 Figure 7.1: Initialization of NetBeans IDE .................................................................. 42 Figure 7.2: Creating a new project ............................................................................... 42 Figure 7.3: Default configuration of the device ........................................................... 43 Figure 7.4: Performance on the emulator ..................................................................... 45 Figure 7.5: Start screen of the game ............................................................................. 47 Figure 7.6: Step one on Red-side ................................................................................. 48 Figure 7.7: Step one on White-side .............................................................................. 48 Figure 7.8: Step two on Red-side ................................................................................. 49
Figure 7.9: Step two on White-side .............................................................................. 49 Figure 7.10: Step three on Red-side ............................................................................. 50 Figure 7.11: Step three on White-side .......................................................................... 50 Figure 7.12: Step four on Red-side............................................................................... 51 Figure 7.13: Step four on White-side ........................................................................... 51 Figure 7.14: Step five on Red-side ............................................................................... 52 Figure 7.15: Step-five on White-side ........................................................................... 52 Figure 7.16: Step six on Red-side................................................................................. 53 Figure 7.17: Step six on White-side ............................................................................. 54 Figure 7.18: Step seven on Red-side ............................................................................ 54 Figure 7.19: Step seven on White-side ......................................................................... 55 Figure 7.20: Step eight on Red-side ............................................................................. 55 Figure 7.21: Step eight on White-side .......................................................................... 56
LIST OF CODE EXAMPLES
Code example 3.1 ......................................................................................................... 14 Code example 6.1 ......................................................................................................... 34 Code example 6.2 ......................................................................................................... 36 Code example 6.3 ......................................................................................................... 37 Code example 6.4 ......................................................................................................... 38 Code example 6.5 ......................................................................................................... 39 Code example 6.6 ......................................................................................................... 39
TABLE OF CONTENTS
1 INTRODUCTION ....................................................................................................... 1 2 TYPES OF MOBILE PHONE GAMES ..................................................................... 3 2.1 Overview .............................................................................................................. 3 2.2 Classification Based on Method of Usage ........................................................... 3 2.2.1 Multi-player Mobile Phone Games ............................................................. 3 2.2.2 Single-player Mobile Phone Games............................................................ 4 2.3 Classification Based on Form of Representation ................................................. 5 2.3.1 Text-mode Games ....................................................................................... 5 2.3.2 Graphic-mode Games.................................................................................. 6 3 MOBILE PROGRAMMING ENVIRONMENT ........................................................ 8 3.1 Java Programming Language ............................................................................... 8 3.1.1 Introduction ................................................................................................. 8 3.1.2 Characteristics ............................................................................................. 8 3.1.3 Classification ............................................................................................... 9 3.2 Java Micro Edition (Java ME) ........................................................................... 10 3.2.1 Introduction ............................................................................................... 10 3.2.2 Architecture ............................................................................................... 11 3.2.3 Configuration ............................................................................................ 11 3.2.4 Connected Device Configuration (CDC) .................................................. 11 3.2.5 Connected Limited Device Configuration (CLDC) .................................. 12 3.2.6 Profile ........................................................................................................ 13 3.2.7 Introduction to MIDP ................................................................................ 13 3.2.8 Specification to MIDP............................................................................... 14 3.2.9 MIDlet ....................................................................................................... 14 3.3.0 Optional Package ...................................................................................... 15 4 JAVA DEVELOPMENT TOOLS ............................................................................ 17 4.1 Overview ............................................................................................................ 17 4.2 Eclipse ................................................................................................................ 17 4.3 NetBeans ............................................................................................................ 17 4.4 Installation .......................................................................................................... 18 5 WORKING PRINCIPLE OF THE GAME ............................................................... 25 5.1 Description of Project......................................................................................... 25 5.1.1 Introduction to Board ................................................................................ 25
5.1.2 Introduction to Pieces ................................................................................ 26 5.1.3 Chariots ..................................................................................................... 26 5.1.4 Horses ........................................................................................................ 26 5.1.5 Elephants ................................................................................................... 27 5.1.6 Advisors .................................................................................................... 27 5.1.7 Generals..................................................................................................... 27 5.1.8 Cannons ..................................................................................................... 28 5.1.9 Soldiers...................................................................................................... 28 5.2 Structure of Project ............................................................................................. 29 6 DIVISION OF THE PROJECT................................................................................. 31 6.1 Introduction ........................................................................................................ 31 6.2 Structure of UI Package ..................................................................................... 31 6.3 Interfaces and Classes of UI Package ................................................................ 31 6.3.1 Interface CommandListener ...................................................................... 32 6.3.2 Class GameCanvas .................................................................................... 32 6.3.3 Class Graphics ........................................................................................... 33 6.4 Description of User Interface API ...................................................................... 34 6.4.1 Game. java................................................................................................. 34 6.4.2 GameCanvas. java ..................................................................................... 35 7 IMPLEMENTING THE PROGRAMMING............................................................. 41 7.1 Building a Programming Environment .............................................................. 41 7.2 Creating a Project ............................................................................................... 41 7.3 Implementation of Project .................................................................................. 45 7.3.1 Start Module .............................................................................................. 46 7.3.2 Game Module ............................................................................................ 47 7.3.3 Result Module ........................................................................................... 56 8 CONCLUSION ......................................................................................................... 57 8.1 Purpose ............................................................................................................... 57 8.2 Challenges .......................................................................................................... 57 8.3 Future Development ........................................................................................... 58 BIBLIOGRAPHY ........................................................................................................ 59 APPENDICES .............................................................................................................. 64 Appendix 1 ............................................................................................................... 64 Appendix 2 ............................................................................................................... 66
1
1 INTRODUCTION
Nowadays, mobile phones have drawn not only adults‟ but also teenagers‟ attention in more than one aspect. They have also become more and more significant tools in our daily life not only for their essential functions related to transmitting and receiving phone calls but also for their great number of advanced functions. Individuals can use their mobile phones to surf the internet, play the phone games based on Java, take photos by using built-in camera, transmit information via Bluetooth, or even listen to the music and watch TV online. With an increasing number of features embedded into the mobile phones, their appearances have also changed from the brick-like box to the suitable device inside the pocket handily.
Recently, there are a multitude of new features provided to make people more convenient and familiar to use their mobile phones like GPS, built-in camera, Infrared, Bluetooth, and WLAN connections. As it is well-known, Java APIs are available to realize these features. In this research area, many different kinds of applications can be implemented as well.
At the same time, mobile phone games are becoming more and more popular among youngsters. They are widely used on different kinds of devices including mobile phones, PDAs, and personal computers. There are also numerous types of mobile games like adventure, roles play, puzzle, and so on, which can be played not only on the multi-player mode but also on the single-player mode.
The aim of my final thesis is to become more familiar with some applications of mobile phone games by using Java Micro Edition, which is one of the most popular platforms for mobile phone development. As we know, Chinese traditional culture is well-known and profound all over the world. So in my research area, I mainly emphasize a Chinese Chess game which I designed briefly to implement my project.
The structure of the thesis is as follows:
In Chapter 2, I introduce a comprehensive view of several types of games between different mobile phones, and analyze the similarities and differences among all the games. Lastly, the main idea of my project is displayed.
2
In Chapter 3, I demonstrate a general introduction of programming environment we usually use, briefly discuss the language in the first place. Thereafter, I illustrate the related issues regarding how to build the proper programming environment based on the Java Micro Edition, which is an edition I use to develop my application.
In Chapter 4, I explain some different kinds of development tools which are widely used among individuals, and compare their differences and relationships with each other. More specifically, NetBeans IDE 6.9.1 is the platform which I use to develop my study.
In Chapter 5, I present a Chinese Chess game which I came up with, and describe the main idea regarding to my project briefly. More specifically, some principles and regulations are demonstrated in order to make everybody understand clearly.
In Chapter 6, I explain the distribution with my partner, Mr. Shuaiyi Lv, on this joint programming project related to this thesis. I am responsible for the interaction for human-machine interface, and he is in charge of the logic control for response and process. As is known to all, which is a critical part of this project, and I emphasize in detail on this section.
In Chapter 7, I introduce the application concerning this project and build a proper programming environment step by step to illustrate the main idea, and then test my project in practice.
In Chapter 8, I draw a conclusion related to my final thesis. What is more, I also argue the challenges and difficulties I faced and the trouble-shooting with regard to my project. Last but not least, I discuss some prospects and further study which can be done in the future.
3
2 TYPES OF MOBILE PHONE GAMES
2.1 Overview Nowadays, mobile phone games are becoming more and more popular in individuals‟ life. We can witness portability and mobility anywhere and anytime among all the different types of mobile phone games. One good reason that contributes to the success of mobile games is because of theirs mobility, which means players can play games anytime and anywhere. A survey on user preferences among students of Institute of Higher Learning (IHL) has been done to gather useful information and relevant data to support my study. The survey showed that an overwhelming majority (60%) of the respondents prefer to play games on mobile phone. (Hashim & Perkins 2008, 1) For this reason, we can see that mobile phone games have a broad marketing field. As we know, the first game that was pre-installed into a mobile phone was „Snake‟ in selected Nokia models in 1997. (Forum Nokia [referred 13.03.2011]) Snake and its variants have since become the most popular game on the market with a great number of people having played. In this chapter, I specifically emphasize numerous kinds of mobile phone games and compare the similarities and differences between them briefly. Finally, I present my main idea of the study.
According to the method of usage, we can divide mobile phone games into two main types, one is called multi-player mobile phone games, and the other is called single-player mobile phone games. However, in terms of the form of representation, which can be classified into text-mode games and graphic-mode games as well. Thus, I explain these two main categories respectively as follows.
2.2 Classification Based on Method of Usage
2.2.1 Multi-player Mobile Phone Games
Recently, multi-player mobile games are always applied into the fields of PAN, LAN, MAN, or WAN by using several different wireless technologies. We can experience convenience in everything from cordless mice and keyboards, to satellite phones and global positioning systems (GPS). Generally speaking, some simple mobile phone games are usually used for WPAN, which is short for Wireless Personal Area Network. WPAN can be used for communication among the personal devices between a few metres. What is more, a WPAN
4
can also be made possible with wireless network technologies such as IrDA, Bluetooth, UWB, Z-Wave and ZigBee. (Personal area network [referred 14.03.2011])
In these several years, the Bluetooth wireless technology is spreading rapidly. The number of Bluetooth chipsets shipped per year has doubled from 2009 to a total of 69 million chipsets in 2010, and manufactured Bluetooth-enabled equipment will still be led by mobile phones currently. (Pilato [referred 14.03.2011]) The following Figure 2.1 exhibits the logo of Bluetooth.
Figure 2.1: Bluetooth logo (Bluetooth.svg [referred 14.03.2011])
2.2.2 Single-player Mobile Phone Games
When it comes into single-player mobile phone games, also called Console games, I think which are more significant in individuals‟ life although multi-player mobile phone games are getting more and more popular in more than one aspect. Players can play the games flexibly without internet access, unlike a game with multiple players competing with or against each other to reach the game's goal, a one-player game is a battle solely against an element of the environment (an artificial opponent), against one's own skills, against time, or against chance. And many games described as "single-player" may be termed actually puzzles or recreations. (Single-player games [referred 14.03.2011]) Figure 2.2 below represents an example of single-mode mobile phone games.
5
Figure 2.2: Chinese Chess game (Baidu pictures [referred 14.03.2011])
2.3 Classification Based on Form of Representation
2.3.1 Text-mode Games
Through the appearance of mobile phone interface, we can classify two main items, one is called text-mode games, and the other is called graphic-mode games.
Speaking of text-mode games, which is a form of games through swapping characteristics. We can play the games by replying the information according to the tips from mobile games. Furthermore, there are two basic approaches to achieve text games, SMS games and WAP games.
As we know, SMS (Short Message Service) games mean that we can play the games by transmitting and receiving the short messages between web servers and players. However, WAP (Wireless Application Protocol) games are similar to SMS games more or less. The games can be running based on the information shown on the WAP browers. Figure 2.3 below depicts the working process regarding to WAP games.
6
Figure 2.3: Working process of WAP games (Mobile games [referred 19.03.2011])
2.3.2 Graphic-mode Games
As far as graphic-mode games, which are the electronic games that involve interaction with a user interface to generate graphic feedback on the mobile phones. They are more like animations. More specifically, they can be divided into several areas like embedded games, J2ME games and some others.
Generally speaking, the games inherent existing in the mobile phones are usually called embedded games because they cannot be modified or deleted manually. Figure 2.4 below describes an example related to embedded games.
Figure 2.4: Classic game “Snake” and “Racing” (Mobile games [referred 19.03.2011])
7
In terms of J2ME games, they are based on Java Micro Edition platform. We can add, delete or modify some information to optimize the performance through Java language programming. Figure 2.5 below illustrates the screenshot of a J2ME game, we can witness clearly that the effect and performance may be better than in embedded games.
Figure 2.5: Performance of J2ME games (Mobile games [referred 19.03.2011])
In my study, I mainly focus on the project regarding to a Chinese Chess game. This project is actually a single-player mobile phone game based on Java Micro Edition platform.
8
3 MOBILE PROGRAMMING ENVIRONMENT
3.1 Java Programming Language
3.1.1 Introduction
With the rapid development of society, Java technology has become one of the most significant parts in our daily lives. From mobile phones to laptops, navigation systems to games, we can use Java technology to make them more functional, entertaining and convenient. The number of Java enabled mobile phones worldwide is over 250 million according to a press release from Sun titled: “Java technology is everywhere, surpasses 1.5 billion devices worldwide”, (Java technology [referred 24.03.2011]) and the number will continue to increase considerably.
In the early 1990s, Java was developed by a team led by James Gosling at Sun Microsystems, which was original called Oak and designed in 1991 for use in embedded chips in consumer electronic applications. But in 1995, it was renamed Java and redesigned for developing internet applications. (Mynttinen, 2011) Figure 3.1 below depicts the logo of Java.
Figure 3.1: Logo of Java (Java.svg [referred 24.03.2011])
3.1.2 Characteristics
Java is an object-oriented, simple, distributed, secure, dynamic and high performance programming language developed by Sun Microsystems, a company best known for its
9
high-end Unix workstations. But now, it is a part of Oracle Corporation. Java language is designed to be small, simple, and portable across platforms and operating systems which derives much of its syntax from C and C++ but has a simpler object model and fewer low-level facilities. (Lemay 1996, 4)
Generally speaking, Java standard is defined by Java language specification and Java API. The Java language specification is a technical definition of the language that includes the syntax and semantics of the Java programming language. The application programming interface (API) contains many predefined classes and interfaces for developing Java programs. As we know, the Java language specification is stable, but the API is still expanding. (Mynttinen, 2011)
3.1.3 Classification
Recently, Java technology has become more and more popular due to its high qualities, and owing to its extendibility feature, we can applied it into various kinds of fields. There are several Java platforms in correspondence with different goals of development for programmers, offering them numerous proper resolutions they require. Figure 3.2 below demonstrates an overview of the components of different Java platforms and the areas of various applications.
Figure 3.2: Different Java platforms (Java platforms [referred 24.03.2011])
10
As can be seen from the graph, Java can be divided into three types of edition: Java Standard Edition (Java SE), Java Micro Edition (Java ME), and Java Enterprise Edition (Java EE).
Java SE can be always used to develop client-side standalone applications or applets, which is mainly designed for personal computing platforms like Sun Solaris, Linux and Microsoft Windows. It can help us to develop and deploy Java applications on desktops and servers, as well as today's demanding Embedded and Real-Time environments. (Java SE [referred 24.03.2011])
Java ME can be usually used to develop applications or MIDlets across many mobile devices, such as mobile phones, PDAs, PSPs, and some other wireless devices. This is a version of Java platforms targeted at devices which have limited processing power and storage capabilities and intermittent or fairly low-bandwidth network connections. (J2ME Tutorial [referred 24.03.2011])
Java EE is always applied into develop server-side applications, which is quite necessary for companies with great deployment needs and is the industry standard for enterprise Java computing. Developers will benefit from productivity improvements with more annotations, more POJOs, simplified packaging, and less XML configuration. (Java EE [referred 24.03.2011])
This final thesis will mainly deal with Java ME, so I will take more details in the next section about Java Micro Edition.
3.2 Java Micro Edition (Java ME)
3.2.1 Introduction
As we know, mobile phones are mobile devices with restricted hardware resources available, which means they cannot run complex applications as computers can because of the limited power capability and the small size of memory. In order to overcome those drawbacks, Java ME is carried out.
Java ME provides a robust, flexible environment for applications running on mobile and other embedded devices like mobile phones, personal digital assistants (PDAs), TV set-top boxes,
11
and printers. Java ME includes flexible user interfaces, steady security, built-in network protocols, and support for networked and offline applications that can be downloaded dynamically. (Java ME [referred 26.03.2011])
3.2.2 Architecture
Generally speaking, there are three key elements in terms of Java ME technology: configuration, profile and optional packages. Thereafter, I will emphasize these several items in detail.
3.2.3 Configuration
Java ME can be divided into two types of configurations based on storage and process capacity which are CDC and CLDC respectively. CDC means Connected Device Configuration while CLDC is short for Connected Device Limited Device Configuration. Apparently, CDC is usually used for unrestricted mobile devices like set-top boxes while CLDC is always implemented into limited embedded devices like PDAs.
After that, I will demonstrate these two kinds of configurations and compare the similarities and differences with each other.
3.2.4 Connected Device Configuration (CDC)
As we know, this configuration is almost applied into some larger devices with more capacity and with a network-connection, like high-end personal digital assistants, and set-top boxes. The aim of the CDC configuration is to support the functions of a broad range of connected devices while fitting within their resource constraints. (Connected Device Configuration [referred 26.03.2011]) Figure 3.3 below depicts the structure of the CDC platform.
12
Figure 3.3: Structure of Connected Device Configuration (Digital Media Platform [referred 26.03.2011])
3.2.5 Connected Limited Device Configuration (CLDC)
CLDC is short for Connected Limited Device Configuration, which is designed to meet the requirements for a Java ME to run on devices with limited memory, processing and computing capabilities. What is more, Java ME defines a number of profiles related to CLDC in order to build an appropriate application. A widely adopted example is to combine the CLDC with the Mobile Information Device Profile (MIDP) to provide a complete Java application environment for mobile phones and other devices with similar capabilities. (Connected Limited Device Configuration [referred 26.03.2011]) As can be seen from Figure 3.4, which is the structure of the CLDC platform.
13
Figure 3.4: Structure of Connected Limited Device Configuration (CLDC Wireless Platform [referred 26.04.2011])
3.2.6 Profile
Profile forms a complete programming environment with Configuration I mentioned above, which consists of several Application Programming Interfaces (APIs) such as Foundation Profile (FP), Personal Basis Profile (PBP) and Personal Profile (PP) related to CDC, and Mobile Information Device Profile (MIDP) and Information Module Profile (IMP) related to CLDC.
In fact, MIDP is a type of profile I used in my study to build a complete Java programming environment for wireless devices based on CLDC.
3.2.7 Introduction to MIDP
As we know, MIDP is short for Mobile Information Device Profile, which is a specification published for the use of Java on embedded devices. MIDP is a key element of the Java ME framework and sits on top of Connected Limited Device Configuration. (Mobile Information Device Profile [referred 26.03.2011]) We can develop and deploy the applications designed by MIDP. Thus, CLDC and MIDP provide the core application functionality required by mobile applications, in the form of a standardized Java runtime environment and a rich set of Java APIs. Here are numerous advantages with regard to it. (MIDP Overview [referred 26.03.2011])
14
Rich User Interface Capabilities Extensive Connectivity Multimedia and Game Functionality Over-the-Air-Provisioning End-to-End Scurity
3.2.8 Specification to MIDP
There are two specifications of MIDP, one is called MIDP 1.0 (JSR 37) and the other is called MIDP 2.0 (JSR 118). When it comes to MIDP 1.0, this is the initial version and may achieve some essential functions like user interface and Java specification requirement. In terms of MIDP 2.0, it is a modified version based on MIDP 1.0, which may provide more advanced features related to Java such as enhanced user interface and TCP/IP support.
3.2.9 MIDlet
MIDlet is short for Mobile Information Devices applet, which is actually a Java application embedded into the mobile devices, and only utilizes various APIs which are defined by MIDP and CLDC. The application I designed is practically a MIDlet.
MIDlets are all derived from the abstract base class javax.microedition.midlet.MIDlet, which contains methods that the MIDP platform calls to control the MIDlet‟s life-cycle, as well as methods that the MIDlet itself can use to request a change in its state. A MIDlet must have a public default constructor which can be supplied by the programmer when initialization or there are no explicit constructors. (Koivisto, 2011) The empty default constructor can be demonstrated in Code example 3.1 as follow.
Code example 3.1: public class MyMIDlet extends MIDlet {
/* Optional constructor */ MyMIDlet( ) { }
15
protected void startApp( ) throws MIDletStateChangeException { }
protected void pauseApp( ) { }
protected void destroyApp(Boolean unconditional) throws MIDletStateChangedException { } }
As can be seen from the code above, there are three possible states in a MIDlet‟s life-cycle. Figure 3.5 demonstrates the MIDlet‟s life-cycle. Paused – The MIDlet instance has been constructed and is inactive. Active – The MIDlet is active. Destroyed – TheMIDlet has been terminated and is ready for reclamation by the garbage collector. (MIDlet [referred 26.03.2011])
Figure 3.5: MIDlet‟s life-cycle (Life-cycle [referred 26.03.2011])
3.3.0 Optional Package
Generally speaking, optional packages provide some additional functions without modifying Profile, which can be regarded as the extension of Profile. In other words, when we need add
16
a new API for Profile, we should firstly define it as an optional package. With time runs, if this package is widely used in many areas, it may be a specific part of Profile.
17
4 JAVA DEVELOPMENT TOOLS
4.1 Overview
Java ME can be implemented on a variety of platforms, but there are two main development tools that a number of people would like to use, one is called Eclipse and the other is called NetBeans. I illustrate these two development environments briefly and manly focus on NetBeans which I use to implement my study I designed.
4.2 Eclipse
Eclipse is a multi-language software development environment comprising an integrated development environment (IDE) and an extensible system. (Eclipse (software) [referred 02.04.2011]) Apart from Java, some other programming languages like C, C++, and PHP can also be compiled by Eclipse. Figure 4.1 demonstrates the development platform of Eclipse.
Figure 4.1: Eclipse development platform (Eclipse IDE [referred 02.04.2011])
4.3 NetBeans
18
NetBeans is a platform for Java desktop applications and also an integrated development environment (IDE) for developing with not only Java but also many other languages. The NetBeans IDE is written in Java and can run anywhere a Java Virtual Machine (JVM) is installed, including Windows, Mac OS, Linux, and Solaris. A Java Development Kit (JDK) is also required for Java development functionality. (NetBeans [referred 05.04.2011]) Figure 4.2 illustrates the development platform of NetBeans.
Figure 4.2: NetBeans development platform (NetBeans IDE [referred 05.04.2011])
In terms of my study, I use NetBeans IDE 6.9.1 as my development tool, which is the newest released version and very suitable for beginners to study gradually.
4.4 Installation
Generally speaking, we can download NetBeans IDE 6.9.1 for free from the internet, which is the
newest
version
and
it
is
available
from
the
website
http://netbeans.org/downloads/index.html. Then we may install this software step by step. Figure 4.3 below depicts the downloading package of NetBeans IDE 6.9.1.
19
Figure 4.3: NetBeans IDE downloading package (NetBeans Downloads [referred 05.04.2011])
Before we install NetBeans IDE, Java Development Kit (JDK) is required for Java ME platform. It can help us to create the Java Archive Files (jar.exe) automatically in terms of the process of Java compiler and applet. We may witness the installation process of JDK 6u24 as follows:
First of all, we must access into Custom Installation Wizard without any altering, and then choose Next Step button. This step is illustrated in Figure 4.4.
20
Figure 4.4: Custom Installation Wizard
Next, we should select an approximate route for installation. Thereafter, the package will be compressed and released. Figure 4.5 demonstrates the process of installing JDK 6u24.
Figure 4.5: Process of JDK installation
21
Finally, JDK 6u24 Installation has been completed successfully as we can see from Figure 4.6, so we can just click the Finish button in order to install NetBeans IDE 6.9.1 for the next sectiom.
Figure 4.6: JDK installation is completed
Once the installation of JDK 6u24 is completed, NetBeans IDE 6.9.1 can be installed step by step so as to develop Java applications and applets. So we can witness the installation process of NetBeans IDE as follows:
In the first step, NetaBeans IDE 6.9.1 initialization must be configured at start. Figure 4.7 below shows the configuration of NetBeans initialization.
22
Figure 4.7: NetBeans IDE initialization
Secondly, we must select some proper routes for NetBeans IDE 6.9.1 and JDK 6u24, which is described in Figure 4.8. Then, choose Next Step button to install the programming environment.
Figure 4.8: Specifying their routes
23
In this step, we can experience the process of NetBeans installation as Figure 4.9 shown. It requires us to wait this process running for a little bit longer time.
Figure 4.9: Process of NetBeans installation
At last, NetBeans IDE 6.9.1 installation has been completed successfully as can be seen from Figure 4.10. Thereafter, we may just select the Finish button so as to run NetBeans IDE 6.9.1 automatically.
24
Figure 4.10: NetBeans installation is completed
25
5 WORKING PRINCIPLE OF THE GAME
5.1 Description of Project
In this chapter, I mainly introduce the project I designed, and some regulations and principles are also explained so as to make individuals understand clearly.
Chinese Chess game, also called Xiang Qi, is an extremely popular game in Asia with a long history, especially in China. Actually, Chinese Chess is quite similar to Western Chess, the general idea is identical. Each player controls an army of pieces, moves one piece at a time, and tries to get the opponent‟s “general” piece. It differs from Western Chess mainly in the board and the pieces. (Xiangqi [referred 10.04.2011])
5.1.1 Introduction to Board
Traditional Chinese Chess is played on a board that is a grid of nine lines wide and ten lines long. In a manner similar to the game Go, the pieces are played on the intersections, and the vertical lines are interrupted by the River between the fifth and sixth horizontal lines. Although the River provides a visual division between the two sides, only a few pieces are affected by its presence: “soldier” pieces have an enhanced move after crossing the river, while “elephant” pieces cannot cross. (Xiangqi_board [referred 10.04.2011]) There are also two Palaces at each side which is located by an X-shaped cross connecting its four corner intersections, which means the “general” and “advisors” should be stagnated in this area. Figure 5.1 below illustrates the board of Chinese Chess.
26
Figure 5.1: Board of Chinese Chess (Learning how to Play Chineses Chess [referred 10.04.2011])
5.1.2 Introduction to Pieces
Chinese Chess involves seven kinds of pieces, each side has one General, two Advisors, two Elephants, two Horses, two Chariots, two Cannons, and five Soldiers respectively. (Yen & Chen & Yang & Hsu [referred 15.04.2011] Next, I explain their location and principle in detail.
5.1.3 Chariots
The chariots are represented “車” for black and “俥” for red, which can move vertically and horizontally anywhere, but cannot jump over any pieces. The chariots are located at the four corners of the board initially.
5.1.4 Horses
The horses are stand for “馬” for black and “傌” for red, which are situated next to the chariots at the start. They can move vertically or horizontally one point and then diagonally away from its previous position. Also, the horses cannot jump over any pieces. We should be
27
reminded that if there is a piece located one point away from the horse, the horse cannot move directly because the path is blocked. Figure 5.2 demonstrates the various situations on the horse‟s movements.
Figure 5.2: Movements of Horse in different situations (Horse movement [referred 15.04.2011])
5.1.5 Elephants The elephants are labeled “象” for black and “相” for red, which are located next to the horses firstly. They can move two points diagonally like the format of 田. In addition, the elephants cannot jump over any pieces, and more critically, they must not cross over the river.
5.1.6 Advisors The advisors are known as “士” for black and “仕” for red, which are mainly used for protecting the general from getting any threats. The advisors begin at the both sides of the general, they can only move diagonally within the palaces on both sides.
5.1.7 Generals As we know, the generals are the royal pieces on the board. They are on behalf of “將” for black and “帥” for red, which are located in the middle of the board bottom at the beginning, each player has only one general. They can only move vertically or horizontally within the palaces on both sides. They are the most significant pieces on the board.
28
5.1.8 Cannons The cannons stand for “炮” for black and “砲” for red, which are initially situated on the row behind the solders while in front of the horses, each side has only two the cannons. They can move vertically and horizontally anywhere like the chariots, while the only difference is that they must move by jumping over one piece anyway. Figure 5.3 shows the regulation of the cannons movement.
Figure 5.3: Rules of Cannons movement (Cannon movement [referred 15.04.2011])
5.1.9 Soldiers The soldiers are usually called “卒” for black and “兵” for red, there are five pieces at each side located in one row behind the river. They can only move forward at own side. However, when they cross over the river, they are more functional, which can move virtually and horizontally without any restrictions.
Figure 5.4 below illustrates the pieces‟ location on a board at the beginning.
29
Figure 5.4: Pieces of Chinese Chess in their starting positions [38]
5.2 Structure of Project
This is a Chinese Chess game. When the game starts, the chess board may be appeared on the touch screen, as well as the chess pieces, which can be moved anywhere we want based on the principle. As the flow chart (Figure 5.5) shows, the structure of the project can be divided into three modules – Main module, Game module, and Result module.
30
Figure 5.5: Flow chart of whole project
The main idea of my project has been illustrated as this flow chart described, like any other Java mobile phone games, this Chinese Chess game may be also separated into two main partitions: user interface and flow control. Basing on this principle, we focus on our own part in this joint project respectively. In the next chapter, I mainly emphasize our allocation and my key research field.
31
6 DIVISION OF THE PROJECT
First of all, I briefly point out the logical control for response and process in the project. Because Chinese Chess game has many regulations and principles. Developers should have a strong logical thinking ability. For instance, the rules of all the pieces which I have mentioned earlier, and how to choose the piece step by step, have become big challenges for us.
Some specific programming description is illustrated by my partner in his thesis, mainly because his strong logical capability. In the next section, I just introduce my key study field. The user interface components in this Chinese Chess game.
6.1 Introduction
In this section, I briefly emphasize the interaction for human-machine interface, which contains some user interface components. Names of these components look very familiar to AWT (Abstract Window Toolkit) programmer. There are also various APIs defined by MIDP 2.0 and CLDC 1.1. Next, I mainly explain the structure and description of Java APIs for User Interface (JSR118).
6.2 Structure of UI Package
In terms of the UI API, it can provide an approach to display the pictures or characteristics on the mobile phone screens, which can be logically composed of two kinds of APIs: high-level user interface and low-level user interface. As we know, the UI API affords a series of functions
for
implementation
of
user
interfaces
for
the
MIDlets,
and
the
javax.microedition.lcdui packet consists of several public interfaces and a number of classes as well. In the next section, I mainly demonstrate some important interfaces and classes. Furthermore, I will illustrate the principles and regulations of these significant components in detail.
6.3 Interfaces and Classes of UI Package
The UI package is javax.microedition.lcdui, and there are four public interfaces related to this packet: Choice, CommandListener, ItemCommandListener, and ItemStateListener. (User
32
Interface Package [referred 20.04.2011]) In terms of this project, I use CommandListener as the interface to implement my MIDlet.
Furthermore, the UI package can also be divided into several public classes such as Canvas, Command, Display, Displayable, Form, Graphics, Screen, List, etc. (User Interface Package [referred 20.04.2011]) In terms of low-level UI APIs, the classes can be separated into Canvas and Graphics. However, when it comes to this project, I use GameCanvas and Graphics as the main classes to implement my MIDlet, and GameCanvas is inherited from Canvas which is defined by MIDP 1.0.
6.3.1 Interface CommandListener
In my partition, I use CommandListener to implement the MIDlet. This public interface is used by applications which need to receive high-level events from the implementation. (Interface CommandListener [referred 20.04.2011])
It
contains
a method
called
commandAction(Command command, Displayable displayable), which means when we press the key on Displayable d, this command will be invoked.
6.3.2 Class GameCanvas
First of all, we should know that the GameCanvas class is a subclass of lcdui‟s Canvas, and provides the basic „screen‟ functionality for the user interface. (Game API Package [referred 20.04.2011]) It belongs to MIDP 2.0, yet Canvas class belongs to MIDP 1.0. The functions of them are all displaying pictures or characteristics on the mobile phone screens. In addition to the features inherited from Canvas, GameCanvas can also provide game-specific capabilities such as an off-screen graphics buffer and the ability to query key status. (Class GameCanvas [referred 20.04.2011]) After that, I could illustrate several critical components with regard to it.
GameCanvas can be divided into a range of fields, like DOWN PRESSED, GAME A PRESSED, LEFT PRESSED, and so on. They all stand for some specified keys on the mobile phones.
The constructor of GameCanvas class is GameCanvas(boolean suppressKeyEvents). It represents a framework which can help us to create a new example of a GameCanvas.
33
There are also various methods related to GameCanvas, such as paint(Graphics g), addCommand(Command command), keyPressed(int keyCode), pointerDragged(int x, int y), etc. In the next phase, I briefly describe some of them to make individuals understand with reference to my project.
public void setCommandListener(CommandListener 1): This method sets a listener for Commands to the Displayable, replacing any previous CommandLiatener.
public void addCommand(Command command): This method may let us add a command to the Displayable.
public void Paint (Graphics g): This method can help us to paint the GameCanvas by default.
protected void keyPressed(int keyCode): This method might be called when a key is pressed.
protected void pointerDragged(int x, int y): This method would be called when the pointer is dragged.
6.3.3 Class Graphics
Generally speaking, public class Graphics affords a simple two dimensional geometric rendering capability, which can also be applied into a variety of fields, like BASELINE, BOTTOM, LEFT, VCENTER, etc. What is more, Graphics class contains numerous kinds of methods as well, such as drawRect(int x, int y, int width, int height), fillRect(int x, int y, int width, int height), setColor(int RGB), drawString(String string, int x, int y, int anchor), and so on. (Class Graphics [referred 20.04.2011]) Next, I generally demonstrate some of these methods to make individuals understand with regard to my project.
public void drawLine(int x1, int y1, int x2, int y2): This method help us to draw the specified line between the coordinates (x1,y1) and (x2,y2) through the current color and style.
34
public void setColor(int red, int green, int blue): This method sets the current color to some designated RGB values.
public void fillArc(int x, int y, int width, int height, int startAngle, int arcAngle): This method can fill a circular or elliptical arc covering the specified rectangle.
public void setFont(Font, font): This method may set the font for all the contiguous text rendering operations.
public void drawstring(String string, int x, int y, int anchor): This method can help us to draw the specified String using the current font and color. The x, y position is the position of the anchor point.
6.4 Description of User Interface API
In this section, I illustrate my design briefly and explain it step by step, the main codes and some descriptions could be demonstrated as well.
6.4.1 Game. java
In my project, I use GameCanvas to draw the board and pieces on the mobile phone instead of invoking pictures, which requires a strongly logical programming capability but can save the space, as well as make the algorithm simpler. First of all, I build a main class Game, and define the parameters and life-cycle. Some explanations have been mentioned in the previous chapter. The structure of the programme is demonstrated in Code example 6.1. This code is also a part of my whole application.
Code example 6.1: import javax.microedition.midlet.*; import javax.microedition.lcdui.*;
public class Game extends MIDlet { GameCanvas game; // Define the object from class GameCanvas public Game() {
35
super(); game=new GameCanvas(this); // Generate the object from class GameCanvas } protected void startApp() { Display.getDisplay(this).setCurrent(game); // Draw the user interface on the screen } protected void pauseApp() { } protected void destroyApp(boolean unconditional) { } }
6.4.2 GameCanvas. java
Thereafter, I could get into the key field --- class GameCanvas, which is the most significant section on the user interface design. Through using a two-dimensional array, I can implement the initialization of board and pieces on the phone screen. More specifically, I initiate the intersections to non-zero in which the pieces are occupied, while initiate the intersections to zero in which the pieces are not occupied. As we can see from the code in Appendices, the graphic outputs and pieces movements are all based on this two-dimensional array.
In this case, I guess it is necessary to introduce some items in terms of Arrays. As we know, an array is a collection of variables of the same types, which is always used for a variety of purposes because it can offer a convenient means of grouping together related variables. (Mynttinen, 2011) To declare a one-dimensional array, we may use this general form:
type [ ] arrayName = new type [size];
For example, the following creates an int array of 10 elements and links it to an array reference variable named sample:
int [ ] sample = new int [10];
In fact, when we represent a one-dimensional array, sometimes we do not have to list all the values one by one. There is an easier approach to initialize an array:
36
type [ ] arrayName = {val1, val2, val3, ……};
When it comes to my design, I use the simplest form of the multidimensional array, two-dimensional array. The format is more or less similar to one-dimensional array, which can be indicated as follow:
type [ ][ ] arrayName = new type [size][size];
Similarly, to declare a two-dimensional integer array details of size 10, 20 (10 rows and 20 columns), we can use this form to indicate it:
int [ ] [] details = new int [10] [20];
However, if we initiate the board by using this general format related to my application, it would be a little bit longer and more complicated. So we can use a shorter and simpler way to initiate this int two-dimensional array just like the easier approach in one-dimensional array which I referred previously. The following Code example 6.2 is the initialization of this two-dimensional array with a simpler way which I came up with.
Code example 6.2: protected int point [ ][ ] = { {1,2,3,4,5,6,7,8,9}, // Initiate an integer array {0,0,0,0,0,0,0,0,0}, {0,10,0,0,0,0,0,11,0}, {12,0,13,0,14,0,15,0,16}, {0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0}, {28,0,29,0,30,0,31,0,32}, {0,26,0,0,0,0,0,27,0}, {0,0,0,0,0,0,0,0,0}, {17,18,19,20,21,22,23,24,25} }
Now that completing the initialization of this two-dimensional array related to the board, I could begin to design the initialization of all the pieces. Through executing if statements
37
based on this two-dimensional array, I can implement the initialization of these characters as Code example 6.3 shown.
Code example 6.3: for (i=0; i