滚动实例在们的
JavaMe坐标变更的功能怎么样实现。当有一定的相应的事件触发的时候,我们可以实现让其显示相关的页面,并且可以用滚动条滚动到相应的页面。
【JavaMe实现代码】
ShowHelp.java
package com.token.view;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.game.GameCanvas;
import com.token.util.StringDealMethod;
import com.token.util.UIController;
import com.token.view.components.*;
public class ShowHelp extends GameCanvas
{
private UIController controller;
private Graphics graphics;
private Font ft;
private int width;
private int height;
private Menu menu;
private Head head;
private BackGroud backGroud;
private int page = 0;
private int currentPageIndex = 0;
private int bodyHeight;
private int dir = 0;
public ShowHelp(UIController control)
{
super(false);
this.controller=control;
setFullScreenMode(true);
width = getWidth();
height = getHeight();
menu = new Menu(this);
head = new Head(this);
backGroud = new BackGroud(this);
}
public void show()
{
int margin = 0;
graphics = getGraphics();
graphics.clipRect(0,0, width, height);
backGroud.drawBackGroud(this, graphics);
head.drawHead(this, graphics, "帮助");
menu.drawMenu(this, graphics, "","返回");
//flushGraphics();
ft = Font.getFont(Font.FACE_PROPORTIONAL,Font.STYLE_BOLD,Font.SIZE_MEDIUM);
String info = "1 滚动分页显示;\n"
+"2 滚动分页显示;\n"
+"3 滚动分页显示;\n"
...
+"34 滚动分页显示;\n";
String info_wrap1[] = StringDealMethod.format(info, width-15, ft);
page = info_wrap1.length*ft.getHeight()/(height-head.menuHeight-menu.menuHeight-2*margin)+1;
bodyHeight = ((int) (height-head.menuHeight-menu.menuHeight)/ft.getHeight())*ft.getHeight();
margin = (height-head.menuHeight-menu.menuHeight-bodyHeight)/2;
graphics.setFont(ft);
graphics.setColor(Color.text);
graphics.clipRect(0, head.menuHeight+margin, width, bodyHeight);
graphics.translate(0, dir*currentPageIndex*bodyHeight);
for(int i=0; i
{
graphics.drawString(info_wrap1[i],5, i * ft.getHeight()+head.menuHeight+margin, Graphics.TOP|Graphics.LEFT);
}
graphics.translate(0, -dir*currentPageIndex*bodyHeight);
drawScrollBar();
flushGraphics();
//System.out.println(graphics.getTranslateY());
}
private void drawScrollBar()
{
int barHeight = height-head.menuHeight-menu.menuHeight;
graphics.setColor(Color.menuFrame);
graphics.fillRect(width-3, head.menuHeight, 2, barHeight);
graphics.setColor(Color.selectBg);
graphics.fillRect(width-4, head.menuHeight+(currentPageIndex)*barHeight/page, 4, barHeight/page);
}
protected void keyPressed(int keyCode)
{
//System.out.println(keycode);
switch(keyCode)
{
case KeyID.SOFT_RIGHT:
{
String flag = "0";
Object [] args = {flag,""};
controller.handleEvent(UIController.EventID.EVENT_MAIN_SCREEN,args);
break;
}
default:
;
}
keyCode = getGameAction(keyCode);
//System.out.println(page);
switch(keyCode)
{
case UP:
{
dir = -1;
if(currentPageIndex>0)
{
currentPageIndex--;
}
else
{
//dir = 0;
}
show();
break;
}
case DOWN:
{
dir = -1;
if(currentPageIndex
{
currentPageIndex++;
}
else
{
//dir = 0;
}
show();
break;
}
}
}
}
广州