📄 graphiccanvas.java
字号:
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;
/**
* 用于绘制图形的画布
*/
class GraphicCanvas extends Canvas {
final static int LINE = 0;
final static int RECTANGL_NO_FILL = 1;
final static int RECTANGL_FILL = 2;
final static int ARC_NO_FILL = 3;
final static int ARC_FILL = 4;
final static int ROUNDRECTANGLE_NO_FILL = 5;
final static int ROUNDRECTANGLE_FILL = 6;
final static int TRIANGLE_FILL = 7;
//指针的起始位置
private int startX, startY;
//指针的新位置
private int newX, newY;
//显示是否为拖动模式
private boolean isDragMode;
//当前绘图模式
private int mode;
/**
* 在这个方法中绘制屏幕
*/
protected void paint(Graphics g) {
//清除屏幕 - 背景黑色
g.setColor(0x000000);
g.fillRect(0, 0, this.getWidth(), this.getHeight());
//设置绘图颜色为白色
g.setColor(0xFFFFFF);
if (isDragMode) {
//用虚线绘制
g.setStrokeStyle(Graphics.DOTTED);
} else {
//用实线绘制
g.setStrokeStyle(Graphics.SOLID);
}
switch(mode) {
case LINE:
g.drawLine(startX, startY, newX, newY);
break;
case RECTANGL_NO_FILL:
g.drawRect(startX, startY, newX-startX, newY-startY);
break;
case RECTANGL_FILL:
g.fillRect(startX, startY, newX-startX, newY-startY);
break;
case ARC_NO_FILL:
g.drawArc(startX, startY,
newX-startX, newY-startY, 0, 180);
break;
case ARC_FILL:
g.fillArc(startX, startY,
newX-startX, newY-startY, 0, 180);
break;
case ROUNDRECTANGLE_NO_FILL:
g.drawRoundRect(startX, startY,
newX-startX, newY-startY, 10, 10);
break;
case ROUNDRECTANGLE_FILL:
g.fillRoundRect(startX, startY,
newX-startX, newY-startY, 10, 10);
break;
case TRIANGLE_FILL:
g.fillTriangle(startX, startY,
newX, newY, newX, newY+50);
break;
default:
break;
}
}
/**
* 指针按下时触发
*/
protected void pointerPressed(int x, int y) {
startX = x;
startY = y;
isDragMode = true;
}
/**
* 指针拖动时触发
*/
protected void pointerDragged(int x, int y) {
newX = x;
newY = y;
repaint();
}
/**
* 指针释放时触发
*/
protected void pointerReleased(int x, int y) {
isDragMode = false;
newX = x;
newY = y;
repaint();
}
void draw(int mode) {
this.mode = mode;
repaint();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -