如何通过canvas在j2me中为Table编写Scrollable?
现在我尝试用 Canvas 绘制表格,但我有一个问题,我的表格不能滚动。
我怎样才能编写一些代码来帮助我的表格可以滚动?
我的代码在这里
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;
/**
*
* @author Kency
*/
public class TableCanvas extends Canvas{
private int w,h;
private int pad;
private int cols = 3;
private int rows = 10;
public TableCanvas() {
w = getWidth();
h = getHeight();
}
protected void paint(Graphics g) {
g.setColor(148, 178, 255);
g.fillRect(0, 0, w, h);
for(int i =0 ; i <= cols ; i++){
g.setColor(0x00D0D0D0);
for(int j = 0 ; j <= rows ; j++){
g.drawLine(0, j * h/rows, cols * w, j* h/rows);
g.drawLine(i * w/cols, 0, i * w/cols, w * rows);
}
}
}
}
Now i try to draw a table by Canvas, but i have a problem, my table doesnt has scrollable.
How can i write some code to help my table can scrollable?
My Code here
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;
/**
*
* @author Kency
*/
public class TableCanvas extends Canvas{
private int w,h;
private int pad;
private int cols = 3;
private int rows = 10;
public TableCanvas() {
w = getWidth();
h = getHeight();
}
protected void paint(Graphics g) {
g.setColor(148, 178, 255);
g.fillRect(0, 0, w, h);
for(int i =0 ; i <= cols ; i++){
g.setColor(0x00D0D0D0);
for(int j = 0 ; j <= rows ; j++){
g.drawLine(0, j * h/rows, cols * w, j* h/rows);
g.drawLine(i * w/cols, 0, i * w/cols, w * rows);
}
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于摘录中的代码,最直接的方法可能是使用 Graphics#translate API。
还必须处理按键/指针事件以允许用户滚动。例如,当按键对应于游戏动作右/左/上/下时,您分别滚动。当拖动指针时,您会找出方向,然后再次分别滚动
绘制滚动条也需要“手工”代码。
另一种选择是使用 3rd 方库,例如 LWUIT 或 J2ME Polish
For code like in your excerpt, most straightforward way would probably be with Graphics#translate API.
One would also have to handle key pressed / pointer events to allow user to scroll. Eg when key press corresponds to game action right/left/up/down, you scroll respectively. When pointer is dragged, you find out the direction and, again, scroll respectively
Painting scrollbar(s) would also require "handmade" code.
Another option would be using 3rd party library like LWUIT or J2ME Polish