81
1 LWUIT Tutorial Jonathan Knudsen Chen Fishbein Ariel Levin Sun Microsystems, Inc. 1

LWUIT M3DD Tutorial 1.0 Important

  • Upload
    long-le

  • View
    35

  • Download
    0

Embed Size (px)

DESCRIPTION

J2ME

Citation preview

Page 1: LWUIT M3DD Tutorial 1.0 Important

1

LWUIT TutorialJonathan KnudsenChen FishbeinAriel LevinSun Microsystems, Inc.

1

Page 2: LWUIT M3DD Tutorial 1.0 Important

2Copyright © 2009 Sun Microsystems, Inc. All rights reserved.

Goals• Learn about LWUIT’s capabilities• Working practical knowledge of LWUIT• Fun!

Page 3: LWUIT M3DD Tutorial 1.0 Important

3Copyright © 2009 Sun Microsystems, Inc. All rights reserved.

Outline• The Big Fly-In• User Interface Choices• Ta-Dum!• On-Device Portal• LWUIT on Whatever• Show Me the Code!• Working With Forms• Component Tour

• Lists• Working with Threads• Dialogs• Working with Containers• Styles, Themes, Resources• Animation and Transitions• Stuff That Just Wouldn’t Fit

Page 4: LWUIT M3DD Tutorial 1.0 Important

4

The Big Fly-In

4

Page 5: LWUIT M3DD Tutorial 1.0 Important

5Copyright © 2009 Sun Microsystems, Inc. All rights reserved.

Java Technology is Everywhere

Java EE Java SE Java ME Java Card

...and more!

The Big Fly-in

Page 6: LWUIT M3DD Tutorial 1.0 Important

6Copyright © 2009 Sun Microsystems, Inc. All rights reserved.

Java ME, Big to Small• CDC

> Most JVM features> Add UI and other APIs to make a complete stack> Set-top boxes, car computers, toasters

• CLDC> Includes trimmed JVM> Usually has MIDP on top to make a stack

• Java Card> Java technology on a smart card> Very widely deployed

The Big Fly-in

Page 7: LWUIT M3DD Tutorial 1.0 Important

7Copyright © 2009 Sun Microsystems, Inc. All rights reserved.

A Very Brief History• 1999: KVM / DoJa• September 2000: MIDP 1.0• November 2002: MIDP 2.0• July 2003: JTWI 1.0• September 2006: MSA 1.0• Feburary 2008: MSA 1.1• On deck: MIDP 3.0

The Big Fly-in

Page 8: LWUIT M3DD Tutorial 1.0 Important

8Copyright © 2009 Sun Microsystems, Inc. All rights reserved.

The Big Fly-inMSA 1.0 1.1

Page 9: LWUIT M3DD Tutorial 1.0 Important

9

User Interface ChoicesBesides LWUIT

9

Page 10: LWUIT M3DD Tutorial 1.0 Important

10Copyright © 2009 Sun Microsystems, Inc. All rights reserved.

The Menu• SVG• 3D Graphics• OpenGL ES• LCDUI

and...• LWUIT

User Interface Choices: LCDUI

Page 11: LWUIT M3DD Tutorial 1.0 Important

11Copyright © 2009 Sun Microsystems, Inc. All rights reserved.

Scalable Vector Graphics (SVG)• XML dialect for describing images• A subset, SVG Tiny, is good for mobile devices• Excellent choice for variable screen sizes• Attractive user interfaces• Good separation between programmer and artist

roles

User Interface Choices: SVG

Page 12: LWUIT M3DD Tutorial 1.0 Important

12Copyright © 2009 Sun Microsystems, Inc. All rights reserved.

JSR 226 SVG API• Play animations• Interactive• Can also create elements at runtime

User Interface Choices: SVG

Page 13: LWUIT M3DD Tutorial 1.0 Important

13Copyright © 2009 Sun Microsystems, Inc. All rights reserved.

JSR 184 Mobile 3D Graphics (M3G)• Render canned content

> Good separation between programmer and artist roles> Known as retained mode

• Create 3D content at runtime> Good for visualizations> Known as immediate mode

• Uses available device 3D engine

