📄 scrollcanvas.java
字号:
package opusmicro.demos.scroll3;
import java.util.Vector;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;
public class ScrollCanvas extends Canvas implements Runnable{
private int position = 0;
Vector children = new Vector();
private int start = 0;
private int width, height;
private ScrollItem item;
private int scrollbarwidth = 6;
private boolean positionchange;
boolean pause = false;
boolean isKeepGoing = true;
boolean isGameA = false;
boolean isGameB = false;
private boolean isPressed;
int gx = -999;
protected byte[] keyCode;
byte index;
public ScrollCanvas(){
width = getWidth();
height = getHeight();
keyCode = new byte[2];
new Thread(this).start();
}
public void addChild(ScrollItem 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){
gx = getGameAction(keyCode);
// gx = keyCode;
start(keyCode);
pause =false;
/* int action = getGameAction(keyCode);
switch(action){
case Canvas.UP:
preposition();
break;
case Canvas.DOWN:
nextposition();
break;
}*/
}
public void keyReleased(int keyCode){
pause = true;
gx = -999;
stop(keyCode);
}
public synchronized void start(int keyCode) {
this.keyCode[index] = (byte) keyCode;
index = (byte) ((index + 1) % this.keyCode.length);
}
public synchronized void stop(int keyCode) {
for ( int i = 0 ; i <= this.keyCode.length - 1 ; i++) {
if ( this.keyCode[i] == (byte) keyCode) {
this.keyCode[i] = 0;
break;
}
}
}
// public synchronized boolean getPressedPoint() {
// isPressed = false;
// for ( int i = 0 ; i <= keyCode.length - 1 ; i++) {
// if ( this.keyCode[i] != 0) {
// isPressed = true;
// break;
// }
// }
// return isPressed;
// }
// 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 += ((ScrollItem) children.elementAt(i)).height;
}
System.out.println("start "+start);
System.out.println("position "+position);
System.out.println("positionStart "+positionStart);
positionEnd = positionStart + ((ScrollItem) 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 -= ((ScrollItem) 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()){
if(isGameA&&isGameB){
System.out.println("combine click"+isGameA +" "+isGameB);
start = 0;position = 0;
for(int i=start;i<children.size()&&y<height;i++){
item = ((ScrollItem)children.elementAt(i));
item.paint(g, x, y, width-scrollbarwidth, 23, i==position);
y += item.height;
}
}
else {
moveStart();
for ( int i = start ; i < children.size() && y < height ; i++) {
item = ((ScrollItem) 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]);
}
public void run() {
while(isKeepGoing){
if(!pause){
byte[] temp = keyCode;
for ( int i = 0 ; i < temp.length ; i++) {
if ( temp[i] != 0) {
switch ( gx) {
case Canvas.LEFT:
case Canvas.UP:
preposition();isGameA=false;isGameB=false;
break;
case Canvas.RIGHT:
case Canvas.DOWN:
nextposition();isGameA=false;isGameB=false;
break;
// case Canvas.GAME_A:
// isGameA = true;
// break;
// case Canvas.GAME_B:
// isGameB = true;
// break;
// default:isGameA=false;isGameB=false;break;
}
if(temp[i]==Canvas.KEY_NUM9){
System.out.println("click 9");
isGameA = true;
}else
if(temp[i]==Canvas.KEY_POUND){
System.out.println("click #");
isGameB = true;
}else{
isGameA=false;isGameB=false;
}
}
}
repaint();
}
try {
Thread.sleep(70);
}
catch (InterruptedException e) {
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -