Part IV: Chapter 3 - PowerPoint PPT Presentation

1 / 22
About This Presentation
Title:

Part IV: Chapter 3

Description:

... standard output and standard error to their serial ports, so if you plug in a ... InputStream is = getClass().getResourceAsStream('music.wav' ... – PowerPoint PPT presentation

Number of Views:36
Avg rating:3.0/5.0
Slides: 23
Provided by: Ran79
Category:
Tags: chapter | part

less

Transcript and Presenter's Notes

Title: Part IV: Chapter 3


1
Part IV Chapter 3
  • Debugging
  • Alert
  • Connections
  • Resources
  • Game Graphics
  • Media

2
Debugging
  • While programming in WTK, you can use
    System.out.println() method for debugging your
    program.
  • In an emulator, System.out.println() usually
    outputs to the emulator console or log. In a real
    device, it may just get junked. However, some
    devices echo standard output and standard error
    to their serial ports, so if you plug in a
    terminal or terminal emulator, you might be able
    to see the output.

3
Alert
  • An alert dialog is a term for a particular type
    of dialog box that occurs in a graphical user
    interface. It is also known as an alert box,
    alert window, or alert popup.
  • Alerts were designed for two main uses. The first
    is to inform the user than an operation could not
    continue or complete due to some insurmountable
    error. The second is to warn them that the
    current course of action could be in some way
    dangerous or detrimental, and to offer the option
    of not proceeding.

4
Alert
  • private Alert alert
  • ...
  • alert new Alert("Busy", "Please try again.",
    null, AlertType.WARNING)
  • alert.setTimeout(Alert.FOREVER)
  • display.setCurrent(alert, form)

5
Socket Connection
  • Sockets are used for sending and receiving
    information over the network (WAP, OTA, etc.)
  • The creation of Connections is performed
    dynamically by looking up a protocol
    implementation class whose name is formed from
    the platform name (read from a system property)
    and the protocol name of the requested
    connection(extracted from the parameter string
    supplied by the application programmer.)
  • The parameter string that describes the target
    should conform to the URL format as described in
    RFC 2396.
  • This takes the general form
  • schemetargetparms
  • where scheme is the name of a protocol such as
    http.
  • The target is normally some kind of network
    address.
  • Any parms are formed as a series of equates of
    the form xy. Example typea

6
Socket Connection example
  • InputStream is
  • OutputStream os
  • SocketConnection sc
  • Sender sender
  • ...
  • try
  • sc (SocketConnection) Connector.open("socket//l
    ocalhost5000")
  • si.setText("Connected to server")
  • is sc.openInputStream()
  • os sc.openOutputStream()
  • while (true) // Loop forever, receiving data
  • StringBuffer sb new StringBuffer()
  • int c 0
  • while (((c is.read()) ! '\n') (c ! -1))
  • sb.append((char) c)
  • if (c -1) break
  • catch (ConnectionNotFoundException cnfe)

7
Server Socket
  • SocketConnection sc
  • ServerSocketConnection scn
  • ...
  • scn (ServerSocketConnection) Connector.open("soc
    ket//5000")
  • // Wait for a connection.
  • ...
  • sc (SocketConnection) scn.acceptAndOpen()

8
HTTPConnection
  • Using HHTP connection instead of a stream socket.
    This code looks very similar to the socket code,
    but there is a striking difference. The socket
    code first opened an OutputStream and sent the
    request to the web server before trying to read
    the response.
  • HttpConnection conn
  • InputStream is
  • ...
  • String url "http//java.sun.com/"
  • conn (HttpConnection)Connector.open(url,
    Connector.READ_WRITE)
  • if (conn.getResponseCode( ) HttpConnection.HTTP
    _OK)
  • is conn.openInputStream( )
  • final int MAX_LENGTH 128
  • byte buf new byteMAX_LENGTH
  • int total 0
  • while (total lt MAX_LENGTH)
  • int count is.read(buf, total, MAX_LENGTH -
    total)
  • if (count lt 0) break
  • total count
  • is.close( )

9
Image from HTTP
  • You can also use HTTPConnection for downloading
    images to the device
  • HttpConnection conn
  • InputStream is
  • ...
  • ...
  • is conn.openInputStream()
  • Image NewImage Image.createImage(is)

10
Resources
  • Application resource files are accessed
    usinggetResourceAsStream(String name) in
    java.lang.Class.
  • In the MIDP specification, getResourceAsStream is
    used to allow resource files to be retrieved from
    the MIDlet Suites JAR file.
  • Resource names refer to the contents of the
    MIDlet suite JAR file. Absolute pathnames,
    beginning with / are fully qualified file names
    within the jar file.

11
Resources
  • InputStream is null
  • try
  • is getClass().getResourceAsStream(
  • "/level1.dat")
  • if (is ! null)
  • while ((c is.read()) ! -1)
  • ... // Process input
  • is.close()
  • catch (java.io.IOException ex)
  • ...

12
GameCanvas
  • The GameCanvas class provides the basis for a
    game user interface. In addition to the features
    inherited from Canvas (commands, input
    events,etc.) it also provides game-specific
    capabilities such as an off-screen graphics
    buffer and the ability to query key status.
  • A dedicated buffer is created for each GameCanvas
    instance. Since a unique buffer is provided for
    each GameCanvas instance, it is preferable to
    re-use a single GameCanvas instance in the
    interests of minimizing heap usage.

13
Layer
  • A Layer is an abstract class representing a
    visual element of a game. Each Layer has position
    (in terms of the upper-left corner of its visual
    bounds), width, height, and can be made visible
    or invisible. Layer subclasses must implement a
    paint(Graphics) method so that they can be
    rendered.
  • The Layers (x,y) position is always interpreted
    relative to the coordinate system of the Graphics
    object that is passed to the Layers paint()
    method.

14
Sprite
  • A Sprite is a basic visual element that can be
    rendered with one of several frames stored in an
    Image.
  • Different frames can be shown to animate the
    Sprite.
  • Several transforms such as flipping and rotation
    can also be applied to a Sprite to further vary
    its appearance. As with all Layer subclasses, a
    Sprites location can be changed and it can also
    be made visible or invisible.

15
Sprite Frames
  • The raw frames used to render a Sprite are
    provided in a single Image object, which may be
    mutable or immutable.
  • If more than one frame is used, the Image is
    broken up into a series of equally-sized frames
    of a specified width and height.
  • The same set of frames may be stored in several
    different arrangements.
  • The developer must manually switch the current
    frame in the frame sequence. This may be
    accomplished by calling setFrame(int),
    prevFrame(), or nextFrame() methods.
  • private Sprite createBody() throws IOException
  • Image image Image.createImage("/body.png")
  • return new Sprite(image, 27, 32)

16
LayerManager
  • The LayerManager manages a series of Layers. The
    LayerManager simplifies the process of rendering
    the Layers that have been added to it by
    automatically rendering the correct regions of
    each Layer in thea ppropriate order.
  • The Layer Manager maintains an ordered list to
    which Layers can be appended, inserted and
    removed.A Layers index correlates to its
    z-order the layer at index 0 is closest to the
    user while the Layer with the highest index is
    furthest away from the user.

17
LayerManager
  • The indices are always contiguous that is, if a
    Layer is removed, the indices of subsequent
    Layers will be adjusted to maintain continuity.
  • private LayerManager mLayerManager
  • ...
  • mLayerManager.append(Sprite1)
  • mLayerManager.append(TiledLayer)
  • mLayerManager.setViewWindow(0, 0, screenWidth,
    screenHeight)
  • ...
  • mLayerManager.paint(g, 0, 0)

18
TiledLayer
  • The TiledLayer is a little like Sprite but it is
    mainly meant for backgrounds, roads or other
    larger areas. TiledLayer consists of a grid of
    cells, which can be filled with images or
    tiles.This can be used by game developers for
    developing tiled layers in their aplications.
  • A TiledLayer can be rendered by manually calling
    its paint method it can also be rendered
    automatically using a LayerManager object.

19
TiledLayer
  • TiledLayer tiledLayer
  • Image bg
  • int tileData8,8,8,9,9,8,8,8,8,8,8,14,8,8,8,8
    ,10,10,10,
  • 10,10,8,8,14,8,8,10,10,10,10,10,8,8,8,8,10,9,10,9,
    10,
  • 8,8,14,8,8,8,8,14,8,8,8,8,8,8,8,8,10,8,10,9,9,
  • 8,8,9,9,8,8,9,11,11
  • ...
  • try
  • bgImage.createImage("/bg.png")
  • catch(Exception e)
  • tiledLayernew TiledLayer(tileData0.length,
  • tileData.length, bg, 30, 30)
  • //Set the tile data
  • for(int i0iltset.lengthi)
  • for(int j0jlttileData0.lengthj)
  • tiledLayer.setCell(j,i, setij)

20
Media Playback
  • javax.microedition.media package is introduced in
    MIDP2.0 profile.
  • This package supports sound and upward
    compatibility with MultiMedia API.It has Manager,
    Player and Control as main objects.
  • Different audio files, from various locations can
    be played on mobile.

21
Simple Media Playback
  • try
  • Player p Manager.createPlayer(
  • "http//webserver/music.wav")
  • p.start()
  • catch (IOException ioe)
  • catch (MediaException me)

22
Playing Back from Media Stored in JAR
  • try
  • InputStream is getClass().getResourceAsStream("m
    usic.wav")
  • Player p Manager.createPlayer(is,
    "audio/X-wav")
  • p.start()
  • catch (IOException ioe)
  • catch (MediaException me)
Write a Comment
User Comments (0)
About PowerShow.com