User Interface Choices: 3D Graphics

Page 14: LWUIT M3DD Tutorial 1.0 Important

14Copyright © 2009 Sun Microsystems, Inc. All rights reserved.

OpenGL ES• JSR 239, not part of

MSA 1.0• OpenGL is a standard

API for 2D and 3D graphics

• OpenGL ES is a subset for mobile devices

• Close to the metal

User Interface Choices: OpenGL ES

Page 15: LWUIT M3DD Tutorial 1.0 Important

15Copyright © 2009 Sun Microsystems, Inc. All rights reserved.

LCDUI• MIDP's user interface toolkit• User interface from a can

> TextBox, Alert, List, Form> Easy to use> Generalized across devices, but...> ...no guarantees about consistent user experience

• Do your own drawing with Canvas> Caramba! Back to square one!> Tricky to get this right

User Interface Choices: LCDUI

Page 16: LWUIT M3DD Tutorial 1.0 Important

16Copyright © 2009 Sun Microsystems, Inc. All rights reserved.

A Full Form

Nokia Series 40Nokia Series 40 3rd ed.Sun Java Wireless Toolkit

User Interface Choices: LCDUI

Page 17: LWUIT M3DD Tutorial 1.0 Important

17Copyright © 2009 Sun Microsystems, Inc. All rights reserved.

Command Examples

User Interface Choices: LCDUI

Page 18: LWUIT M3DD Tutorial 1.0 Important

18Copyright © 2009 Sun Microsystems, Inc. All rights reserved.

You Say To-may-to, I Say To-mah-to

Nokia Series 40 Motorola RAZRSun Java Wireless Toolkit

User Interface Choices: LCDUI

Page 19: LWUIT M3DD Tutorial 1.0 Important

19Copyright © 2009 Sun Microsystems, Inc. All rights reserved.

Command Placement

Sun toolkit RAZR Series 40 3rd edition

User Interface Choices: LCDUI

Page 20: LWUIT M3DD Tutorial 1.0 Important

20Copyright © 2009 Sun Microsystems, Inc. All rights reserved.

Canvas• Do your own drawing• Lines, rectangles, ellipses• Limited fonts• Limited image control• It is possible to roll your own UI

toolkit this way• Many application developers

take this route, but it’s tough

User Interface Choices: LCDUI

Page 21: LWUIT M3DD Tutorial 1.0 Important

21Copyright © 2009 Sun Microsystems, Inc. All rights reserved.

GameCanvas• Extensions to Canvas for 2D games• Streamlined animation pipeline

> Synchronous repainting> Key polling

• Layers• Sprites

> Animation> Collisions

User Interface Choices: LCDUI

Page 22: LWUIT M3DD Tutorial 1.0 Important

22

Ta-Dum!Here’s LWUIT!

22

Page 23: LWUIT M3DD Tutorial 1.0 Important

23Copyright © 2009 Sun Microsystems, Inc. All rights reserved.

Overview of LWUIT• LightWeight User Interface Toolkit• Looks great• Highly portable• Open source library

> GPL v2 with classpath exception> http://lwuit.dev.java.net/

• Best of Swing• Familiar APIs• Built on MIDP 2.0, atop Canvas

Ta-Dum!

Page 24: LWUIT M3DD Tutorial 1.0 Important

24Copyright © 2009 Sun Microsystems, Inc. All rights reserved.

Clinchers• Runs on many devices• Transitions• Layouts• Fonts• Themes• Touch support• Thriving community• Can’t beat the price

Ta-Dum!

LWUITDemo

Page 25: LWUIT M3DD Tutorial 1.0 Important

25

On-Device PortalThe original challenge

25

Page 26: LWUIT M3DD Tutorial 1.0 Important

26Copyright © 2009 Sun Microsystems, Inc. All rights reserved.

What is the ODP?• Think Facebook Apps, Konfabulator/Dashboard• Peers: Yahoo! Go, WidSets• ODP application contains mobile Java widgets,

something like mini-applications, representing services

• Superpowered idle screen> User-customized> Current from the network> Seamless installation> Interactive, links to full applications

On-Device Portal

