Code/Resource
Windows Develop
Linux-Unix program
Internet-Socket-Network
Web Server
Browser Client
Ftp Server
Ftp Client
Browser Plugins
Proxy Server
Email Server
Email Client
WEB Mail
Firewall-Security
Telnet Server
Telnet Client
ICQ-IM-Chat
Search Engine
Sniffer Package capture
Remote Control
xml-soap-webservice
P2P
WEB(ASP,PHP,...)
TCP/IP Stack
SNMP
Grid Computing
SilverLight
DNS
Cluster Service
Network Security
Communication-Mobile
Game Program
Editor
Multimedia program
Graph program
Compiler program
Compress-Decompress algrithms
Crypt_Decrypt algrithms
Mathimatics-Numerical algorithms
MultiLanguage
Disk/Storage
Java Develop
assembly language
Applications
Other systems
Database system
Embeded-SCM Develop
FlashMX/Flex
source in ebook
Delphi VCL
OS Develop
MiddleWare
MPI
MacOS develop
LabView
ELanguage
Software/Tools
E-Books
Artical/Document
ScrollCanvas.java
Package: J2ME&Game.rar [view]
Upload User: gyyuli
Upload Date: 2013-07-09
Package Size: 3050k
Code Size: 5k
Category:
J2ME
Development Platform:
Java
- import java.io.IOException;
- import javax.microedition.lcdui.*;
- import javax.microedition.lcdui.game.*;
- /**
- * The GameCanvas that dispalys the different background layers
- *
- */
- public class ScrollCanvas extends GameCanvas implements Runnable {
- private boolean isRunning;
- private LayerManager layerManager;
- // Sets the pixel speed of the backgrounds
- private static final int BACKGROUND_HORIZONTAL_MOVE = -2;
- private static final int FOREGROUND_HORIZONTAL_MOVE = -4;
- private int w = getWidth();
- private int h = getHeight();
- public ScrollCanvas() throws IOException {
- super(true);
- w = getWidth();
- h = getHeight();
- // Create a LayerManager.
- layerManager = new LayerManager();
- layerManager.setViewWindow(0, 0, w, h);
- // The penguin will get the first LayerManager index, thus being foremost
- createpenguin();
- // The Backgrounds will also be appended to the LayerManager
- createBackgrounds();
- }
- /**
- * Creates the individual backgrounds and adds them to a LayerManager
- */
- private void createBackgrounds()throws IOException {
- layerManager.append(createForegroundMountains());
- layerManager.append(createBackgroundMountains());
- }
- private BackgroundLayer createForegroundMountains()throws IOException {
- Image backgroundImage = Image.createImage("/mountain.png");
- int[] map = {
- 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,
- 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
- };
- int maprows=2;
- int mapcols=48;
- BackgroundLayer background = new BackgroundLayer(mapcols, maprows, backgroundImage, 48, 48 );
- background.setHorizontalMove(FOREGROUND_HORIZONTAL_MOVE);
- background.setPosition(12, 176-(maprows*48));
- background.setCells(map,maprows, mapcols);
- return background;
- }
- private BackgroundLayer createBackgroundMountains()throws IOException {
- Image backgroundImage = Image.createImage("/mountain.png");
- int[] map = {
- 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,
- 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
- };
- int maprows=2;
- int mapcols=48;
- BackgroundLayer background = new BackgroundLayer(mapcols, maprows, backgroundImage, 48, 48 );
- background.setHorizontalMove(BACKGROUND_HORIZONTAL_MOVE);
- background.setPosition(12, 176-(maprows*48));
- background.setCells(map,maprows, mapcols);
- return background;
- }
- private void createpenguin()throws IOException {
- Image penguinImage = Image.createImage("/penguin.png");
- // Even though it's actually a penguin, lets call it a penguin...
- Sprite penguin = new Sprite(penguinImage, 48, 48);
- penguin.setPosition(0 + (getWidth() - 48) / 2, 96);
- penguin.defineReferencePixel(24, 24);
- layerManager.append(penguin);
- }
- public void start() {
- isRunning = true;
- Thread t = new Thread(this);
- t.start();
- }
- public void run() {
- Graphics g = getGraphics();
- // Main game loop, do this to the bitter end (well, until the backgrounds has reached their ends)
- while (isRunning) {
- if (isShown()) {
- int keyStates = getKeyStates();
- // Move the penguin
- if ((keyStates & UP_PRESSED) != 0) {
- layerManager.getLayerAt(0).move(0,-2);
- }else if ((keyStates & DOWN_PRESSED) != 0) {
- layerManager.getLayerAt(0).move(0,2);
- }
- // Move the Backgrounds
- scrollBackgrounds();
- // Stop the game when all background have scolled by...
- if(EndOfBackgrounds()){
- isRunning=false;
- }
- g.setColor(0x6600CC);
- g.fillRect(0, 0, w, h);
- layerManager.paint(g, 0, 0);
- flushGraphics();
- }
- try {
- Thread.sleep(10);
- }catch (InterruptedException e) {System.out.println(e.getMessage());}
- }
- }
- public void stop() {
- isRunning = false;
- }
- /**
- * Scrolls the backgrounds individually
- */
- private void scrollBackgrounds(){
- BackgroundLayer background;
- //Don't mind the first Layer of the LayerManager, it's the penguin
- for(int i=1; i<layerManager.getSize(); i++){
- background = (BackgroundLayer)layerManager.getLayerAt(i);
- background.move();
- }
- }
- /**
- * Just checks if the backgrounds have reached their end.
- * Returns true if thats the case, false otherwise.
- */
- private boolean EndOfBackgrounds(){
- //Don't mind the first Layer of the LayerManager, it's the penguin!
- for(int i=1; i<layerManager.getSize(); i++){
- if (((BackgroundLayer)layerManager.getLayerAt(i)).endOfBackground(w)){
- return true;
- }
- }
- return false;
- }
- }