7 Lwuit Sun Maxmu

Embed Size (px)

Citation preview

  • 8/9/2019 7 Lwuit Sun Maxmu

    1/73

    1

    LWUIT Tutorial

    ([email protected])Sun Microsystems, Inc.

    1

  • 8/9/2019 7 Lwuit Sun Maxmu

    2/73

  • 8/9/2019 7 Lwuit Sun Maxmu

    3/73

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

    Outline

    The Big Fly-In

    User Interface Choices

    Ta-Dum!

    LWUIT on Whatever

    Show Me the Code!

    Working With Forms

    Component Tour

    Lists

    Dialogs

    Working with Containers

    Styles, Themes, Resources

    Animation and Transitions

    Stuff That Just Wouldnt Fit

  • 8/9/2019 7 Lwuit Sun Maxmu

    4/73

    4

    The Big Fly-In

    4

  • 8/9/2019 7 Lwuit Sun Maxmu

    5/73

    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

  • 8/9/2019 7 Lwuit Sun Maxmu

    6/73

    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

  • 8/9/2019 7 Lwuit Sun Maxmu

    7/737Copyright 2009 Sun Microsystems, Inc. All rights reserved.

    A VeryBrief 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

  • 8/9/2019 7 Lwuit Sun Maxmu

    8/738Copyright 2009 Sun Microsystems, Inc. All rights reserved.

    The Big Fly-in

    MSA 1.0 1.1

  • 8/9/2019 7 Lwuit Sun Maxmu

    9/739

    User Interface ChoicesBesides LWUIT

    9

  • 8/9/2019 7 Lwuit Sun Maxmu

    10/7310Copyright 2009 Sun Microsystems, Inc. All rights reserved.

    The Menu

    SVG

    3D Graphics

    OpenGL ES

    LCDUIand...

    LWUIT

    User Interface Choices: LCDUI

  • 8/9/2019 7 Lwuit Sun Maxmu

    11/7311Copyright 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 artistroles

    User Interface Choices: SVG

  • 8/9/2019 7 Lwuit Sun Maxmu

    12/7312Copyright 2009 Sun Microsystems, Inc. All rights reserved.

    JSR 226 SVG API

    Play animations

    Interactive

    Can also create elements at runtime

    User Interface Choices: SVG

  • 8/9/2019 7 Lwuit Sun Maxmu

    13/7313Copyright 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

  • 8/9/2019 7 Lwuit Sun Maxmu

    14/7314Copyright 2009 Sun Microsystems, Inc. All rights reserved.

    OpenGL ES

    JSR 239, notpart ofMSA 1.0

    OpenGL is a standard

    API for 2D and 3Dgraphics

    OpenGL ES is a subsetfor mobile devices

    Close to the metal

    User Interface Choices: OpenGL ES

  • 8/9/2019 7 Lwuit Sun Maxmu

    15/7315Copyright 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

  • 8/9/2019 7 Lwuit Sun Maxmu

    16/7316Copyright 2009 Sun Microsystems, Inc. All rights reserved.

    A Full Form

    Nokia Series 40

    Nokia Series 40 3rd ed.Sun Java Wireless Toolkit

    User Interface Choices: LCDUI

    U I f Ch i LCDUI

  • 8/9/2019 7 Lwuit Sun Maxmu

    17/7317Copyright 2009 Sun Microsystems, Inc. All rights reserved.

    Command Examples

    User Interface Choices: LCDUI

    U I t f Ch i LCDUI

  • 8/9/2019 7 Lwuit Sun Maxmu

    18/7318Copyright 2009 Sun Microsystems, Inc. All rights reserved.

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

    Nokia Series 40 Motorola RAZR

    Sun Java Wireless Toolkit

    User Interface Choices: LCDUI

    U I t f Ch i LCDUI

  • 8/9/2019 7 Lwuit Sun Maxmu

    19/7319Copyright 2009 Sun Microsystems, Inc. All rights reserved.

    Command Placement

    Sun toolkit RAZR Series 40 3rd edition

    User Interface Choices: LCDUI

    U I t f Ch i LCDUI

  • 8/9/2019 7 Lwuit Sun Maxmu

    20/73

    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 UItoolkit this way

    Many application developerstake this route, but its tough

    User Interface Choices: LCDUI

    U I t f Ch i LCDUI

  • 8/9/2019 7 Lwuit Sun Maxmu

    21/73

    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

  • 8/9/2019 7 Lwuit Sun Maxmu

    22/73

    22

    Ta-Dum!Heres LWUIT!

    22

    T D !

  • 8/9/2019 7 Lwuit Sun Maxmu

    23/73

    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!

    Ta Dum!

    http://lwuit.dev.java.net/http://lwuit.dev.java.net/
  • 8/9/2019 7 Lwuit Sun Maxmu

    24/73

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

    Clinchers

    Runs on many devices

    Transitions

    Layouts

    Fonts

    Themes

    Touch support

    Thriving community

    Cant beat the price

    Ta-Dum!

    LWUITDemo

  • 8/9/2019 7 Lwuit Sun Maxmu

    25/73

    25

    LWUIT on WhateverTV, desktop, toasters...

    25

  • 8/9/2019 7 Lwuit Sun Maxmu

    26/73

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

    ARIB B.23OCAP

    ACAP

    DVB-MHP

    Digital TV Standards Worldwide

    DVB-GEM GEM-IPTV

    DTMS

    CDC,PBP

  • 8/9/2019 7 Lwuit Sun Maxmu

    27/73

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

    CDC and PBP ties together the Java based TVstandards

    CDC Market CoverageBlu-ray Disc DTV/IPTV Cable DTV

    Emerging:

    IPTV,Brazil,

    China

    CDC,FP,PBP CDC,FP,PBP

  • 8/9/2019 7 Lwuit Sun Maxmu

    28/73

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

    LWUIT Everywhere

    LWUITs platform interface is thin

  • 8/9/2019 7 Lwuit Sun Maxmu

    29/73

    29

    Show Me the Code!A Quick Start with Form

    29

  • 8/9/2019 7 Lwuit Sun Maxmu

    30/73

    30Copyright 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

  • 8/9/2019 7 Lwuit Sun Maxmu

    31/73

    31

    Working With Forms

    31

  • 8/9/2019 7 Lwuit Sun Maxmu

    32/73

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

    Using Form

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

    Add some stuff

    Label label = new Label("Baldy");f.addComponent(label);

    Slap it up on the screenf.show();

  • 8/9/2019 7 Lwuit Sun Maxmu

    33/73

    33Copyright 2009 Sun Microsystems, Inc. All rights reserved.

    Layout Managers

    Remember Swing?

    Six supported layout managers> BorderLayout

    > BoxLayout> FlowLayout

    > GridLayout

    > CoordinateLayout> GroupLayout

  • 8/9/2019 7 Lwuit Sun Maxmu

    34/73

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

    Layout Manager Family Album

  • 8/9/2019 7 Lwuit Sun Maxmu

    35/73

    35Copyright 2009 Sun Microsystems, Inc. All rights reserved.

    Working With a Layout Manager

    Create a form as usualForm f = new Form("Layouts");

    Set a layout manager

    f.setLayout(new BorderLayout()); Add components with constraints//Label label = ...

    //Container buttonBar = ...

    f.addComponent(BorderLayout.CENTER, label);

    f.addComponent(

    BorderLayout.SOUTH, buttonBar);

  • 8/9/2019 7 Lwuit Sun Maxmu

    36/73

    36

    Component Tour

    36

  • 8/9/2019 7 Lwuit Sun Maxmu

    37/73

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

    Partial Component Hierarchy

    Component

    List Container TextArea Label

    ComboBox Form TabbedPane Button

    Dialog RadioButton CheckBox

  • 8/9/2019 7 Lwuit Sun Maxmu

    38/73

    38Copyright 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()

  • 8/9/2019 7 Lwuit Sun Maxmu

    39/73

    39Copyright 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 its clicked

    ComponentMIDlet

    showButtons()

    showEvents()

  • 8/9/2019 7 Lwuit Sun Maxmu

    40/73

    40Copyright 2009 Sun Microsystems, Inc. All rights reserved.

    RadioButton

    Inherits from Button

    Adds a boolean state

    Not much good by itself

    Use ButtonGroup

    ComponentMIDlet

    showMoreButtons()

  • 8/9/2019 7 Lwuit Sun Maxmu

    41/73

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

    CheckBox

    Inherits from Button

    Adds a boolean state

    Find out with isSelected()

    ComponentMIDlet

    showMoreButtons()

  • 8/9/2019 7 Lwuit Sun Maxmu

    42/73

    42Copyright 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 cangrow as needed.

    ComponentMIDlet

    showText()

  • 8/9/2019 7 Lwuit Sun Maxmu

    43/73

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

    TextField

    Subclass ofTextArea

    Single line

    In situediting

    Native editing is also available

    ComponentMIDlet

    showText()

  • 8/9/2019 7 Lwuit Sun Maxmu

    44/73

    44

    Lists

    44

  • 8/9/2019 7 Lwuit Sun Maxmu

    45/73

    45Copyright 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

  • 8/9/2019 7 Lwuit Sun Maxmu

    46/73

    46Copyright 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");

    ListDemo

    list1()

  • 8/9/2019 7 Lwuit Sun Maxmu

    47/73

    47Copyright 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 lists modellist.setModel(new RMSListModel());

    ListDemo

    list5()

  • 8/9/2019 7 Lwuit Sun Maxmu

    48/73

    48Copyright 2009 Sun Microsystems, Inc. All rights reserved.

    List Cell Rendering

    A cell rendererdisplays 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()

  • 8/9/2019 7 Lwuit Sun Maxmu

    49/73

    49Copyright 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();

    ListDemo

    list6()

  • 8/9/2019 7 Lwuit Sun Maxmu

    50/73

    50Copyright 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

    ComponentDemo

    showListAndComboBox()

  • 8/9/2019 7 Lwuit Sun Maxmu

    51/73

    51

    Dialogs

    51

  • 8/9/2019 7 Lwuit Sun Maxmu

    52/73

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

    Dialog

    A Dialog is a Form that occupies a part of thescreen as a top component.

    Dialog can be modal/modalless

    Use show() ordispose() methods Two ways to create:

    > Static factory methods

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

    dialog.show();

  • 8/9/2019 7 Lwuit Sun Maxmu

    53/73

    53Copyright 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

  • 8/9/2019 7 Lwuit Sun Maxmu

    54/73

    54

    Working with Containers

    54

  • 8/9/2019 7 Lwuit Sun Maxmu

    55/73

    55Copyright 2009 Sun Microsystems, Inc. All rights reserved.

    Nesting Containers

    Container is a Component

    Container.addComponent(...) accept Components

    Each Container has its own layout

    DialogDemo

    nesting()

  • 8/9/2019 7 Lwuit Sun Maxmu

    56/73

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

    TabbedPane

    Each contained component has alabeled 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

    ComponentDemo

    showTabbedPane()

  • 8/9/2019 7 Lwuit Sun Maxmu

    57/73

    57

    Styles, Themes,and ResourcesOh My!

    57

  • 8/9/2019 7 Lwuit Sun Maxmu

    58/73

    58Copyright 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()

  • 8/9/2019 7 Lwuit Sun Maxmu

    59/73

    59Copyright 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 runtime

    Resources r = Resources.open("/javaTheme.res");UIManager.getInstance().setThemeProps(

    r.getTheme("javaTheme"));

  • 8/9/2019 7 Lwuit Sun Maxmu

    60/73

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

  • 8/9/2019 7 Lwuit Sun Maxmu

    61/73

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

    Resources

    LWUIT Binary file> Themes

    > Fonts

    >Images> Animations

    > Localization

    Create

    > Ant> Resource Editor

  • 8/9/2019 7 Lwuit Sun Maxmu

    62/73

    62Copyright 2009 Sun Microsystems, Inc. All rights reserved.

    Resource Editor

    Resource Editor

  • 8/9/2019 7 Lwuit Sun Maxmu

    63/73

    63Copyright 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()

  • 8/9/2019 7 Lwuit Sun Maxmu

    64/73

    64

    Animation and TransitionsSwoosh!

    64

  • 8/9/2019 7 Lwuit Sun Maxmu

    65/73

    65Copyright 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);

  • 8/9/2019 7 Lwuit Sun Maxmu

    66/73

    66Copyright 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()

  • 8/9/2019 7 Lwuit Sun Maxmu

    67/73

    67Copyright 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(...)

    DialogDemo

    dialog3()

  • 8/9/2019 7 Lwuit Sun Maxmu

    68/73

    68

    Stuff That Just Wouldnt Fit

    68

  • 8/9/2019 7 Lwuit Sun Maxmu

    69/73

    69Copyright 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 LWUITs own Transition3D for an example

  • 8/9/2019 7 Lwuit Sun Maxmu

    70/73

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

    Scalable Vector Graphics (JSR 226)

    Bridge between SVG Tiny content and LWUIT Create LWUIT Image from URL orInputStream

    Access to document model

    Convenience methods for scaling and rotating

  • 8/9/2019 7 Lwuit Sun Maxmu

    71/73

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

    Creating Your Own Components

    Define familiar behavior> Paint

    > Sizing

    > Key and pointer events

    > Focus

    See Developers Guide, Chapter 13

  • 8/9/2019 7 Lwuit Sun Maxmu

    72/73

    72Copyright 2009 Sun Microsystems, Inc. All rights reserved.

    Look and Feel

    Pluggable Look and Feel (PlaF) In case styles, painters, and themes werent good

    enough for you

    com.sun.lwuit.plaf

  • 8/9/2019 7 Lwuit Sun Maxmu

    73/73

    The End

    Thank [email protected]