Page 27: LWUIT M3DD Tutorial 1.0 Important

27Copyright © 2009 Sun Microsystems, Inc. All rights reserved.

FX W

idget

Feed Widget

Java Widget

Java ODP Client Overview

On-Device Portal

Page 28: LWUIT M3DD Tutorial 1.0 Important

28Copyright © 2009 Sun Microsystems, Inc. All rights reserved.

ODP Screen Shots

On-Device Portal

Page 29: LWUIT M3DD Tutorial 1.0 Important

29

LWUIT on WhateverTV, desktop, toasters...

29

Page 30: LWUIT M3DD Tutorial 1.0 Important

30Copyright © 2009 Sun Microsystems, Inc. All rights reserved.

ARIB B.23OCAP

ACAP

DVB-MHP

Digital TV Standards Worldwide

DVB-GEM GEM-IPTV

DTMSCDC,PBP

LWUIT on Whatever

Page 31: LWUIT M3DD Tutorial 1.0 Important

31Copyright © 2009 Sun Microsystems, Inc. All rights reserved.

CDC/PBP is Standard for TV

CDC,FP,PBP

CDC Market CoverageBlu-ray DTV/IPTV Cable

LWUIT on Whatever

Page 32: LWUIT M3DD Tutorial 1.0 Important

32Copyright © 2009 Sun Microsystems, Inc. All rights reserved.

LWUIT Everywhere• LWUIT’s platform interface is thin

LWUIT on Whatever

Page 33: LWUIT M3DD Tutorial 1.0 Important

33

Show Me the Code!A Quick Start with Form

33

Page 34: LWUIT M3DD Tutorial 1.0 Important

34Copyright © 2009 Sun Microsystems, Inc. All rights reserved.

Hello, LWUIT!• Initialize LWUITDisplay.init(this);

• Load a theme> Sets colors, images, fonts> More on this magic later

• Create a formForm f = new Form("Hello, LWUIT!");

• Show the formf.show();

HelloLWUIT

Show Me the Code!

Page 35: LWUIT M3DD Tutorial 1.0 Important

35

Working With Forms

35

Page 36: LWUIT M3DD Tutorial 1.0 Important

36Copyright © 2009 Sun Microsystems, Inc. All rights reserved.

Using Form• Create a formForm f = new Form("Labels");

• Add some stuffLabel label = new Label("Baldy");f.addComponent(label);

• Slap it up on the screenf.show();

Working with Forms

Page 37: LWUIT M3DD Tutorial 1.0 Important

37Copyright © 2009 Sun Microsystems, Inc. All rights reserved.

Layout Managers• Remember Swing?• Six supported layout managers

> BorderLayout> BoxLayout> FlowLayout> GridLayout> CoordinateLayout> GroupLayout

Working with Forms

Page 38: LWUIT M3DD Tutorial 1.0 Important

38Copyright © 2009 Sun Microsystems, Inc. All rights reserved.

Layout Manager Family Album

Working with Forms

Page 39: LWUIT M3DD Tutorial 1.0 Important

39Copyright © 2009 Sun Microsystems, Inc. All rights reserved.

Working With a Layout Manager• Create a form as usualForm f = new Form("Layouts");

• Set a layout managerf.setLayout(new BorderLayout());

• Add components with constraints//Label label = ...//Container buttonBar = ...f.addComponent( BorderLayout.CENTER, label);f.addComponent( BorderLayout.SOUTH, buttonBar); ComponentMIDlet

showLayout()

Working with Forms

Page 40: LWUIT M3DD Tutorial 1.0 Important

40

Component Tour

40

Page 41: LWUIT M3DD Tutorial 1.0 Important

41Copyright © 2009 Sun Microsystems, Inc. All rights reserved.

Partial Component HierarchyComponent

List Container TextArea Label

ComboBox Form TabbedPane Button

Dialog RadioButton CheckBox

Component Tour

Page 42: LWUIT M3DD Tutorial 1.0 Important

42Copyright © 2009 Sun Microsystems, Inc. All rights reserved.

Label• Text• Image• Text plus image• Horizontal alignment• Vertical alignment

Label label = new Label("Baldy");Image image = Image.createImage("/baldy.png");

