Java
  Home arrow Java arrow Page 8 - Storing and Retrieving Data
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVA

Storing and Retrieving Data
By: Apress Publishing
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 14
    2005-05-12

    Table of Contents:
  • Storing and Retrieving Data
  • Serializing More Complex Data Using Streams
  • Using Data Types and Byte Arithmetic
  • Applying Data Storage to a Game
  • Converting an array of bytes into a dungeon
  • Creating the Complete Example Game
  • DungeonManager.java
  • Doors and keys

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Storing and Retrieving Data - Doors and keys


    (Page 8 of 8 )

    The princess and the crown Sprites were too simple to warrant making whole subclasses for them (similarly I didn’t bother to subclass TiledLayer for the background this time). But for the doors and keys, I wanted to store their colors in the Sprite object itself, so I created a subclass, DoorKey.java (see Listing 5-10).

    Listing 5-10. DoorKey.java

      package net.frog_parrot.dungeon;
     
    import javax.microedition.lcdui.*;
      import javax.microedition.lcdui.game.*;
     
    /**
       
    * This class represents doors and keys.
        *
       
    * @author Carol Hamer
       
    */
      public class DoorKey extends Sprite {
     
    //-------------------------------------------------------   // fields
     
    /**
       
    * The image file shared by all doors and keys.
       
    */
      public static Image myImage;
     
    /**
       
    * A code int that indicates the door or key's color.
       
    */
      private int myColor;
     
    //-------------------------------------------------------  // get/set data
     
    /**
       
    * @return the door or key's color.
        */
      public int getColor() {
       
    return(myColor);
      }
     
    //-------------------------------------------------------  // constructor and initializer
      static {
        try {
          myImage = Image.createImage("/images/keys.png");
        } catch(Exception e) {
          throw(new RuntimeException(
              "DoorKey.-->failed to load image, caught "
              + e.getClass() + ": " + e.getMessage()));
        }
      }
     
    /**
       
    * Standard constructor sets the image to the correct frame
       
    * (according to whether this is a door or a key and what
       
    * color it should be) and then puts it in the correct location.
        */
     
    public DoorKey(int color, boolean isKey, int[] gridCoordinates) {
        super(myImage, DungeonManager.SQUARE_WIDTH, DungeonManager.SQUARE_WIDTH);
        myColor = color;
        int imageIndex = color * 2;
        if(isKey) {
         
    imageIndex++;
        }
        setFrame(imageIndex);
        setPosition(gridCoordinates[0] * DungeonManager.SQUARE_WIDTH,
           gridCoordinates[1] * DungeonManager.SQUARE_WIDTH);
      }

    }

    And, of course, you don’t want to forget about the Thread subclass GameThread.java (see Listing 5-11).

    Listing 5-11. GameThread.java

      package net.frog_parrot.dungeon;
     
    /**
       
    * This class contains the loop that keeps the game running.
        *
       
    * @author Carol Hamer
       
    */
      public class GameThread extends Thread {
     
    //-------------------------------------------------------  // fields
     
    /**
       
    * Whether the main thread would like this thread
       
    * to pause.
       
    */
      private boolean myShouldPause;
     
    /**
       
    * Whether the main thread would like this thread
       
    * to stop.
       
    */
      private static boolean myShouldStop;
     
    /**
       
    * Ahandle back to the graphical components.
       
    */
      private DungeonCanvas myDungeonCanvas;
      //-------------------------------------------------------  // initialization
     
    /**
       
    * standard constructor.
        */
      GameThread(DungeonCanvas canvas) {
       
    myDungeonCanvas = canvas;
      }
      //-------------------------------------------------------  // actions
     
    /**
       
    * pause the game.
        */
      void pause() {
       
    myShouldPause = true;
      }
     
    /**
       
    * restart the game after apause.
        */
     
    synchronized void resumeGame() {
        myShouldPause = false;
        notify();
      }
      /**
       
    * stops the game.
        */
      synchronized void requestStop() {
      myShouldStop = true;
      this.notify();
     
    }
     
    /**
       
    * start the game..
        */
     
    public void run() {
        // flush any keystrokes that occurred before the
        // game started:
        myDungeonCanvas.flushKeys();
        myShouldStop = false;
        myShouldPause = false;
        while(true) {
         
    if(myShouldStop) {
           
    break;
          }
          myDungeonCanvas.checkKeys();
          myDungeonCanvas.updateScreen();
          // you do a very short pause to allow the other thread
          // to update the information about which keys are pressed:
          synchronized(this) {
           
    try {
              wait(1);
           
    } catch(Exception e) {}
          }
          if(myShouldPause) {
           
    synchronized(this) {
              try {
                wait();
              } catch(Exception e) {}
             }
            }
           }
          }

       }

    Storing your data locally on the device isn’t the only application of transforming your object data into byte arrays (and later converting it back). You can use the same functions that prepare your data to be stored to prepare your data to be transmitted over a network, and you can use the functions that interpret the bytes of data without modification to interpret data that the device receives from a server. In the next chapter, you’ll see how to add a little bit of communications code to this same example game to allow a remote server to update the data used by the game.


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

     

    Buy this book now. This article is taken from chapter five of the book J2ME Games with MIDP2, written by Carol Hamer (Apress, 2004; ISBN: 1590593820). Check it out at your favorite bookstore. Buy this book now.

    JAVA ARTICLES

    - Deploying Multiple Java Applets as One
    - Deploying Java Applets
    - Understanding Deployment Frameworks
    - Database Programming in Java Using JDBC
    - Extension Interfaces and SAX
    - Entities, Handlers and SAX
    - Advanced SAX
    - Conversions and Java Print Streams
    - Formatters and Java Print Streams
    - Java Print Streams
    - Wildcards, Arrays, and Generics in Java
    - Wildcards and Generic Methods in Java
    - Finishing the Project: Java Web Development ...
    - Generics and Limitations in Java
    - Getting Started with Java Web Development in...







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    Stay green...Green IT