Quatsch MIDlet : 2D « J2ME « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JDK 6
25.JNDI LDAP
26.JPA
27.JSP
28.JSTL
29.Language Basics
30.Network Protocol
31.PDF RTF
32.Reflection
33.Regular Expressions
34.Scripting
35.Security
36.Servlets
37.Spring
38.Swing Components
39.Swing JFC
40.SWT JFace Eclipse
41.Threads
42.Tiny Application
43.Velocity
44.Web Services SOA
45.XML
Java » J2ME » 2DScreenshots 
Quatsch MIDlet
Quatsch MIDlet

/*
Wireless Java 2nd edition 
Jonathan Knudsen
Publisher: Apress
ISBN: 1590590775 
*/
import java.io.IOException;

import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;
import javax.microedition.midlet.MIDlet;

public class QuatschMIDlet extends MIDlet
    implements CommandListener {
  private Display mDisplay;
  
  private QuatschCanvas mQuatschCanvas;
  private Form mShowForm;
  private Command mExitCommand, mShowCommand, mOkCommand;
  
  public void startApp() {
    if (mQuatschCanvas == null) {
      try {
        mQuatschCanvas = new QuatschCanvas("/quatsch.png",
            "/atmosphere.png""/background_tiles.png");
        mQuatschCanvas.start();
        mExitCommand = new Command("Exit", Command.EXIT, 0);
        mShowCommand = new Command("Show/Hide", Command.SCREEN, 0);
        mOkCommand = new Command("OK", Command.OK, 0);
        mQuatschCanvas.addCommand(mExitCommand);
        mQuatschCanvas.addCommand(mShowCommand);
        mQuatschCanvas.setCommandListener(this);
      }
      catch (IOException ioe) {
        System.out.println(ioe);
      }
    }
    
    mDisplay = Display.getDisplay(this);
    mDisplay.setCurrent(mQuatschCanvas);
  }
  
  public void pauseApp() {}
  
  public void destroyApp(boolean unconditional) {
    mQuatschCanvas.stop();
  }
  
  public void commandAction(Command c, Displayable s) {
    if (c.getCommandType() == Command.EXIT) {
      destroyApp(true);
      notifyDestroyed();
    }
    else if (c == mShowCommand) {
      if (mShowForm == null) {
        mShowForm = new Form("Show/Hide");
        ChoiceGroup cg = new ChoiceGroup("Layers", Choice.MULTIPLE);
        cg.append("Fog"null);
        cg.append("Dr. Quatsch"null);
        cg.append("Background"null);
        mShowForm.append(cg);
        mShowForm.addCommand(mOkCommand);
        mShowForm.setCommandListener(this);
      }
      ChoiceGroup cg = (ChoiceGroup)mShowForm.get(0);
      cg.setSelectedIndex(0, mQuatschCanvas.isVisible(0));
      cg.setSelectedIndex(1, mQuatschCanvas.isVisible(1));
      cg.setSelectedIndex(2, mQuatschCanvas.isVisible(2));
      mDisplay.setCurrent(mShowForm);
    }
    else if (c == mOkCommand) {
      ChoiceGroup cg = (ChoiceGroup)mShowForm.get(0);
      mQuatschCanvas.setVisible(0, cg.isSelected(0));
      mQuatschCanvas.setVisible(1, cg.isSelected(1));
      mQuatschCanvas.setVisible(2, cg.isSelected(2));
      mDisplay.setCurrent(mQuatschCanvas);
    }
  }
}

class QuatschCanvas extends GameCanvas implements Runnable {
  private boolean mTrucking;
  
  private LayerManager mLayerManager;
  
  private TiledLayer mAtmosphere;
  private TiledLayer mBackground;
  private int mAnimatedIndex;
  
  private Sprite mQuatsch;
  private int mState, mDirection;
  
  private static final int kStanding = 1;
  private static final int kRunning = 2;
  
  private static final int kLeft = 1;
  private static final int kRight = 2;
  