Label picLabel = new Label(image);ComponentMIDlet

showLabels()

Component Tour

Page 43: LWUIT M3DD Tutorial 1.0 Important

43Copyright © 2009 Sun Microsystems, Inc. All rights reserved.

Button• Inherits from Label• Can receive focus• Use select key or pointer to

click it• Add an ActionListener

to find out when it’s clicked

ComponentMIDletshowButtons()showEvents()

Component Tour

Page 44: LWUIT M3DD Tutorial 1.0 Important

44Copyright © 2009 Sun Microsystems, Inc. All rights reserved.

RadioButton• Inherits from Button• Adds a boolean state• Not much good by itself• Use ButtonGroup

ComponentMIDletshowMoreButtons()

Component Tour

Page 45: LWUIT M3DD Tutorial 1.0 Important

45Copyright © 2009 Sun Microsystems, Inc. All rights reserved.

CheckBox• Inherits from Button• Adds a boolean state• Find out with isSelected()

ComponentMIDletshowMoreButtons()

Component Tour

Page 46: LWUIT M3DD Tutorial 1.0 Important

46Copyright © 2009 Sun Microsystems, Inc. All rights reserved.

TextArea• Specify rows and columns• Displays text• Constraints: URL, EMAILADDR, NUMERIC, etc.

• Editing is done in a platform-specific text box

• Multiline TextAreas can grow as needed.

ComponentMIDletshowText()

Component Tour

Page 47: LWUIT M3DD Tutorial 1.0 Important

47Copyright © 2009 Sun Microsystems, Inc. All rights reserved.

TextField• Subclass of TextArea• Single line• In situ editing• Native editing is also available

ComponentMIDletshowText()

Component Tour

Page 48: LWUIT M3DD Tutorial 1.0 Important

48

Lists

48

Page 49: LWUIT M3DD Tutorial 1.0 Important

49Copyright © 2009 Sun Microsystems, Inc. All rights reserved.

Lists• Crucial for mobile applications• MVC separation model• Plenty of useful modes

> FIXED_NONE> FIXED_NONE_CYCLIC> FIXED_NONE_ONE_ELEMENT_MARGIN_FROM_EDGE> FIXED_LEAD> FIXED_TRAIL> FIXED_CENTER

Lists

Page 50: LWUIT M3DD Tutorial 1.0 Important

50Copyright © 2009 Sun Microsystems, Inc. All rights reserved.

Using List• Create a listList list = new List();

• Add items to the default modellist.addItem("Item1");list.addItem("Item2");list.addItem("Item3");

ListDemolist1()

Lists

Page 51: LWUIT M3DD Tutorial 1.0 Important

51Copyright © 2009 Sun Microsystems, Inc. All rights reserved.

ListModel• Encapsulates list data• Fires data events to the view• Allows the list to show unlimited size of data• List has a default model, DefaultModel• Create your own by implementing ListModel• Then set the list’s model

list.setModel(new RMSListModel());

ListDemolist5()

Lists

Page 52: LWUIT M3DD Tutorial 1.0 Important

52Copyright © 2009 Sun Microsystems, Inc. All rights reserved.

List Cell Rendering• A cell renderer displays data from the model• Rubber stamp• Animating focus

public interface ListCellRenderer {

public Component getListCellRendererComponent(List list, Object value, int index, boolean isSelected);

public Component getListFocusComponent(List list);

} ListDemolist4()

Lists

Page 53: LWUIT M3DD Tutorial 1.0 Important

53Copyright © 2009 Sun Microsystems, Inc. All rights reserved.

List Events• Action Events – when user clicks select on the listlist.addActionListener(al);

• Selection Events – when user navigate list itemslist.addSelectionListener(sl);

• Data Events – from the list modellist.getModel().addDataChangedListener();

ListDemolist6()

Lists

Page 54: LWUIT M3DD Tutorial 1.0 Important

54Copyright © 2009 Sun Microsystems, Inc. All rights reserved.

ComboBox• Yes, it is a kind of list

> Has a model> Can use a custom cell renderer

• Shows current selection• Pops up list of choices

ComponentDemoshowListAndComboBox()

