Part IV: chapter 2 - PowerPoint PPT Presentation

1 / 27
About This Presentation
Title:

Part IV: chapter 2

Description:

super('Super-cool-game'); this.game = game; StringItem item = new StringItem( 'Welcome to Super-cool-game!!!', null); append(item); Items ... – PowerPoint PPT presentation

Number of Views:36
Avg rating:3.0/5.0
Slides: 28
Provided by: Ran79
Category:

less

Transcript and Presenter's Notes

Title: Part IV: chapter 2


1
Part IV chapter 2
  • MIDP Classes
  • Screen
  • Basic UI
  • Graphics
  • Java2ME plugin for Eclipse IDE
  • http//eclipseme.org/

2
MIDP Classes
  • User Interface
  • javax.microedition.lcdui
  • javax.microedition.lcdui.game
  • Persistence Package
  • javax.microedition.rms
  • Application Lifecycle Package
  • javax.microedition.midlet

3
MIDP Classes
  • Networking Package
  • javax.microedition.io
  • Audio Package
  • javax.microedition.media
  • javax.microedition.media.control
  • Public Key Package
  • javax.microedition.pki
  • Core Packages
  • java.io
  • java.lang
  • java.util

4
The Screen Class
  • The javax.microedition.lcdui.Screen class is a
    superclass of all GUI components that can be put
    on a screen. Such components can also contain
    other components.
  • The Screen class itself has methods to set and
    get the values of the title bar and ticker text.
    It usually has automatic component positioning
    and scolling mechanisms, so developers don't need
    to bother with such things.

5
Text Boxes
  • javax.microedition.lcdui.TextBox class is a
    Screen class successor that allows the user to
    enter and edit text. It has a maximum size, which
    is the maximum number of characters that can be
    stored in the object at any time (its capacity).
  • The maximum size is the maximum stored capacity,
    and is unrelated to the number of characters that
    can be displayed at any given time.

6
Text Boxes
  • public class EditScreen extends TextBox
    implements CommandListener
  • private Command exitCommand
  • public EditScreen(String string)
  • super("Type smth.", string, 160, 0)
  • exitCommand new Command("Quit",
    Command.EXIT, 1)
  • addCommand(exitCommand)
  • setCommandListener(this)
  • public void commandAction(Command c,
    Displayable d)
  • if (c exitCommand)
  • midlet.quitMidlet()

7
Forms
  • The javax.microedition.lcdui.Form class is one of
    the most commonly used child classes of the
    Screen class. It is the only one that is a
    container in which developers can place different
    GUI objects.
  • Items that are placed in forms are descended from
    the javax.microedition.lcdui.Item class.
  • The Form class is similar to the java.awt.Panel
    class in J2SE.

8
Forms
  • The javax.microedition.lcdui.Form class is the
    most commonly used child of Screen. It might
    contain an arbitrary mixture of items, such as
    images, text fields, date fields, gauges, and
    choice groups. In general, any subclass of the
    Item class can be contained within a Form.
  • Every mobile device will handle the layout,
    traversal, and scrolling of a form in a slightly
    different way.

9
Forms
  • The items contained in a Form are referred to by
    their indices, which are consecutive integers
    starting from zero. These items can be edited
    using the append(), delete(), insert(), and set()
    methods.

10
Forms
  • New forms must extend the Form class. The form's
    constructor must call the parent's constructor,
    which accepts the title as a parameter. To be
    able to access the MIDlet's methods, a global
    variable pointing to the main Game class is
    provided.
  • javax.microedition.lcdui.StringItem is a user
    interface component to present different
    read-only text labels.
  • Example creates a start-up form and sets it as
    the current Displayable.

11
Creating and Calling a Form
  • import javax.microedition.MIDlet.
  • import javax.microedition.lcdui.
  • public class Game extends MIDlet
  • private Display display
  • public void startApp()
  • display Display.getDisplay(this)
  • StartForm form new StartForm(this)
  • display.setCurrent(form)
  • public void pauseApp()
  • public void destroyApp(boolean b)
  • public Display getDisplay()
  • return display
  • public void exit()

12
Creating and Calling a Form
  • import javax.microedition.lcdui.
  • public class StartForm extends Form
  • private Game game
  • public StartForm(Game game)
  • super("Super-cool-game")
  • this.game game
  • StringItem item new StringItem(
  • "Welcome to Super-cool-game!!!", null)
  • append(item)

13
Items
  • The javax.microedition.lcdui.Item class is the
    parent of them all. Any user interface component
    that can be added onto a Form must derive from
    Item. All Item objects have a label field, which
    is a string that is attached to the item. The
    label is typically displayed near the component
    when it is displayed within a screen and has the
    following methods
  • setLabel(String label) Sets the label of this
    object.
  • getLabel() Gets the label of this object.

14
Item State Listening
  • Sometimes it is useful to know if a user is
    selecting one of your items. You can have a form
    listen to a item by using the following static
    method
  • Form.setItemStateListener(ItemStateListener)
  • When a user does something interesting with a
    user interface component, the listener's
    itemStateChanged(Item item) method will be
    triggered. Such changes include
  • Changing the set of selected values in a
    ChoiceGroup
  • Adjusting the value of an interactive Gauge
  • Entering a new value into a TextField
  • Entering a new Date into a DateField
  • The listener is not called if the application (as
    opposed to the user) changes the value of an
    interactive item.

15
Menus and Commands
  • Because the MIDP user interface is highly
    abstract, it does not dictate any concrete
    interaction techniques, such as soft buttons or
    menus. Even low-level interactions such as
    scrolling are hidden to the application.
  • MIDP defines the javax.microedition.lcdui.Command
    class. Whenever a user hits a button, menu, dial,
    touch-screen, or other supported input element, a
    particular command will be triggered.

16
Menus and Commands
  • Because every Screen's subclass implements the
    Displayable interface, every screen has access to
    a slew of command methods
  • addCommand(Command cmd) Adds a command to the
    Displayable.
  • removeCommand(Command cmd) Removes a command
    from the Displayable.
  • setCommandListener(CommandListener listener)
    Sets a listener for Commands to this Displayable,
    replacing any previous CommandListener. A null
    reference is allowed, and has the effect of
    removing any existing listener.
  • Command exitCommand new Command("Exit",
    Command.EXIT, 0)

17
Menus and Commands
  • A command's type specifies the intent of the
    command. For example, if the application
    specifies BACK (to go back to the previously
    shown screen), then some devices that the command
    is of type will automatically associate all back
    commands with a given soft key.
  • BACK Returns the user to the previous screen.
  • OK A command that is a standard positive answer
    to a dialog.
  • CANCEL A command that is a standard negative
    answer to a dialog.
  • EXIT A command used for exiting from the
    application.

18
Menus and Commands
  • HELP A request for online help.
  • ITEM A command type relevant to a particular
    user interface item on the screen.
  • SCREEN An application-defined command that
    somehow pertains to the current screen.
  • STOP A command that will stop some currently
    running process or operation.

19
Menus and Commands
  • The javax.microedition.lcdui.CommandListener
    interface is used by applications to receive
    high-level events that are invoked by commands.
    The application must provide an implementation of
    a listener and must provide an instance of it on
    a screen in order to receive high-level events on
    that screen.
  • public class CanvasDemoMidlet extends MIDlet
    implements CommandListener

20
The Canvas Class
  • Canvas handles low-level events and allows you to
    draw directly onto the device's display. The
    Canvas class, just like the Screen class,
    implements the javax.microedition.lcdui.Displayabl
    e interface.
  • Just like a Form or any other type of screen, you
    can easily create and display multiple Canvases
    using the setDisplay() method.

21
The Canvas Class
  • Canvases require applications to subclass them
    because the paint() method is declared paint()
    method is where you actually put the pedal to the
    metal and draw things abstract.
  • Canvas class has the following methods
  • getWidth()
  • getHeight()
  • isDoubleBuffered()
  • getKeyCode(int gameAction) Gets a key code that
    corresponds to the specified game action on the
    device.
  • getKeyName(int keyCode) Gets an informative key
    string for a key.
  • getGameAction(int keyCode) Gets the game action
    associated with the given key code of the device.

22
The Canvas Class
  • keyPressed(int keyCode) Called when a key is
    first pressed.
  • keyRepeated(int keyCode) Called when a key is
    repeated (held down).
  • keyReleased(int keyCode) Called when a key is
    released.
  • repaint(int x, int y, int width, int height)
    Requests a repaint for the specified region of
    the screen.
  • repaint() Requests a repaint for the entire
    canvas.
  • showNotify() Called immediately prior to this
    canvas being made visible on the display. If you
    need to do something to the canvas right before
    it is shown, do it here.
  • hideNotify() Called shortly after the canvas has
    been removed from the display. This is a good
    place to clean up any objects and free up some
    extra memory.
  • paint(Graphics g) Renders the canvas. The
    application must implement this method in order
    to paint any graphics.
  • etc. (for others look in the specification)

23
Canvas Events
  • Canvas makes it easy to handle game actions, key
    presses, and pointer manipulations. Special
    methods are also provided to let you know what
    the current device can handle.
  • Every key is assigned a special key code. The key
    code values are unique for each hardware key.
  • The following key codes are defined
  • KEY_NUM0 to KEY_NUM9 for numeric 0 through 9 keys
  • KEY_STAR for the key
  • KEY_POUND for key
  • Most games use arrow keys and fire keys. The MID
    Profile defines the following game actions
  • UP, DOWN, LEFT, and RIGHT for navigation keys.
    Almost every phone now features these cursor
    keys.
  • GAME_A, GAME_B, GAME_C, and GAME_D for special
    keys. This usually corresponds to the soft keys
    immediately beneath the screen.

24
Painting on the Screen
  • The javax.microedition.lcdui.Graphics class is an
    abstract class that represents the actual
    device's display.
  • The Graphics class provides simple 2D geometric
    rendering capabilities. You can easily use
    graphics to draw primitive lines, rectangles, and
    arcs.
  • The Graphics class can be rendered directly to
    the display or to an offscreen image buffer,
    depending on the device. A graphics object for
    rendering to the display is passed to the Canvas
    paint() method. This is the only means by which a
    graphics object destined for the display can be
    obtained.

25
The Image Class
  • The javax.microedition.lcdui.Image class holds
    graphical image data.
  • Images exist only in offscreen memory, and will
    not be painted on the display unless an explicit
    command is paint() method of a Canvas) or when an
    Image issued by the application (such as within
    the object is placed within a form screen or an
    alert screen.

26
The Image Class
  • Images are either mutable or immutable, depending
    upon how they are created.
  • Immutable images are generally created by loading
    image data (usually as a Portable Network
    Graphics, or PNG file) from resource bundles,
    from files, or from the network. They may not be
    modified once they are created.

27
The Image Class
  • Mutable images are created in offscreen memory.
    The application might paint into them after
    having created a Graphics object expressly for
    this purpose.
  • The Image class supports images stored in the PNG
    format, version 1.0.
Write a Comment
User Comments (0)
About PowerShow.com