28
1 Enterprise Application Development Using J2EE / © Shmulik London 2004 Interdisciplinary Center Herzeliza Israel Development of multi-user cellular games using J2EE Shmulik London Course Introduction Enterprise Application Development Using J2EE / © Shmulik London 2008 About this course A Workshop Course Mobile programming, J2EE, Deepen knowledge of Java But mostly OOP, design… Course project Enterprise Application Development Using J2EE / © Shmulik London 2008 About this course No rocket science but a lot of work No exam, but team ‘interview’ Work in teams 1-4 http://www1.idc.ac.il/coursesftp/cs/ j2ee/index.html news://news.idc.ac.il/News_j2ee

01 - Course Intro and J2ME - idc.ac.il · 3 Enterprise Application Development Using J2EE / © Shmulik London 2008 EA can be Boring… • What could be more fun than going over the

  • Upload
    others

  • View
    4

  • Download
    0

Embed Size (px)

Citation preview

Page 1: 01 - Course Intro and J2ME - idc.ac.il · 3 Enterprise Application Development Using J2EE / © Shmulik London 2008 EA can be Boring… • What could be more fun than going over the

1

Enterprise Application Development Using J2EE / © Shmulik London 2004

Interdisciplinary Center Herzeliza Israel

Development of multi-usercellular games using J2EE

Shmulik London

Course Introduction

Enterprise Application Development Using J2EE / © Shmulik London 2008

About this course

� A Workshop Course

� Mobile programming, J2EE,

Deepen knowledge of Java…

� But mostly OOP, design…

� Course project

Enterprise Application Development Using J2EE / © Shmulik London 2008

About this course

� No rocket science but a lot of work

� No exam, but team ‘interview’

� Work in teams 1-4

� http://www1.idc.ac.il/coursesftp/cs/

j2ee/index.html

� news://news.idc.ac.il/News_j2ee

Page 2: 01 - Course Intro and J2ME - idc.ac.il · 3 Enterprise Application Development Using J2EE / © Shmulik London 2008 EA can be Boring… • What could be more fun than going over the

2

Enterprise Application Development Using J2EE / © Shmulik London 2008

What are Enterprise Apps?

• These are the applications that drive banks, insurance companies, online stores, market places, Procurement, Supply chain, … B2B, B2C

They’re what makes the world go round…

Enterprise Application Development Using J2EE / © Shmulik London 2008

Robust

Non-Stop24/7

Enterprise App Challenges

ScaleableSecure Transactional

FaultTolerant

Heterogeneous

Distributed

Complex

Web Front

Multi Faces

Localized

Configurable Traceable

Enterprise Application Development Using J2EE / © Shmulik London 2008

Enterprise App Challenges

Robust

Non-Stop24/7

ScaleableSecure Transactional

FaultTolerant

Heterogeneous

Distributed

Complex

Web Front

Multi Faces

Localized

Configurable Traceable

Robust

Non-Stop24/7

Scaleable

Secure Transactional

FaultTolerant

Heterogeneous

Distributed

Complex

Web Front

Multi Faces

Localized

ConfigurableTraceable

How do you developsomething that addressesall these challenges??

This picture is taken from www.compugeeks.us/

Page 3: 01 - Course Intro and J2ME - idc.ac.il · 3 Enterprise Application Development Using J2EE / © Shmulik London 2008 EA can be Boring… • What could be more fun than going over the

3

Enterprise Application Development Using J2EE / © Shmulik London 2008

EA can be Boring…

• What could be more fun than

going over the details of

an insurance company?

Enterprise Application Development Using J2EE / © Shmulik London 2008

Let’s Make it Fun!

Enterprise Application Development Using J2EE / © Shmulik London 2008

Course Project(Example)

Internet

J2EE Server

J2ME Client

J2ME Client

Web Interface

RDBMS

Page 4: 01 - Course Intro and J2ME - idc.ac.il · 3 Enterprise Application Development Using J2EE / © Shmulik London 2008 EA can be Boring… • What could be more fun than going over the

4

Enterprise Application Development Using J2EE / © Shmulik London 2008

The Grand Theme

RDBMS

Data LayerBusiness Logic LayerPresentation Layer

Entity Beans

JSP

JSPSessionFaçade

ControllerServlet

Enterprise Application Development Using J2EE / © Shmulik London 2008

Start Thinking of a Game

Enterprise Application Development Using J2EE / © Shmulik London 2008

What kind of a game?

• Casual games, 2D or semi-3D

• With multi-player version

• Avoid heavy communication

• Simple maze games

• Adventure / Rule playing games

• Tournament board/card games

• Educational games

Page 5: 01 - Course Intro and J2ME - idc.ac.il · 3 Enterprise Application Development Using J2EE / © Shmulik London 2008 EA can be Boring… • What could be more fun than going over the

5

Enterprise Application Development Using J2EE / © Shmulik London 2008

Don’t be offensive!

• Non-violent. Combat games are fine but not brutal content

• No racial messages or anti religion messages

• No political sayings or hints

• No commercial political hints

• No pornographic content - pictures, audio or text

• ... you get my point, a decent, naive game, not offensive

• Do not violate any copyright, trademark, patent or license

– If you use media (images, audio, video), take care of the

copyright/license notice and act accordingly. You can use media that

is marked CC (Creative Commons)

• Do not choose games whose idea is patented and is patent is

still valid

Enterprise Application Development Using J2EE / © Shmulik London 2008

Where do you begin?

Enterprise Application Development Using J2EE / © Shmulik London 2008

Agile vs. Waterfall

Page 6: 01 - Course Intro and J2ME - idc.ac.il · 3 Enterprise Application Development Using J2EE / © Shmulik London 2008 EA can be Boring… • What could be more fun than going over the

6

Enterprise Application Development Using J2EE / © Shmulik London 2008

Functional Spec

• Before developing a software/feature you must first write a functional spec for it!

• It makes it clear what you need to develop, avoid pit-falls early, and most important - let you get feedback from customers early

“Programmers and software engineers who dive into code without writing aspec tend to think they're cool gunslingers, shooting from the hip. They're not. They are terribly unproductive. They write bad code and produce shoddysoftware, and they threaten their projects by taking giant risks which are completely uncalled for.”

Joel Spolsky / Painless Functional Specifications

Enterprise Application Development Using J2EE / © Shmulik London 2008

Multi-player SokobanFunctional Spec

• A multi-player variant of the game Sokoban

• Mobile/J2ME client and web interface

• Played on a grid-map stored at the server

• Each user has a player figure on the mapand sees a portion of the map that surrounds the figure

• All players share the same map

• A user can join and leave the game whenever s/he like; when a user logs intothe game, a new player figure is created for him/her and placed on the map

Enterprise Application Development Using J2EE / © Shmulik London 2008

Game Rules

• The map is a grid consisting of cells. The content of a cell can be one of the following:

– Empy, Wall, Box, Gem, Your figure, Opponent

• You move by discrete cells – up/down/left…

• Goal: collect as much gems as possible.For each get you get 10 points

• You can’t walk into a wall, opponent or box

• You can push a box if the cell behind it is empty. You can only push boxes, not pull!

• (you can block opponents from reaching gems before you with boxes)

Wall cellYour figure(colored green)

Box

Gem

Opponent(colored red)

Page 7: 01 - Course Intro and J2ME - idc.ac.il · 3 Enterprise Application Development Using J2EE / © Shmulik London 2008 EA can be Boring… • What could be more fun than going over the

7

Enterprise Application Development Using J2EE / © Shmulik London 2008

Game Flow

click

Tap3sec

click

Failed toConnect(message dialog)

click

Game over(message dialog)

5sec

Enterprise Application Development Using J2EE / © Shmulik London 2008

Milestones

• Break project into milestones

• Prototype uncertain features

• Advance in small phases, while the system is always working

• Our milestones:

– Standalone mobile game

– Pushing logic to server

– Multi-player game

– Adding web-interface

– Storing state in database

Enterprise Application Development Using J2EE / © Shmulik London 2008

Ex#1

• Functional spec for course game

• Simplest mobile game

– Simple, but yet a game

– What is a game?

Page 8: 01 - Course Intro and J2ME - idc.ac.il · 3 Enterprise Application Development Using J2EE / © Shmulik London 2008 EA can be Boring… • What could be more fun than going over the