Lists

Page 55: LWUIT M3DD Tutorial 1.0 Important

55

Working with Threads

55

Page 56: LWUIT M3DD Tutorial 1.0 Important

56Copyright © 2009 Sun Microsystems, Inc. All rights reserved.

About the Event Dispatch Thread• LWUIT uses the Event Dispatch Thread (EDT) for

its work:> Painting components> Delivering events> Animation

• Avoids multithreading trouble• You must use the EDT too• You must use the EDT correctly

Working with Threads

Page 57: LWUIT M3DD Tutorial 1.0 Important

57Copyright © 2009 Sun Microsystems, Inc. All rights reserved.

Using the EDT• For component updates, you must also use the EDT

> Same principle: avoid multiple threads working with the same stuff at the same time

> Just like Swing• Useful methods in Display

public void callSerially(Runnable r)public void callSeriallyAndWait(Runnable r)

• The EDT is not your thread> Don’t hog it> Do long stuff in your own thread

Working with Threads

Page 58: LWUIT M3DD Tutorial 1.0 Important

58Copyright © 2009 Sun Microsystems, Inc. All rights reserved.

Typical Usage• In your ActionListener implementation:

public void actionPerformed(ActionEvent ae) { Runnable r = new LongOperationRunnable(); Thread t = new Thread(r); t.start();}

Working with Threads

Page 59: LWUIT M3DD Tutorial 1.0 Important

59

Dialogs

59

Page 60: LWUIT M3DD Tutorial 1.0 Important

60Copyright © 2009 Sun Microsystems, Inc. All rights reserved.

Dialog• A Dialog is a Form that occupies a part of the

screen as a top component.• Dialog can be modal/modeless• Use show() or dispose() methods• Two ways to create:

> Static factory methodsDialog.show(.....);

> Create a new objectDialog dialog = new Dialog();dialog.show();

Dialogs

Page 61: LWUIT M3DD Tutorial 1.0 Important

61Copyright © 2009 Sun Microsystems, Inc. All rights reserved.

More about Dialogs• Dialog type indicates an associated sound and icon• Types:

> ALARM> CONFIRMATION> ERROR> INFO> WARNING

DialogDemo

Dialogs

Page 62: LWUIT M3DD Tutorial 1.0 Important

62

Working with Containers

62

Page 63: LWUIT M3DD Tutorial 1.0 Important

63Copyright © 2009 Sun Microsystems, Inc. All rights reserved.

Nesting Containers• Container is a Component • Container.addComponent(...) accept Components• Each Container has its own layout

DialogDemonesting()

Working with Containers

Page 64: LWUIT M3DD Tutorial 1.0 Important

64Copyright © 2009 Sun Microsystems, Inc. All rights reserved.

TabbedPane• Each contained component has a

labeled tab• Also can use images for tabs• Put tabs on any side: top, bottom,

left, right• Pass a component to addTab()

and specify tab label or image

ComponentDemoshowTabbedPane()

Working with Containers

Page 65: LWUIT M3DD Tutorial 1.0 Important

65

Styles, Themes,and ResourcesOh My!

65

Page 66: LWUIT M3DD Tutorial 1.0 Important

66Copyright © 2009 Sun Microsystems, Inc. All rights reserved.

Style• Component attributes

> Colors - – Normal: bgColor, fgColor– Focus: bgSelectionColor, fgSelectionColor

> Font> Transparency> Image> Margin, Padding> Border> Painter

• Store defaults in a .res file (a theme)ComponentMIDlet

showStyle()

Styles, Themes, and Resources

Page 67: LWUIT M3DD Tutorial 1.0 Important

67Copyright © 2009 Sun Microsystems, Inc. All rights reserved.

Themes• Collection of style attributes• Declared in .res file• Create using:

> Resource Editor> Ant task

• Can be replaced at runtimeResources r = Resources.open("/javaTheme.res");UIManager.getInstance().setThemeProps( r.getTheme("javaTheme"));

Styles, Themes, and Resources

Page 68: LWUIT M3DD Tutorial 1.0 Important

68Copyright © 2009 Sun Microsystems, Inc. All rights reserved.

