ScrollCanvas.java
Upload User: gyyuli
Upload Date: 2013-07-09
Package Size: 3050k
Code Size: 5k
Category:

J2ME

Development Platform:

Java

  1. import java.io.IOException;
  2. import javax.microedition.lcdui.*;
  3. import javax.microedition.lcdui.game.*;
  4. /**
  5.  * The GameCanvas that dispalys the different background layers
  6.  *
  7. */
  8. public class ScrollCanvas extends GameCanvas implements Runnable {
  9.   private boolean isRunning;
  10.   
  11.   private LayerManager layerManager;  
  12.   
  13.   // Sets the pixel speed of the backgrounds
  14.   private static final int BACKGROUND_HORIZONTAL_MOVE = -2;
  15.   private static final int FOREGROUND_HORIZONTAL_MOVE = -4;
  16.   private int w = getWidth();
  17.   private int h = getHeight();
  18.   
  19.   public ScrollCanvas() throws IOException {
  20.     super(true);    
  21.     w = getWidth();
  22.     h = getHeight();
  23.     
  24.     // Create a LayerManager.
  25.     layerManager = new LayerManager();
  26.     layerManager.setViewWindow(0, 0, w, h);
  27.    
  28.     // The penguin will get the first LayerManager index, thus being foremost 
  29.     createpenguin();
  30.     
  31.     // The Backgrounds will also be appended to the LayerManager
  32.     createBackgrounds();
  33.    
  34.   }
  35.   
  36.   /**
  37.    * Creates the individual backgrounds and adds them to a LayerManager
  38.    */
  39.   private void createBackgrounds()throws IOException {
  40.       layerManager.append(createForegroundMountains());  
  41.       layerManager.append(createBackgroundMountains());
  42.   }
  43.   
  44.   private BackgroundLayer createForegroundMountains()throws IOException {
  45.         Image backgroundImage = Image.createImage("/mountain.png");        
  46.         int[] map = {
  47.             0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,2,0,0,0,
  48.             0,0,0,0,0,1,2,0,0,0,0,1,2,0,0,0,0,0,1,3,3,2,0,0,0,0,0,0,0,1,2,0,0,0,0,1,2,0,0,0,0,0,1,3,3,2,0,0
  49.          };
  50.          int maprows=2;
  51.          int mapcols=48;
  52.         BackgroundLayer background = new BackgroundLayer(mapcols, maprows, backgroundImage, 48, 48 );
  53.         background.setHorizontalMove(FOREGROUND_HORIZONTAL_MOVE);
  54.         background.setPosition(12, 176-(maprows*48));
  55.         background.setCells(map,maprows, mapcols);
  56.        return background;
  57.   }
  58.   
  59.   private BackgroundLayer createBackgroundMountains()throws IOException {
  60.         Image backgroundImage = Image.createImage("/mountain.png");  
  61.           int[] map = {
  62.             0,1,2,0,0,0,0,1,2,0,0,0,0,1,2,0,0,0,0,1,2,0,0,0,0,1,2,0,0,0,0,1,2,0,0,0,0,1,2,0,0,0,0,1,2,0,0,0,
  63.             1,3,3,2,0,0,1,3,3,2,0,0,1,3,3,3,3,3,3,3,3,2,0,0,1,3,3,2,0,0,1,3,3,2,0,0,1,3,3,3,3,3,3,3,3,2,0,0
  64.          };
  65.          int maprows=2;
  66.          int mapcols=48;
  67.         BackgroundLayer background = new BackgroundLayer(mapcols, maprows, backgroundImage, 48, 48 );
  68.         background.setHorizontalMove(BACKGROUND_HORIZONTAL_MOVE);
  69.         background.setPosition(12, 176-(maprows*48));
  70.         background.setCells(map,maprows, mapcols);
  71.        return background;
  72.   }
  73.   
  74.   
  75.   private void createpenguin()throws IOException {
  76.     Image penguinImage = Image.createImage("/penguin.png");
  77.     // Even though it's actually a penguin, lets call it a penguin...
  78.     Sprite penguin = new Sprite(penguinImage, 48, 48);
  79.     penguin.setPosition(0 + (getWidth() - 48) / 2, 96);
  80.     penguin.defineReferencePixel(24, 24);
  81.     layerManager.append(penguin);
  82.   }
  83.   public void start() {
  84.     isRunning = true;
  85.     Thread t = new Thread(this);
  86.     t.start();
  87.   }
  88.   
  89.   public void run() {
  90.     Graphics g = getGraphics();
  91.     
  92.     // Main game loop, do this to the bitter end (well, until the backgrounds has reached their ends)
  93.     while (isRunning) {
  94.       if (isShown()) {
  95.         int keyStates = getKeyStates();
  96.         // Move the penguin
  97.         if ((keyStates & UP_PRESSED) != 0) {
  98.           layerManager.getLayerAt(0).move(0,-2);
  99.         }else if ((keyStates & DOWN_PRESSED) != 0) {
  100.           layerManager.getLayerAt(0).move(0,2);
  101.         }
  102.         
  103.         // Move the Backgrounds
  104.         scrollBackgrounds();
  105.         
  106.         // Stop the game when all background have scolled by...
  107.         if(EndOfBackgrounds()){
  108.             isRunning=false;
  109.         }
  110.         g.setColor(0x6600CC);
  111.         g.fillRect(0, 0, w, h);
  112.         layerManager.paint(g, 0, 0);
  113.         flushGraphics();
  114.       }
  115.       
  116.       try { 
  117.           Thread.sleep(10);
  118.       }catch (InterruptedException e) {System.out.println(e.getMessage());}
  119.     }
  120.   }
  121.   
  122.   public void stop() {
  123.     isRunning = false;
  124.   }
  125.   
  126.   /**
  127.    * Scrolls the backgrounds individually
  128.    */
  129.   private void scrollBackgrounds(){ 
  130.     BackgroundLayer background; 
  131.     //Don't mind the first Layer of the LayerManager, it's the penguin
  132.    for(int i=1; i<layerManager.getSize(); i++){
  133.      background = (BackgroundLayer)layerManager.getLayerAt(i);
  134.      background.move();
  135.    }
  136.   }
  137.   
  138.   /**
  139.    * Just checks if the backgrounds have reached their end.
  140.    * Returns true if thats the case, false otherwise.
  141.    */
  142.   private boolean EndOfBackgrounds(){
  143.      //Don't mind the first Layer of the LayerManager, it's the penguin!
  144.     for(int i=1; i<layerManager.getSize(); i++){
  145.         if (((BackgroundLayer)layerManager.getLayerAt(i)).endOfBackground(w)){
  146.             return true;
  147.         }
  148.     }
  149.     return false;
  150.   }
  151. }