8

Enterprise Application Development Using J2EE / © Shmulik London 2008

How to approach coding?

Be driven by

Abstraction,

not by functionality!

What do I mean…

Enterprise Application Development Using J2EE / © Shmulik London 2008

Demo

Enterprise Application Development Using J2EE / © Shmulik London 2008

… how to approach coding

• A computer is a Universal Machine

• A program creates a machine, creates a world

• This machine (world) and its parts are as much as real as are objects of our world

• When writing a program, don’t think brute-forceabout solving the problem. Instead, think about the machine you’re designing to solve it!

• Once appreciating the realness of this world, you’ll find that reconstructing a software ‘wall’ is as costly as a brick-n-mortar one, and your architecture will become modular!

Page 9: 01 - Course Intro and J2ME - idc.ac.il · 3 Enterprise Application Development Using J2EE / © Shmulik London 2008 EA can be Boring… • What could be more fun than going over the

9

Enterprise Application Development Using J2EE / © Shmulik London 2004

Interdisciplinary Center Herzeliza Israel

Mobile Development

Enterprise Application Development Using J2EE / © Shmulik London 2008

Active Industry

1999199919991999 20072007200720071983198319831983

Enterprise Application Development Using J2EE / © Shmulik London 2008

Complex Market

Nokia

Sun Microsystems

AppleGoogle

Microsoft

RIM

NTT DoCoMo

J2ME Windows Mobile

Android

iPhone

Others...

BlackBerryQualcommBREW

Symbian

DoJa

Page 10: 01 - Course Intro and J2ME - idc.ac.il · 3 Enterprise Application Development Using J2EE / © Shmulik London 2008 EA can be Boring… • What could be more fun than going over the

10

Enterprise Application Development Using J2EE / © Shmulik London 2004

Interdisciplinary Center Herzeliza Israel

Introduction to J2ME

Java 2 Micro Edition

Enterprise Application Development Using J2EE / © Shmulik London 2008

What is J2ME

• Application development platform for

consumer & embedded devices– PDAs, mobile phones, set-top boxes, vehicle embedded,

… in practice mobile phones!

• Committee Driven– Sun Microsystems, Nokia, Sony Ericsson, Motorola, NTT DoCoMo, …

• Augment existing platforms rather then

replace them!

• Since 2000

Enterprise Application Development Using J2EE / © Shmulik London 2008

Java Platform Stack

J2EE

Java 2Enterprise

Edition

J2SE

Java 2StandardEdition

J2MEJava 2Micro

EditionJavaCard

CDC CLDC

Page 11: 01 - Course Intro and J2ME - idc.ac.il · 3 Enterprise Application Development Using J2EE / © Shmulik London 2008 EA can be Boring… • What could be more fun than going over the

11

Enterprise Application Development Using J2EE / © Shmulik London 2008

J2ME Architecture

KVM

Profiles Optionalpackages

Configuration APIs

Enterprise Application Development Using J2EE / © Shmulik London 2008

J2ME Architecture

KVM

MIDP Optionalpackages

CLDC

Enterprise Application Development Using J2EE / © Shmulik London 2008

MIDP Profile

• Hardware (for MIDP 2.0)

– 256Kb “ROM”, 8Kb app storage, 128Kb runtime (e.g. heap)

– Min display 96x54 1bit approx 1:1

– Keypad (1 or 2 hand) and/or stylus

– 2-way wireless networking (limited bandwidth, possibly intermittent)

– Ability to play sounds

Page 12: 01 - Course Intro and J2ME - idc.ac.il · 3 Enterprise Application Development Using J2EE / © Shmulik London 2008 EA can be Boring… • What could be more fun than going over the

12

Enterprise Application Development Using J2EE / © Shmulik London 2008

MIDP Profile

• Application model – MIDlet

• Libraries

– UI (LCDUI)

– Persistence (RMS)

– Networking

– Sound

– Game

– HTTPS & SSL

– Utility classes

Enterprise Application Development Using J2EE / © Shmulik London 2004

Interdisciplinary Center Herzeliza Israel

Writing MIDlets

Enterprise Application Development Using J2EE / © Shmulik London 2008

