Horizontal Scroll Bar Implementation On J2ME

[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

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();
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值