Wednesday, August 09, 2006

Image Scroller for J2ME

Here is a class written for J2ME used for image scrolling on the screen.
Previously I posted this on Bulletproof Productions but here it is again.


/*
* ImageScroller.java
* (C) Copyright Serkan Azmi. 2001
* All rights reserved
* The material(s) may be used and/or copied only with the written
* permission of Serkan Azmi. or in accordance with the terms and
* conditions stipulated in any agreement/contract under which
* the material(s) have been supplied.
* Created on March 19, 2002, 8:08 PM
*/



/**
*
* @author serkan azmi
* @version 1.0
*/

import javax.microedition.lcdui.*;

/**
* This object is used to pan an Image.
*/

public class ImageScroller implements ActionListener {
private final Image m_image;
private final int m_screenWidth;
private final int m_screenHeight;
private final int m_imageWidth;
private final int m_imageHeight;

// values we need for movement
private int m_dx = 0;
private int m_dy = 0;
private int m_x;
private int m_y;
private int m_gameAction;
public static int m_factor;

/**
* The constructor registers the object with the updater,
* loads the image,and initialies all the constants.
*/
public ImageScroller(Canvas screen, Image image)
throws Exception {
ScrollScreen.register(this);
m_screenWidth = screen.getWidth();
m_screenHeight = screen.getHeight();
m_image = image;
m_imageWidth = m_image.getWidth();
m_imageHeight = m_image.getHeight();
m_x = (m_screenWidth - m_imageWidth)/2;
m_y = (m_screenHeight - m_imageHeight)/2;
m_factor = 1;
}

/**
* Draw the object on the screen.
*
* @param g The Graphics object to be used for rendering
* the Canvas.
*/
protected void paint(Graphics g) {
g.drawImage(m_image, m_x, m_y, Graphics.TOP|Graphics.LEFT);
}

/**
* Called when a game action is to be performed.
*/
public void performAction() {

System.out.println(""+m_factor);
switch(m_gameAction) {

case Canvas.UP:
if ((m_y+m_factor) < 0) {
m_y += m_factor;
}
else if (m_y < 0) {
m_y++;
}
break;
case Canvas.DOWN:
if((m_y-m_factor) > m_screenHeight-m_imageHeight) {
m_y -= m_factor;
}
else if (m_y > m_screenHeight-m_imageHeight) {
m_y--;
}
break;
case Canvas.LEFT:
if ((m_x+m_factor) < 0) {
m_x += m_factor;
}
else if (m_x < 0) {
m_x++;
}
break;
case Canvas.RIGHT:
if((m_x-m_factor) > m_screenWidth - m_imageWidth) {
m_x -= m_factor;
}
else if (m_x > m_screenWidth - m_imageWidth) {
m_x--;
}
break;
}
}

/**
* Called when a key is released. If the currently repeated
* key is the same as the one that is to be stopped then stop;
* otherwise ignore it, we must be repeating a different key.
*
* @param gameAction The game action that was pressed.
*/
protected void keyReleased(int gameAction) {
if (gameAction == m_gameAction) {
m_gameAction = 0;
}
}

/**
* Called when a key is pressed. Initialises the game action
* member variable only for the appropriate keys.
*
* @param gameAction The game action that was pressed.
*/
protected void keyPressed(int gameAction) {
switch (gameAction) {
case Canvas.LEFT:
case Canvas.RIGHT:
case Canvas.UP:
case Canvas.DOWN:
m_gameAction = gameAction;
break;
}
}


/**
* Called when a key is pressed. Initialises the game action
* member variable only for the appropriate keys.
*/
protected void keyRepeated() {
switch (m_gameAction) {
case Canvas.LEFT:
case Canvas.RIGHT:
case Canvas.UP:
case Canvas.DOWN:
m_gameAction = m_gameAction;
break;
}
}


/**
* Called when the pointer is pressed.
*


* This method is for devices with pointers (PDAs).
*
* @param x The horizontal location where the pointer was
* released (relative to the Canvas).
* @param y The vertical location where the pointer was
* released (relative to the Canvas).
*/
protected void pointerPressed(int x, int y) {
m_dx = x;
m_dy = y;
}

/**
* Called when the pointer is dragged.
*


* This method is for devices with pointers (PDAs).
*
* @param x The horizontal location where the pointer was
* released (relative to the Canvas).
* @param y The vertical location where the pointer was
* released (relative to the Canvas).
*/
protected void pointerDragged(int x, int y) {
m_y += y - m_dy;
m_x += x - m_dx;
m_dx = x;
m_dy = y;

if (m_y > 0) {
m_y = 0;
}
else if (m_y < m_screenHeight - m_imageHeight) {
m_y = m_screenHeight - m_imageHeight;
}

if (m_x > 0) {
m_x = 0;
}
else if (m_x < m_screenWidth - m_imageWidth) {
m_x = m_screenWidth - m_imageWidth;
}
}
}

No comments: