How to scroll an image, if it does not fit in the display

转载 2006年05月28日 19:29:00
answered by 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(); }

【转帖】Android zxing 横屏 改竖屏识别解决方案

在网上找了很多方法,但最后都有问题,自己调试了好几个小时,最后终于完美解决了竖屏识别。 首先你需要有zxing项目的简化版代码,在这里。 使用简化版可以免去许多不必要的代码,方便学习研究,...
  • ilittleone
  • ilittleone
  • 2013年10月15日 11:28
  • 15958

zxing横屏改为竖屏识别,多次扫描,以及存在的摄像拉伸的问题

摘自http://www.cnblogs.com/moka/archive/2013/05/24/3096937.html 按照以上做法,可以实现。 使用简化版可以免去许多不必要的代码,方便学习研...
  • chencehnggq
  • chencehnggq
  • 2013年12月05日 13:00
  • 8244

Collections.copy 报错: Source does not fit in dest

今天在使用 Collections.copy 方法时候,报错 Source does not fit in dest 自己方法中的代码如下: List proStateList = Selec...
  • fuli_mouren
  • fuli_mouren
  • 2015年04月22日 15:22
  • 1965

普通二维码生成

依赖:ZXing-core-3.2.1.jar 1、二维码生成import java.awt.image.BufferedImage; import java.io.File; import jav...
  • zk_1325572803
  • zk_1325572803
  • 2017年09月07日 13:55
  • 115

FIT image介绍

作者:wowo 发布于:2016-9-2 21:49 分类:u-boot分析 1. 前言 Linux kernel在ARM架构中引入device tree(全称是flattened devic...
  • ee230
  • ee230
  • 2016年11月24日 10:55
  • 292

u-boot FIT image介绍

1. 前言 Linux kernel在ARM架构中引入device tree(全称是flattened device tree,后续将会以FDT代称)的时候[1],其实怀揣了一个Unify Ke...
  • wdsfup
  • wdsfup
  • 2016年09月19日 15:10
  • 1839

在Allegro中放置的元件显示不出来E- (SPMHA1-150): Symbol does not fit into the drawing ... reposition the symbol.

现这个问题的原因是:你在建立的这个元件封装的坐标原点超出了这个板子的范围。解决方法有两种: 一是,修改这个封装,一般我们最好把器件的原点设置在1脚位置或者中心位置,总之在器件内部,这个放置时就不...
  • songisgood
  • songisgood
  • 2015年12月11日 20:56
  • 1948

Allegro报“Symbol does not fit into the drawing”解决办法

问题: 解决办法:
  • David_xtd
  • David_xtd
  • 2014年06月26日 13:59
  • 5717

java集合复制的另一种方法

Question: Hi, I want to copy a List to an ArrayList but I can't seem to be able to do so...
  • zzcchunter
  • zzcchunter
  • 2011年10月03日 00:33
  • 15107

Android 5.1编译大于2G的OTA包是会出 Android OverflowError: size does not fit in an int或者would require ZIP64 ex

Android 5.1编译大于2G的OTA包是会出 AndroidOverflowError: size does not fit in an int或者would require ZIP64exte...
  • oman111
  • oman111
  • 2015年12月11日 16:12
  • 1614
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:How to scroll an image, if it does not fit in the display
举报原因:
原因补充:

(最多只允许输入30个字)