  private static final int[] kRunningSequence = 01};
  private static final int[] kStandingSequence = };
  
  public QuatschCanvas(String quatschImageName,
      String atmosphereImageName, String backgroundImageName)
      throws IOException {
    super(true);
    
    // Create a LayerManager.
    mLayerManager = new LayerManager();
    int w = getWidth();
    int h = getHeight();
    mLayerManager.setViewWindow(960, w, h);
    
    createBackground(backgroundImageName);
    createAtmosphere(atmosphereImageName);
    createQuatsch(quatschImageName);
  }
  
  private void createBackground(String backgroundImageName)
      throws IOException {
    // Create the tiled layer.
    Image backgroundImage = Image.createImage(backgroundImageName);
    int[] map = {
      12000000,
      33200050,
      33324132,
      66666666
    };
    mBackground = new TiledLayer(84, backgroundImage, 4848);
    mBackground.setPosition(120);
    for (int i = 0; i < map.length; i++) {
      int column = i % 8;
      int row = (i - column8;
      mBackground.setCell(column, row, map[i]);
    }
    mAnimatedIndex = mBackground.createAnimatedTile(8);
    mBackground.setCell(30, mAnimatedIndex);
    mBackground.setCell(50, mAnimatedIndex);
    mLayerManager.append(mBackground);
  }
    
  private void createAtmosphere(String atmosphereImageName)
      throws IOException {
    // Create the atmosphere layer
    Image atmosphereImage = Image.createImage(atmosphereImageName);
    mAtmosphere = new TiledLayer(81, atmosphereImage,
        atmosphereImage.getWidth(), atmosphereImage.getHeight());
    mAtmosphere.fillCells(00811);
    mAtmosphere.setPosition(096);
    mLayerManager.insert(mAtmosphere, 0);
  }

  private void createQuatsch(String quatschImageName)
      throws IOException {
    // Create the sprite.
    Image quatschImage = Image.createImage(quatschImageName);
    mQuatsch = new Sprite(quatschImage, 4848);
    mQuatsch.setPosition(96 (getWidth() 48296);
    mQuatsch.defineReferencePixel(2424);
    setDirection(kLeft);
    setState(kStanding);
    mLayerManager.insert(mQuatsch, 1);
  }

  public void start() {
    mTrucking = true;
    Thread t = new Thread(this);
    t.start();
  }
  
  public void run() {
    int w = getWidth();
    int h = getHeight();
    Graphics g = getGraphics();
    int frameCount = 0;
    int factor = 2;
    int animatedDelta = 0;
    
    while (mTrucking) {
      if (isShown()) {
        int keyStates = getKeyStates();
        if ((keyStates & LEFT_PRESSED!= 0) {
          setDirection(kLeft);
          setState(kRunning);
          mBackground.move(30);
          mAtmosphere.move(30);
          mQuatsch.nextFrame();
        }
        else if ((keyStates & RIGHT_PRESSED!= 0) {
          setDirection(kRight);
          setState(kRunning);
          mBackground.move(-30);
          mAtmosphere.move(-30);
          mQuatsch.nextFrame();
        }
        else {
          setState(kStanding);
        }
        
        frameCount++;
        if (frameCount % factor == 0) {
          int delta = 1;
          if (frameCount / factor < 10delta = -1;
          mAtmosphere.move(delta, 0);
          if (frameCount / factor == 20frameCount = 0;

          mBackground.setAnimatedTile(mAnimatedIndex,
              + animatedDelta++);
          if (animatedDelta == 3animatedDelta = 0;
        }

        g.setColor(0x5b1793);
        g.fillRect(00, w, h);
        
        mLayerManager.paint(g, 00);
        
        flushGraphics();
      }
      
      try Thread.sleep(80)}
      catch (InterruptedException ie) {}
    }
  }
  
  public void stop() {
    mTrucking = false;
  }
  
  public void setVisible(int layerIndex, boolean show) {
    Layer layer = mLayerManager.getLayerAt(layerIndex);
    layer.setVisible(show);
  }
  
  public boolean isVisible(int layerIndex) {
    Layer layer = mLayerManager.getLayerAt(layerIndex);
    return layer.isVisible();
  }
  
  private void setDirection(int newDirection) {
    if (newDirection == mDirectionreturn;
    if (mDirection == kLeft)
      mQuatsch.setTransform(Sprite.TRANS_MIRROR);
    else if (mDirection == kRight)
      mQuatsch.setTransform(Sprite.TRANS_NONE);
    mDirection = newDirection;
  }
  
  private void setState(int newState) {
    if (newState == mStatereturn;
    switch (newState) {
      case kStanding:
        mQuatsch.setFrameSequence(kStandingSequence);
        mQuatsch.setFrame(0);
        break;
      case kRunning:
        mQuatsch.setFrameSequence(kRunningSequence);
        break;
      default:
        break;
    }
    mState = newState;
  }
}



           
       
Related examples in the same category
1.Simple Midlet DemoSimple Midlet Demo
2.Piano MIDletPiano MIDlet
3.PacerPacer
4.Pointer ExamplePointer Example
5.Text Example
6.Simple Canvas
7.Illustrate Graphics MIDletIllustrate Graphics MIDlet
8.Translate CoordinatesTranslate Coordinates
9.Key Canvas
10.Box Text CanvasBox Text Canvas
11.Offscreen MIDletOffscreen MIDlet
12.Sweep GameSweep Game
13.SweepSweep
14.Text MIDletText MIDlet
15.Translate coordinate systemTranslate coordinate system
16.Show various anchor pointsShow various anchor points
17.Draw mutable image on a canvas
18.Canvas for processing key code and commandsCanvas for processing key code and commands
19.Draw immutable image on a canvasDraw immutable image on a canvas
20.Use pointer events to draw onto the CanvasUse pointer events to draw onto the Canvas
21.A quick sample of graphics, commands, and event handling.A quick sample of graphics,  commands, and event handling.
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.