Styles, Themes, and Resources

Page 69: LWUIT M3DD Tutorial 1.0 Important

69Copyright © 2009 Sun Microsystems, Inc. All rights reserved.

Resources• LWUIT Binary file

> Themes> Fonts> Images> Animations> Localization

• Create> Ant> Resource Editor

Styles, Themes, and Resources

Page 70: LWUIT M3DD Tutorial 1.0 Important

70Copyright © 2009 Sun Microsystems, Inc. All rights reserved.

Resource Editor

Resource Editor

Styles, Themes, and Resources

Page 71: LWUIT M3DD Tutorial 1.0 Important

71Copyright © 2009 Sun Microsystems, Inc. All rights reserved.

Using Painters• Insert paint code to any Component• No need to override paint()• Reusable (green!)• Painters chain• Inspired by swingx project

public interface Painter { public void paint(Graphics g, Rectangle rect);}

Style.setBgPainter(Painter bgPainter);ComponentMIDlet

showPainter()

Styles, Themes, and Resources

Page 72: LWUIT M3DD Tutorial 1.0 Important

72

Animation and TransitionsSwoosh!

72

Page 73: LWUIT M3DD Tutorial 1.0 Important

73Copyright © 2009 Sun Microsystems, Inc. All rights reserved.

Animation• Simple interface

> animate() - if returns 'true', paint will be called> paint()

• Executed on the EDT• Activate

> form.registerAnimated(AnimatedComponent);

• De-activate> form.deregisterAnimated(AnimatedComponent);

Animation and Transitions

Page 74: LWUIT M3DD Tutorial 1.0 Important

74Copyright © 2009 Sun Microsystems, Inc. All rights reserved.

Transitions• Ready-made transitions

> Slide, Fade> 3D! Cube , Rotate, Fly-in

• Extendable• Applicable for

> Forms> Dialogs> Menu> Components ComponentMIDlet

showPainter()

Animation and Transitions

Page 75: LWUIT M3DD Tutorial 1.0 Important

75Copyright © 2009 Sun Microsystems, Inc. All rights reserved.

Working with Transitions• Forms and Dialogs

> setTransitionInAnimator(Transition t)> setTransitionOutAnimator(Transition t)

• Form menus> setMenuTransitions(Transition tIn, Transition tOut)

• Components in a container> replace(Component current, Component next, final Transition t)

> replaceAndWait(...)

DialogDemodialog3()

Animation and Transitions

Page 76: LWUIT M3DD Tutorial 1.0 Important

76

Stuff That Just Wouldn’t Fit

76

Page 77: LWUIT M3DD Tutorial 1.0 Important

77Copyright © 2009 Sun Microsystems, Inc. All rights reserved.

Mobile 3D Graphics (M3G, JSR 184)• M3G class is a bridge to JSR 184• Use renderM3G() to invoke your callback to

render 3D graphics• See LWUIT’s own Transition3D for an example

Stuff That Just Wouldn’t Fit

Page 78: LWUIT M3DD Tutorial 1.0 Important

78Copyright © 2009 Sun Microsystems, Inc. All rights reserved.

Scalable Vector Graphics (JSR 226)• Bridge between SVG Tiny content and LWUIT• Create LWUIT Image from URL or InputStream• Access to document model• Convenience methods for scaling and rotating

Stuff That Just Wouldn’t Fit

Page 79: LWUIT M3DD Tutorial 1.0 Important

79Copyright © 2009 Sun Microsystems, Inc. All rights reserved.

Creating Your Own Components• Define familiar behavior

> Paint> Sizing> Key and pointer events> Focus

• See Developer’s Guide, Chapter 13

Stuff That Just Wouldn’t Fit

Page 80: LWUIT M3DD Tutorial 1.0 Important

80Copyright © 2009 Sun Microsystems, Inc. All rights reserved.

Look and Feel• Pluggable Look and Feel (PlaF)• In case styles, painters, and themes weren’t good

enough for you• com.sun.lwuit.plaf

Stuff That Just Wouldn’t Fit

Page 81: LWUIT M3DD Tutorial 1.0 Important

81

The EndThank [email protected]@[email protected]

81