MIDlets

• MIDP applications are called MIDlets

• Executed within a container / have

a life-cycle (like applets)

• Don’t have a main() method and

cannot cause the VM to exit

Page 13: 01 - Course Intro and J2ME - idc.ac.il · 3 Enterprise Application Development Using J2EE / © Shmulik London 2008 EA can be Boring… • What could be more fun than going over the

13

Enterprise Application Development Using J2EE / © Shmulik London 2008

MIDlets

• Extends javax.microedition.midlet.MIDlet

• Should have no-arg public constructor

• Implements life-cycle methods defined in

class MIDlet to react to life-cycle events

– startApp(), pauseApp(), destroyApp()

Enterprise Application Development Using J2EE / © Shmulik London 2008

MIDlet life-cycle

Paused

Destroyed

Active

Constructed

User/device drivenor MIDlet requesttermination bynotifyDestroyed()

User/device drivenor MIDlet requesttermination bynotifyDestroyed()

Device driven MIDlet requests usingresumeRequest()

Device driven orMIDlet call tonotifyPaused()

Enterprise Application Development Using J2EE / © Shmulik London 2008

Writing a MIDlet

import java.util.*;

import javax.microedition.midlet.*;

import javax.microedition.lcdui.*;

public class DecisionMakerMIDlet

extends MIDlet {

... // continued on next slide

import java.util.*;

import javax.microedition.midlet.*;

import javax.microedition.lcdui.*;

public class DecisionMakerMIDlet

extends MIDlet {

... // continued on next slide

Page 14: 01 - Course Intro and J2ME - idc.ac.il · 3 Enterprise Application Development Using J2EE / © Shmulik London 2008 EA can be Boring… • What could be more fun than going over the

14

Enterprise Application Development Using J2EE / © Shmulik London 2008

Writing a MIDlet

public class DecisionMakerMIDlet extends...

private Random random;private Form form;private StringItem label;

public DecisionMakerMIDlet() {random = new Random();form = new Form(“DecisionMaker”);label = new StringItem(“”, “”);form.append(label);

}

public class DecisionMakerMIDlet extends...

private Random random;private Form form;private StringItem label;

public DecisionMakerMIDlet() {random = new Random();form = new Form(“DecisionMaker”);label = new StringItem(“”, “”);form.append(label);

}

Enterprise Application Development Using J2EE / © Shmulik London 2008

Writing a MIDlet

... public void startApp() {

boolean toss = random.nextInt()>0;String decision = toss ?

“Yes, you should do it!” :“Better not do it!”;

label.setText(decision);Display.getDisplay(this).

setCurrent(form);}

public void pauseApp() {}

... public void startApp() {

boolean toss = random.nextInt()>0;String decision = toss ?

“Yes, you should do it!” :“Better not do it!”;

label.setText(decision);Display.getDisplay(this).

setCurrent(form);}

public void pauseApp() {}

Enterprise Application Development Using J2EE / © Shmulik London 2008

Writing a MIDlet

public void startApp() {

...

}

public void pauseApp() {}

public void destroyApp(boolean unconditional) {

}

}

public void startApp() {

...

}

public void pauseApp() {}

public void destroyApp(boolean unconditional) {

}

}

Page 15: 01 - Course Intro and J2ME - idc.ac.il · 3 Enterprise Application Development Using J2EE / © Shmulik London 2008 EA can be Boring… • What could be more fun than going over the

15

Enterprise Application Development Using J2EE / © Shmulik London 2008

MIDlet in Action

Enterprise Application Development Using J2EE / © Shmulik London 2004

Interdisciplinary Center Herzeliza Israel

Questions?

Enterprise Application Development Using J2EE / © Shmulik London 2008

Managed Environments

• Applets, MIDles, Servlets, EJB, …

• Why not regular application model?

• The phone needs to call the MIDlet(e.g. incoming call), and vice versa

• Prevent the MIDlet from doing various things – SecurityManager

• More implementation freedom

• Context, initialization, life-cycle

Page 16: 01 - Course Intro and J2ME - idc.ac.il · 3 Enterprise Application Development Using J2EE / © Shmulik London 2008 EA can be Boring… • What could be more fun than going over the

