Answered
Micro Java Network (Roman bialach)
So you have this large image that will not fit into the display, this application will show you how you can use the key and pointer into to display different portions of the image.
import javax.microedition.midlet.*;import javax.microedition.lcdui.*;/** * ImageMove application for phone and PDA. * * This class implements AppExiter to exit the application. It passes * its own reference to the clss that is listening to the Commands. */public class ImageMove extends MIDlet implements AppExiter{ /** * Signals the MIDlet to start and enter the Active state. */ protected void startApp() { // create the screen and pass a reference to so that EXIT can be // performed // tell the Display Manager to display our screen Display.getDisplay(this).setCurrent(new ImageMoveScreen(this)); } /** * Signals the MIDlet to stop and enter the Paused state. */ protected void pauseApp() { // we need to implement this since it is abstract in the MIDlet } /** * Signals the MIDlet to terminate and enter the Destroyed state. */ protected void destroyApp(boolean unconditional) { // notify the application management that it has entered into the // Destroyed state, all resources held by the MIDlet will be considered // eligible for reclamation. // Normally, we would performed some clean-up operations here (releasing of // resources etc.) but we have nothing to release notifyDestroyed(); } /** * Indicates that the app must exit. */ public void exitApp() { destroyApp(true); }}/** * Since we are drawing on the screen and need to control the position * of the "cursor", we need to use the Canvas. We implement the paint() * method to perform the actual drawing and the keyPressed() method for * the movement. *
* We put an exit Command on the Canvas, so that we give the user the ability * to exit. So we need to implement CommandListener and add code to the * commandAction method. But now what? The destroyApp() method is protected, * we use our AppExiter interface to call the destroyApp() method. *
* There are other ways to do this, but this seemed to be the simplest. */class ImageMoveScreen extends Canvas implements CommandListener{ // save some calculated values for speed private int m_screenWidth = getWidth(); private int m_screenHeight = getHeight(); private int m_imageWidth; private int m_imageHeight; // values we need for movement private int m_x; private int m_y; private int m_dx = 0; private int m_dy = 0; // objects necessary for app private AppExiter m_appExiter; private KeyRepeater m_repeat = new KeyRepeater(); private Image m_origImage; private Image m_screenBuf; private Graphics m_bufferGraphics; /** * The constructor assigns the AppExiter, load the image, and initialies * all the constants. It also adds the EXIT Command, and registers itself * as a command listener. */ public ImageMoveScreen(AppExiter exiter) { m_appExiter = exiter; try { m_origImage = Image.createImage("/background.png"); } catch (Exception e) { m_origImage = null; } // calculate some constant values m_imageWidth = m_origImage.getWidth(); m_imageHeight = m_origImage.getHeight(); m_x = (m_screenWidth - m_imageWidth)/2; m_y = (m_screenHeight - m_imageHeight)/2; // created a buffered screen m_screenBuf = Image.createImage(m_screenWidth, m_screenHeight); // Front graphics m_screenBuffer m_bufferGraphics = m_screenBuf.getGraphics(); m_bufferGraphics.setColor(0,0,0); m_bufferGraphics.fillRect(0,0,m_screenWidth,m_screenHeight); // add the EXIT command to the Canvas addCommand(AppExiter.EXIT); // register the Canavas as the listener for Commands, the action for // the Commands will be handled in the commandAction() method setCommandListener(this); m_repeat = new KeyRepeater(); m_repeat.start(); } /** * Draw all the objects on the screen. * * @param g The Graphics object to be used for rendering the Canvas. */ protected void paint(Graphics g) { m_bufferGraphics.drawImage(m_origImage, m_x, m_y, m_bufferGraphics.TOP|m_bufferGraphics.LEFT); g.drawImage(m_screenBuf, 0, 0, Graphics.TOP|Graphics.LEFT); g.setColor(255,255,255); g.drawString(m_x + " " + m_y, 0, 0, Graphics.TOP|Graphics.LEFT); } /** * Called when a key is repeated. * * @param keyCode The key code of the key that was pressed. */ protected void keyRepeated(int keyCode) { if (hasRepeatEvents()) { moveImage(getGameAction(keyCode)); } } /** * Called when a key is pressed. * * @param keyCode The key code of the key that was pressed. */ protected void keyPressed(int keyCode) { if (!hasRepeatEvents()) { m_repeat.startRepeat(keyCode); } moveImage(getGameAction(keyCode)); } /** * Called when a key is released. * * @param keyCode The key code of the key that was pressed. */ protected void keyReleased(int keyCode) { if (!hasRepeatEvents()) { m_repeat.stopRepeat(keyCode); } } private void moveImage(int gameAction) { // the codes coming back do not map exactly to the GAME ACTION keys // we need to map them using getGameAction switch(gameAction) { case UP: if (m_y < 0) m_y++; break; case DOWN: if (m_y > m_screenHeight - m_imageHeight) m_y--; break; case LEFT: if (m_x < 0) m_x++; break; case RIGHT: if (m_x > m_screenWidth - m_imageWidth) m_x--; break; } // we want to show the result, so we ask the canvas to repaint repaint(); } /** * 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). */ // this method is for devices with pointers (PDAs) protected void pointerPressed(int x, int y) { if (hasPointerEvents()) { 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) { if (hasPointerMotionEvents()) { 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; } repaint(); } } /** * Indicates that a Command c event has occurred on Displayable d. *
* This is where we handle all the Commands that we know how. Everything * else is passed to the MIDlet (we defined it as a CommandListener as * well). * * @param c A Command object identifying the command. * @param d The Displayable on which this event has occurred. */ public void commandAction(Command c, Displayable d) { if (c == ImageMove.EXIT) { m_repeat.done(); m_repeat = null; m_appExiter.exitApp(); } } /** * Emulates the key repeat functionality if it does not exist in the device. */ class KeyRepeater extends Thread { private boolean m_done = false; private int m_gameAction; /** * Stops the thread. */ public synchronized void done() { m_done = true; } /** * Start the key to be repeated. * * @param keyCode The key to be repeated. */ public synchronized void startRepeat(int keyCode) { m_gameAction = getGameAction(keyCode); } /** * Stop the key from being repeated. 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 keyCode The key to be repeated. */ public synchronized void stopRepeat(int keyCode) { if (getGameAction(keyCode) == m_gameAction) { m_gameAction = 0; } } /** * Perform the repetition of the key. */ public void run() { while (!m_done) { // yield to other threads if thereis no key to be repeated while (m_gameAction == 0 && !m_done) { Thread.yield(); } // wait for a little bit synchronized (this) { try { sleep(10); } catch (InterruptedException e) { } } // perform the repetition of the last key moveImage(m_gameAction); } } }}/** * This interface is used for any object that can perform an exit. */interface AppExiter{ /** Since many screens will be using the EXIT command then define it * onecd and make a public final. */ public static final Command EXIT = new Command("Exit", Command.EXIT, 1); /** * Indicates that the app must exit. */ public void exitApp();}