[url=http://www.coderanch.com/t/229731/Java-Micro-Edition/Mobile/Horizontal-Scroll-Bar-J-ME]一个提问[/url]
[url=http://203.208.37.132/search?q=cache:TL_ZHq7Sy9oJ:www.experts-exchange.com/Programming/Languages/Java/Q_20840864.html+Java+Programming:+J2ME+-+Midlet+Form+-+Horizontal+Scrolling&cd=1&hl=zh-CN&ct=clnk&gl=cn&st_usg=ALhdy2-8keHKA1WvbOp4KQwBskLr2VkCQg]按照回帖的方法从google cache获取的solution页面[/url]
下面是代码:
MIDlet
Canvas
[url=http://203.208.37.132/search?q=cache:TL_ZHq7Sy9oJ:www.experts-exchange.com/Programming/Languages/Java/Q_20840864.html+Java+Programming:+J2ME+-+Midlet+Form+-+Horizontal+Scrolling&cd=1&hl=zh-CN&ct=clnk&gl=cn&st_usg=ALhdy2-8keHKA1WvbOp4KQwBskLr2VkCQg]按照回帖的方法从google cache获取的solution页面[/url]
下面是代码:
MIDlet
public class Scroller extends MIDlet implements CommandListener
{
private Display display;
private Command exitCommand;
private Command viewCommand;
private Form mainForm;
private ScrollCanvas scrollCanvas;
public Scroller()
{
exitCommand = new Command("Exit", Command.EXIT, 1);
viewCommand = new Command("View", Command.SCREEN, 1);
mainForm = new Form("Scroller");
mainForm.append("Press 'View' to access the scrolling image");
mainForm.addCommand(viewCommand);
mainForm.addCommand(exitCommand);
mainForm.setCommandListener(this);
scrollCanvas = new ScrollCanvas();
scrollCanvas.addCommand(exitCommand);
scrollCanvas.setCommandListener(this);
}
public void startApp()
{
display = Display.getDisplay(this);
display.setCurrent(mainForm);
}
public void pauseApp()
{
}
public void destroyApp(boolean flag)
{
}
public void commandAction(Command command, Displayable displayable)
{
if (command == viewCommand)
{
display.setCurrent(scrollCanvas);
}
else if (command == exitCommand)
{
destroyApp(true);
notifyDestroyed();
}
}
}
Canvas
public class ScrollCanvas extends Canvas
{
private static final int TOP_LEFT = Graphics.TOP | Graphics.LEFT;
private Image bigImage;
private int imgX;
private int imgY;
public ScrollCanvas()
{
try
{
bigImage = Image.createImage("/images/myImg.png");
}
catch (IOException ioe)
{
// Deal with failure to load image.
}
}
public void paint(Graphics graphics)
{
graphics.setColor(0x000000);
graphics.fillRect(0, 0, getWidth(), getHeight());
graphics.drawImage(bigImage, imgX, imgY, TOP_LEFT);
}
public void keyReleased(int keyCode)
{
int gameKey = getGameAction(keyCode);
if ((gameKey == UP) || (keyCode == KEY_NUM2))
{
imgY++;
}
else if ((gameKey == LEFT) || (keyCode == KEY_NUM4))
{
imgX++;
}
else if ((gameKey == RIGHT) || (keyCode == KEY_NUM6))
{
imgX--;
}
else if ((gameKey == DOWN) || (keyCode == KEY_NUM8))
{
imgY--;
}
repaint();
serviceRepaints();
}
}