站内搜索: 请输入搜索关键词

当前页面: 开发资料首页J2ME 专题局部repaint()到底是怎样工作的?

局部repaint()到底是怎样工作的?

摘要: 局部repaint()到底是怎样工作的?


一个测试例子(其中的一个类):
package MyCalendar;

import javax.microedition.lcdui.*;


public class MyCalendar
extends CustomItem
implements ItemCommandListener {

private final static Command CMD_EDIT = new Command("Select",
Command.ITEM, 1);
private Display display;
private int rows = 6;//定义表格行数
private int cols = 7;//定义表格列数
private int dx = 20;//定义表格宽度
private int dy = 20;//定义表格高度

private int currentX = 0;//当前的行数
private int currentY = 0;//当前的列数
private String[][] data = new String[rows][cols];



public MyCalendar(String title, Display d) {
super(title);
display = d;
setDefaultCommand(CMD_EDIT);
setItemCommandListener(this);
int interactionMode = getInteractionModes();
}


protected int getMinContentHeight() {

return (rows * dy) + 1;
}

protected int getMinContentWidth() {

return (cols * dx) + 1;
}

protected int getPrefContentHeight(int width) {

return (rows * dy) + 1;
}

protected int getPrefContentWidth(int height) {

return (cols * dx) + 1;
}

protected void paint(Graphics g, int w, int h) {

for (int i = 0; i <= rows; i++) {
g.drawLine(0, i * dy, cols * dx, i * dy);
}

for (int i = 0; i <= cols; i++) {
g.drawLine(i * dx, 0, i * dx, rows * dy);
}

int oldColor = g.getColor();
g.setColor(0x00D0D0D0);
//填充灰色方格
g.fillRect((currentX * dx) + 1, (currentY * dy) + 1, dx - 1, dy - 1);
g.setColor(oldColor);

}

protected boolean traverse(int dir, int viewportWidth, int viewportHeight,
int[] visRect_inout) {

switch (dir) {

case Canvas.DOWN:

if (currentY < (rows - 1)) {
currentY++;
repaint(currentX * dx, (currentY - 1) * dy, dx, dy);
repaint(currentX * dx, currentY * dy, dx, dy);
} else
return false;

break;

case Canvas.UP:

if (currentY > 0) {
currentY--;
repaint(currentX * dx, (currentY + 1) * dy, dx, dy);
repaint(currentX * dx, currentY * dy, dx, dy);
} else
return false;

break;

case Canvas.LEFT:

if (currentX > 0) {
currentX--;
repaint((currentX + 1) * dx, currentY * dy, dx, dy);
repaint(currentX * dx, currentY * dy, dx, dy);
}

break;

case Canvas.RIGHT:

if (currentX < (cols - 1)) {
currentX++;
repaint((currentX - 1) * dx, currentY * dy, dx, dy);
repaint(currentX * dx, currentY * dy, dx, dy);
}
}
return true;
}
public void commandAction(Command c, Item i) {
}
}
我有一些疑问:repaint()函数是不是调用paint()函数来实现重绘的?那么局部重绘的话调用paint()函数以后,paint()函数中的画格子线语句不是也执行了?那这个局部重绘有什么意义?还有就是在traverse函数中,为什么在判断按键的时候需要重绘两次?将原来的那个格子再绘一遍?



几乎不需要使用局部重绘,跟全部重绘速度差不多。
如果非要用局部重绘,自己去画象素


可以使用setclip()和repaint()来做!
比如:把屏幕划分成n*m块;
用一个数组来存储是否要重绘标志!
private boolean[][] isRepaint;
for(int i=0;i for(int j=0;j {
if(isRepaint[i][j])
setclip(i*scrW/n,j*scrH/m,scrW/n,scrH/m);
setclip(0,0,scrW,scrH);
}
不过这个方法效率不高!





多谢楼上的


↑返回目录
前一篇: 请教:关于厂商API
后一篇: 使用netbeans5.0开发J2ME项目遇到问题