16

Enterprise Application Development Using J2EE / © Shmulik London 2004

Interdisciplinary Center Herzeliza Israel

Development, Packaging and Deployment

Enterprise Application Development Using J2EE / © Shmulik London 2008

MIDlet Development

Coding

Compilation

Pre-verify

[Obfuscate]

Emulator

Package

Deploy

Run on Device

Enterprise Application Development Using J2EE / © Shmulik London 2008

MIDlet Development

• You write the MIDlet in your favorite editor

• You compile the MIDlet into regular Java classes (same bytecode)

• However, you need to run a pre-verifier on the bytecode

Page 17: 01 - Course Intro and J2ME - idc.ac.il · 3 Enterprise Application Development Using J2EE / © Shmulik London 2008 EA can be Boring… • What could be more fun than going over the

17

Enterprise Application Development Using J2EE / © Shmulik London 2008

Pre-verifier

• The pre-verifier

– Verifies that your classes conform to the reduced libraries of CLDC & MIDP

– Inline local method calls

– Add/removes bytecode attributes to simplify the task of the device bytecode verifier and reduce the size of the JAR

Enterprise Application Development Using J2EE / © Shmulik London 2008

Emulator

• You run the MIDlet first inside an Emulator – difficult to debug from within the device!

• Each vendor provides an emulator and sometimes complete development tools

Enterprise Application Development Using J2EE / © Shmulik London 2008

MIDlet Suites

• You package one or more MIDlets as a MIDlet Suite (unit of installation)

• MIDlets of the same suite:

– Share the same namespace – they can interact using static references

– Can share persistence storage entries

Page 18: 01 - Course Intro and J2ME - idc.ac.il · 3 Enterprise Application Development Using J2EE / © Shmulik London 2008 EA can be Boring… • What could be more fun than going over the

18

Enterprise Application Development Using J2EE / © Shmulik London 2008

MIDlet Suites

• A MIDlet Suite consist of two files:

– A JAR (Java Archive) file that contains all the classes & resources plus information about the MIDlets

– A JAD (Java Application Descriptor) file – a simple text file containing installation information about the MIDlets.

Enterprise Application Development Using J2EE / © Shmulik London 2008

JAD & Manifest

• The JAD & Manifest are almost identical

• Describe the content of the suite

• The JAD is used during installation

• They are simple text files containing attribute/value pairs

Enterprise Application Development Using J2EE / © Shmulik London 2008

JAD & Manifest

MIDlet-1: DecisionMaker,, DecisionMakerMIDlet

MIDlet-Jar-Size: 1409

MIDlet-Jar-URL: DecisionMaker.jar

MIDlet-Name: DecisionMaker

MIDlet-Vendor: Shmulik London

MIDlet-Version: 1.0

MIDlet-1: DecisionMaker,, DecisionMakerMIDlet

MIDlet-Jar-Size: 1409

MIDlet-Jar-URL: DecisionMaker.jar

MIDlet-Name: DecisionMaker

MIDlet-Vendor: Shmulik London

MIDlet-Version: 1.0

DecisionMaker.jad

Page 19: 01 - Course Intro and J2ME - idc.ac.il · 3 Enterprise Application Development Using J2EE / © Shmulik London 2008 EA can be Boring… • What could be more fun than going over the

19

Enterprise Application Development Using J2EE / © Shmulik London 2008

MIDlet Deployment

• Direct– USB, Infrared, Bluetooth, ...

• OTA (Over the Air) Provisioning– Download the application from a web-

server

• In both methods the device inspects the JAD, then download the JAR and install it

Enterprise Application Development Using J2EE / © Shmulik London 2008

OTA Provisioning

This image was taken from http://developers.sun.com/techtopics/mobility/midp/articles/ota/

Enterprise Application Development Using J2EE / © Shmulik London 2008

Development Process Demo

Page 20: 01 - Course Intro and J2ME - idc.ac.il · 3 Enterprise Application Development Using J2EE / © Shmulik London 2008 EA can be Boring… • What could be more fun than going over the

20

Enterprise Application Development Using J2EE / © Shmulik London 2008

J2ME Cons & Pros

☺ Ubiquitous

☺ Simple, shortlearning curve

