scrollmidlet.java.svn-base
来自「example2 众多JAVA实例源码...学习java基础的好帮手」· SVN-BASE 代码 · 共 142 行
SVN-BASE
142 行
package opusmicro.demos.scalllist;
import java.util.Vector;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
public class ScrollMIDlet extends MIDlet {
public ScrollMIDlet() {}
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {}
protected void pauseApp() {}
protected void startApp() throws MIDletStateChangeException {
ScrollCanvas sc = new ScrollCanvas();
for(int i=0;i<25;i++){
Item si = new Item("ITEM "+i);
// si.height = 30 + i*2;
sc.addChild(si);
}
Display.getDisplay(this).setCurrent(sc);
}
}
class Item {
private String text;
int height=26;
public Item(String text){
this.text = text;
}
public void paint(Graphics g, int x, int y, int w, int h, boolean focus){
if(focus){
g.setColor(0xffff);
}else{
g.setColor(0x10ff);
}
// g.fillRect(x, y, w, height);
g.fillRect(x, y, w, h);
if(text!=null){
g.setColor(0);
g.drawString(text, x+5, (y+Font.getDefaultFont().getHeight()/2), g.LEFT|g.TOP);
}
}
}
class ScrollCanvas extends Canvas{
private int position = 0;
Vector children = new Vector();
private int start = 0;
private int width, height;
private Item item;
private int scrollbarwidth = 6;
private boolean positionchange;
public ScrollCanvas(){
width = getWidth();
height = getHeight();
}
public void addChild(Item item){
if(!children.contains(item)){
children.addElement(item);
}
}
void preposition(){
if(position>0){
position--;
}
positionchange = true;
repaint();
serviceRepaints();
}
void nextposition(){
if(position<children.size()-1){
position++;
}
positionchange = true;
repaint();
serviceRepaints();
}
public void keyPressed(int keyCode){
int action = getGameAction(keyCode);
switch(action){
case Canvas.UP:
preposition();
break;
case Canvas.DOWN:
nextposition();
break;
}
}
public void keyRepeated(int keyCode){
keyPressed(keyCode);
}
public void moveStart(){
if ( positionchange) {
positionchange = false;
int positionStart = 0;
int positionEnd = 0;
for ( int i = start, stop = children.size() ; i < stop && i < position ; i++) {
positionStart += ((Item) children.elementAt(i)).height;
}
System.out.println("start "+start);
System.out.println("position "+position);
System.out.println("positionStart "+positionStart);
positionEnd = positionStart + ((Item) children.elementAt(position)).height;
if ( positionStart <= 0) {
start = position;
}
else if ( positionEnd > height) {
for ( int i = start ; i <= position && i < children.size() && positionEnd > height ; i++) {
start++;
positionEnd -= ((Item) children.elementAt(i)).height;
}
}
}
}
protected void paint(Graphics g) {
int x=0, y=0;
g.setColor(0x00aa12);
g.fillRect(0, 0, width, height);
int[] clip = {g.getClipX(),g.getClipY(),g.getClipWidth(),g.getClipHeight()};
g.setClip(x, y, width, height);
if(!children.isEmpty()){
moveStart();
for(int i=start;i<children.size()&&y<height;i++){
item = ((Item)children.elementAt(i));
item.paint(g, x, y, width-scrollbarwidth, 23, i==position);
y += item.height;
}
if(start>0 || y>height){
double scrollbarheight = Math.floor((height+.5)/children.size());
int scrollbary = (int) Math.floor(scrollbarheight*position+.5);
g.setColor(0);
g.fillRect(width-scrollbarwidth, scrollbary, scrollbarwidth, (int)scrollbarheight);
}
}
g.setClip(clip[0], clip[1], clip[2], clip[3]);
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?