Menu
When doing network programming, you can always revert to the HTTP programming model when you need to, confident that your application will run on any MIDP device, whether it is a GSM phone with a WAP stack, a phone with i-mode, a Palm VII wireless, or a hand-held device with Bluetooth. In addition, HTTP is firewall-friendly. Java Programming Code to Make Calculate. Following is a simple Java Program which is a menu-driven program based on simple calculation like addition, subtraction, multiplication and division according to user's choice: /. Java Program Example - Make Calculator./ import java.util.Scanner; public class JavaProgram public static void.
10 Mar 2009CPOL
Introduction
JAVA ME (or in its full name JAVA Mobile Edition) is a small lightweight version of the JAVA language. Its running environments are focused on limited memory and/or processing power such as cellular phones, PDAs, TV set-top boxes, smart cards, etc.
Download SDK and Create Your First Project
In this tutorial, we will learn how to write a simple 'Hello world' application using the most elementary SDK available, the Sun Java ME Wireless toolkit which can be downloaded from here.
After installing the SDK, we can execute the following stages:
After installing the SDK, we can execute the following stages:
- Start running the wireless toolkit by choosing: Start Button -> Sun Java (TM) Wireless Toolkit for CLDC -> Wireless toolkit.
- You should see a window like this one:
- In order to create a new project, just press the 'New Project' button. A new window will be opened and you will have to fill in a project name and a MIDlet (
MIDlet
class name will be explained later) class name. In our case, the project name will be Hello World and theMIDlet
name will beHelloWorldMIDlet
. - An additional window will be opened with plenty of options for various settings. At this stage, just press OK button.
- At the end of the project creation, you will see in the main window information about where to place source files, resource files (such as pictures and sounds files), and application files. Java source files can be written in any text editor.
A Typical MIDlet Structure
A typical JAVA ME application consists of a Java source file and optionally you can add PNG files (for graphic files), MIDI and WAV files for audio files. The build and compile stage creates two files, JAR file which contains all the executable files and a JAD (JAVA descriptor) file. A JAD file contains information about the project such as vendor, CLDC and MIDP Configurations, and user defined definitions.
The core framework of the JAVA ME environment is the CLDC (Connected Limited Device Configuration).
CLDC contains the core classes of the JAVA ME environment, classes that handle IO functions, language functions and other basic utilities. The current version of CLDC is 1.1.
The core framework of the JAVA ME environment is the CLDC (Connected Limited Device Configuration).
CLDC contains the core classes of the JAVA ME environment, classes that handle IO functions, language functions and other basic utilities. The current version of CLDC is 1.1.
Alongside with the CLDC comes a profile. There are several profiles such as MIDP (Mobile Information Device Profile) for use with cellular phones and hand held devices. IMP (Information Module Profile) for use with other limited devices and DOJA for use with NTT DoCoMo i-mode mobile phones.
Every JAVA ME application must have at least one class which is extended from
MIDlet
class. This class actually manages the whole application. An application state defines the execution mode of the application. There are three runtime states of a JAVA ME application: 'Active', 'Paused' and 'Destroyed' states. - An active state is the normal running mode of the application.
- A paused state is invoked when an interruption occurs such as an incoming phone call.
- A destroyed state is invoked when the
MIDlet
application ends its operation.
There are three abstract methods in a
MIDlet
class: startApp():
is called when theMIDlet
enters the active state.pauseApp():
is called when theMIDlet
enters the paused state.destroyApp(boolean unconditional):
is called when theMIDlet
ends its execution.
These three methods are required for initializing and freeing resources and other user defined objects.
Our sample program is a simple
As I mentioned before, every
Our sample program is a simple
MIDlet
which displays 'Hello World' on the cellular phone device.As I mentioned before, every
MIDlet
has at least one class which is extended from MIDlet
class. The name of the MIDlet
has to be the name which was defined in the 'New Project' stage. The skeleton class looks like this:There are several classes which are all extended from the
The
First of all, you have to define a
Displayable
class. These classes are classes that can be displayed on the screen. One such common class is the Canvas
class. This class, which is very popular in game application, can draw, various objects on the screen, display pictures (in PNG format) and can handle animations too. Other Displayable
classes are the List
class which display menus and the Form
class which displays plain text. The
Display
class manages the display of the MIDlet
. By this class, the users can decide which Displayable
class should be displayed right now.First of all, you have to define a
Displayable
class. Then you have to initialize it:
As I mentioned before, the
display
class has to show a Displayable
class. So now we have to create a class which is extended from the Displayable
class. In the constructor method, we initialize the
MainScreen
class: And in the startup method, we set the
mainScreen
class to be displayed on the screen. Now, all that is left is to compile and run the midlet.
Conclusion
This has been only a basic
Another Java ME game with full source code can be found here.
MIDlet
. Anyone who is interested in writing a full scale JAVA ME game can see my article here.Another Java ME game with full source code can be found here.
The following Java code is for a simple calculator. There are nine JButtons to represent the numbers 1 to 9, and three JButtons for addition, subtraction and totaling the result. A JTextField at the top keeps track of the numbers being pressed and the result of the arithmetic operation.
Java Code
The purpose of this Java program is to show how to implement an ActionListener interface for handling JButton button event clicks by using the containing class, an inner class, and an anonymous inner class.