☺ Veteran

☺ Anyone canpublish

☹ Signing!!!

☹ Wide differences

☹ Low-end UI support

☹ Development tools

☹ Marketplace

Enterprise Application Development Using J2EE / © Shmulik London 2004

Interdisciplinary Center Herzeliza Israel

MIDP API

Enterprise Application Development Using J2EE / © Shmulik London 2008

MIDP 1.0 API Packages

javax.microedition.rmsPersistent Storage

javax.microedition.ioNetworking

javax.microedition.lcduiUser Interface

javax.microedition.midletApplication Model

java.lang, java.io, java.utilCLDC

Page 21: 01 - Course Intro and J2ME - idc.ac.il · 3 Enterprise Application Development Using J2EE / © Shmulik London 2008 EA can be Boring… • What could be more fun than going over the

21

Enterprise Application Development Using J2EE / © Shmulik London 2004

Interdisciplinary Center Herzeliza Israel

User Interface

javax.microedition.lcdui

Enterprise Application Development Using J2EE / © Shmulik London 2008

LCDUI

• UI libraries are often large & complex

– Swing 1587 classes, AWT 485 classes -

~40% of all Java Standard API

• Mobile devices require simpler API

• Devices vary greatly in their graphical

capabilities – resolution, color, ...

Enterprise Application Development Using J2EE / © Shmulik London 2008

LCDUI

• LCDUI consist of:

– Limited (!) set of widgets for building forms, with limited but high level functionality

– Low level component for drawing graphics and handling low level events (games, charts..)

Page 22: 01 - Course Intro and J2ME - idc.ac.il · 3 Enterprise Application Development Using J2EE / © Shmulik London 2008 EA can be Boring… • What could be more fun than going over the

22

Enterprise Application Development Using J2EE / © Shmulik London 2008

Display & Displayable

• LCDUI doesn’t use the abstraction

of a windowing system

• A MIDlet is associated with a single

Display object that can display a

single Displayable object at a time

• A Displayable object occupies the

entire screen of the device

Enterprise Application Development Using J2EE / © Shmulik London 2008

Displayable

Displayable

Screen

FormText Box ListAlert Canvas

Enterprise Application Development Using J2EE / © Shmulik London 2008

Form widgets

Adding an image to a formImageItem

Radio-buttons, checkboxesChoiceGroup

Progress or ranged valueGauge

Date & Time inputDateField

Short textual inputTextField

Display a labelStringItem

Page 23: 01 - Course Intro and J2ME - idc.ac.il · 3 Enterprise Application Development Using J2EE / © Shmulik London 2008 EA can be Boring… • What could be more fun than going over the

23

Enterprise Application Development Using J2EE / © Shmulik London 2008

Non uniform look

• The same screen can look very different on different devices

• Some vendors offer proprietary UI API for better results

Enterprise Application Development Using J2EE / © Shmulik London 2008

Alert code example

if ( last cell was home and current cell is

other then home and no Bluetooth signal

from radio panel...) {

Alert alert = new Alert(

“Warning", // Title

“Didn’t you forget the radio panel?”,

AlertType.WARNING

);

Display.getDisplay(this).setCurrent(alert);

if ( last cell was home and current cell is

other then home and no Bluetooth signal

from radio panel...) {

Alert alert = new Alert(

“Warning", // Title

“Didn’t you forget the radio panel?”,

AlertType.WARNING

);

Display.getDisplay(this).setCurrent(alert);

Enterprise Application Development Using J2EE / © Shmulik London 2008

Form Example

public class LoginForm extends Form {

private TextField usernameField;private TextField passwordField;

public LoginForm() {super(“Login"); // TitleusernameField = new TextField(

"User", "", 6, TextField.ANY);passwordField = new TextField(

"Pass", "", 12, TextField.PASSWORD);append(usernameField);

append(passwordField);}

public class LoginForm extends Form {

private TextField usernameField;private TextField passwordField;

public LoginForm() {super(“Login"); // TitleusernameField = new TextField(

"User", "", 6, TextField.ANY);passwordField = new TextField(

"Pass", "", 12, TextField.PASSWORD);append(usernameField);

append(passwordField);}

Page 24: 01 - Course Intro and J2ME - idc.ac.il · 3 Enterprise Application Development Using J2EE / © Shmulik London 2008 EA can be Boring… • What could be more fun than going over the

24

Enterprise Application Development Using J2EE / © Shmulik London 2008

High-level features

Ticker ticker = new Ticker("");

screen.setTicker(ticker);

...

String nasdaq100Info = getNasdaqInfo();

ticker.setString("NASDAQ-100 "+nasdaq100Info);

Ticker ticker = new Ticker("");

screen.setTicker(ticker);

...

String nasdaq100Info = getNasdaqInfo();

ticker.setString("NASDAQ-100 "+nasdaq100Info);

• Components have limited but high-level tailored features

Enterprise Application Development Using J2EE / © Shmulik London 2008

Commands

• Commands allow the user to perform actions

– navigate between screens, submit the input of a screen, ..

• They are presented as soft-keys, buttons or menu items

Commands

Enterprise Application Development Using J2EE / © Shmulik London 2008

Commands

• The location of the command is set by ‘hints’ you provide– sometimes there is no room for

displaying all the commands

– preserve the default locations of the device for common commands

• You can add commands to any Displayable. You can share commands between Displayables.

Page 25: 01 - Course Intro and J2ME - idc.ac.il · 3 Enterprise Application Development Using J2EE / © Shmulik London 2008 EA can be Boring… • What could be more fun than going over the

25

Enterprise Application Development Using J2EE / © Shmulik London 2008

Commands

Form loginForm = new LoginForm();

Command loginCommand = new Command(

"Login", Command.OK, 1);

Command exitCommand = new Command(

"Exit", Command.EXIT, 1);

loginForm.addCommand(loginCommand);

loginForm.addCommand(exitCommand);

Display.getDisplay(this).setCurrent(loginForm);

Form loginForm = new LoginForm();

Command loginCommand = new Command(

"Login", Command.OK, 1);

Command exitCommand = new Command(

"Exit", Command.EXIT, 1);

loginForm.addCommand(loginCommand);

loginForm.addCommand(exitCommand);

Display.getDisplay(this).setCurrent(loginForm);

label type priority

Enterprise Application Development Using J2EE / © Shmulik London 2008

Handling Commands

public SampleMIDlet extends MIDlet

implements CommandListener {

private Command loginCommand = new Command(

"Login", Command.SCREEN, 1);

private Command exitCommand = new Command(

"Exit", Command.EXIT, 1);

private boolean initialized = false;

public SampleMIDlet extends MIDlet

implements CommandListener {

private Command loginCommand = new Command(

"Login", Command.SCREEN, 1);

private Command exitCommand = new Command(

"Exit", Command.EXIT, 1);

private boolean initialized = false;

Enterprise Application Development Using J2EE / © Shmulik London 2008

Handling Commands

public void startApp() {if (!initialized) {

Form loginForm = new LoginForm();loginForm.addCommand(loginCommand);loginForm.addCommand(exitCommand);loginForm.setCommandListener(this);getDisplay(this).setCurrent(loginForm);initialized = true;

}}

public void pauseApp() {}

public void destroyApp(boolean unconditional) {}

public void startApp() {if (!initialized) {

Form loginForm = new LoginForm();loginForm.addCommand(loginCommand);loginForm.addCommand(exitCommand);loginForm.setCommandListener(this);getDisplay(this).setCurrent(loginForm);initialized = true;

}}

public void pauseApp() {}

public void destroyApp(boolean unconditional) {}

Page 26: 01 - Course Intro and J2ME - idc.ac.il · 3 Enterprise Application Development Using J2EE / © Shmulik London 2008 EA can be Boring… • What could be more fun than going over the

26

Enterprise Application Development Using J2EE / © Shmulik London 2008

Handling Commands

public void commandAction(Command command, Displayable displayable) {

if (command == exitCommand) {notifyDestroyed();return;

}if (command == loginCommand) {

... perform login logic}

}}

public void commandAction(Command command, Displayable displayable) {

if (command == exitCommand) {notifyDestroyed();return;

}if (command == loginCommand) {

... perform login logic}

}}

Enterprise Application Development Using J2EE / © Shmulik London 2004

Interdisciplinary Center Herzeliza Israel

Questions?

Enterprise Application Development Using J2EE / © Shmulik London 2008

Low Level UI

• Screens �provide simple way for building forms

� have very limited functionality

� limit control over their appearance

• Canvas provide better control over the appearance and behavior– specify appearance by overriding paint()

method and drawing graphics (like Swing/AWT)

– handle event by overriding callback methods (like in JDK 1.04)

Page 27: 01 - Course Intro and J2ME - idc.ac.il · 3 Enterprise Application Development Using J2EE / © Shmulik London 2008 EA can be Boring… • What could be more fun than going over the

27

Enterprise Application Development Using J2EE / © Shmulik London 2008

Canvas Example

public ChessboardCanvas extends Canvas {

protected void paint(Graphics g) {int width = getWidth();int height = getHeight();g.setColor(0xffffff);g.fillRect(0, 0, width, height);g.setColor(0x000000);int cell = 10;int size = cell*8;g.drawRect(0, 0, size, size);for (int row=0,y=0; row<8; row++,y+=cell) {

for (int col=0,x=0; col<8; col++,x+=cell) {if ((row+col)%2==0) {

g.fillRect(x, y, cell, cell);}

}}

}}

public ChessboardCanvas extends Canvas {

protected void paint(Graphics g) {int width = getWidth();int height = getHeight();g.setColor(0xffffff);g.fillRect(0, 0, width, height);g.setColor(0x000000);int cell = 10;int size = cell*8;g.drawRect(0, 0, size, size);for (int row=0,y=0; row<8; row++,y+=cell) {

for (int col=0,x=0; col<8; col++,x+=cell) {if ((row+col)%2==0) {

g.fillRect(x, y, cell, cell);}

}}

}}

Enterprise Application Development Using J2EE / © Shmulik London 2008

Loading Resources

import java.io.*;import javax.mircoedition.lcdui.*;

public class GameCanvas extends Canvas {

private int x = getWidth()/2;private int y = getHeight()/2;

private Image playerIcon;

public GameCanvas() throws IOException {playerIcon = Image.createImage(

"/icons/player.png");}

import java.io.*;import javax.mircoedition.lcdui.*;

public class GameCanvas extends Canvas {

private int x = getWidth()/2;private int y = getHeight()/2;

private Image playerIcon;

public GameCanvas() throws IOException {playerIcon = Image.createImage(

"/icons/player.png");}

Enterprise Application Development Using J2EE / © Shmulik London 2008

Loading Resources

protected void paint(Graphics g) {g.setColor(0xffffff);g.fillRect(0, 0, getWidth(), getHeight());if (playerIcon != null) {

g.drawImage(playerIcon, x, y,Graphics.HCENTER | Graphics.VCENTER);

}}

// ... next handling key-strokes

}

protected void paint(Graphics g) {g.setColor(0xffffff);g.fillRect(0, 0, getWidth(), getHeight());if (playerIcon != null) {

g.drawImage(playerIcon, x, y,Graphics.HCENTER | Graphics.VCENTER);

}}

// ... next handling key-strokes

}

Page 28: 01 - Course Intro and J2ME - idc.ac.il · 3 Enterprise Application Development Using J2EE / © Shmulik London 2008 EA can be Boring… • What could be more fun than going over the

28

Enterprise Application Development Using J2EE / © Shmulik London 2008

Handling Keystrokes

protected void keyPressed(int keyCode) {int gameAction = getGameAction(keyCode);switch (gameAction) {

case Canvas.LEFT:x-=5; break;

case Canvas.RIGHT:x+=5; break;

case Canvas.UP:y-=5; break;

case Canvas.DOWN:y+=5; break;

}repaint();

}}

protected void keyPressed(int keyCode) {int gameAction = getGameAction(keyCode);switch (gameAction) {

case Canvas.LEFT:x-=5; break;

case Canvas.RIGHT:x+=5; break;

case Canvas.UP:y-=5; break;

case Canvas.DOWN:y+=5; break;

}repaint();

}}

Enterprise Application Development Using J2EE / © Shmulik London 2004

Interdisciplinary Center Herzeliza Israel

Questions?