📄 paintlet.java
字号:
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
import java.util.Vector;
// class Paintlet
public class Paintlet extends MIDlet implements CommandListener
{
private Display paintletDisplay;
private PaintCanvas paintletCanvas;
private final static Command CMD_PENUP = new Command("Pen Up", Command.EXIT, 0);
private final static Command CMD_PENDOWN = new Command("Pen Down", Command.EXIT, 0);
private final static Command CMD_ERASE = new Command("Erase", Command.SCREEN, 1);
private final static Command CMD_EXIT = new Command("Exit", Command.SCREEN, 2);
private final static Command CMD_UNDO = new Command("Undo", Command.SCREEN, 3);
private final static Command CMD_CANCEL = new Command("Cancel", Command.SCREEN, 4);
private final static Command CMD_LINE = new Command("LINE", Command.SCREEN, 5);
private final static Command CMD_RECT = new Command("RECTANGLE", Command.SCREEN, 6);
private final static Command CMD_CIR = new Command("CIRCLE", Command.SCREEN, 7);
public Paintlet()
{
paintletDisplay = Display.getDisplay(this);
paintletCanvas = new PaintCanvas();
paintletCanvas.addCommand(CMD_PENDOWN);
paintletCanvas.addCommand(CMD_EXIT);
paintletCanvas.addCommand(CMD_ERASE);
paintletCanvas.addCommand(CMD_UNDO);
paintletCanvas.addCommand(CMD_LINE);
paintletCanvas.addCommand(CMD_RECT);
paintletCanvas.addCommand(CMD_CIR);
paintletCanvas.setCommandListener(this);
}
protected void destroyApp(boolean unconditional){}
protected void pauseApp(){}
protected void startApp(){
paintletDisplay.setCurrent(paintletCanvas);
}
public void commandAction(Command c, Displayable d)
{
switch (c.getCommandType()){
case Command.SCREEN:
if (c == CMD_ERASE) paintletCanvas.erase();
else if (c == CMD_EXIT) {
paintletCanvas.stopTime_Tick();
destroyApp(false);
notifyDestroyed();
}
else if (c == CMD_CANCEL){
paintletCanvas.penUp = true;
paintletCanvas.removeCommand(CMD_PENUP);
paintletCanvas.removeCommand(CMD_CANCEL);
paintletCanvas.addCommand(CMD_PENDOWN);
paintletCanvas.addCommand(CMD_LINE);
paintletCanvas.addCommand(CMD_RECT);
paintletCanvas.addCommand(CMD_CIR);
paintletCanvas.addCommand(CMD_UNDO);
paintletCanvas.repaint();
}
else if (c == CMD_UNDO) paintletCanvas.undo();
else if (c == CMD_LINE) paintletCanvas.setShapeType('L');
else if (c == CMD_RECT) paintletCanvas.setShapeType('R');
else if (c == CMD_CIR) paintletCanvas.setShapeType('C');
break;
case Command.EXIT:
if (c == CMD_PENUP)
{ paintletCanvas.penUp = true;
paintletCanvas.removeCommand(CMD_PENUP);
paintletCanvas.removeCommand(CMD_CANCEL);
paintletCanvas.addCommand(CMD_PENDOWN);
paintletCanvas.addCommand(CMD_LINE);
paintletCanvas.addCommand(CMD_RECT);
paintletCanvas.addCommand(CMD_CIR);
paintletCanvas.addCommand(CMD_UNDO);
paintletCanvas.addShape();
}
else if (c == CMD_PENDOWN)
{ paintletCanvas.penUp = false;
paintletCanvas.removeCommand(CMD_PENDOWN);
paintletCanvas.addCommand(CMD_PENUP);
paintletCanvas.addCommand(CMD_CANCEL);
paintletCanvas.removeCommand(CMD_LINE);
paintletCanvas.removeCommand(CMD_RECT);
paintletCanvas.removeCommand(CMD_CIR);
paintletCanvas.removeCommand(CMD_UNDO);
paintletCanvas.createShape();
}
break;
}
}
}
// class Point
class Point{
public int xc = 0, yc = 0;
public static final int DIAM = 1;
public Point(int x, int y){
xc = x;
yc = y;
}
public Point(Point p){
xc = p.xc;
yc = p.yc;
}
public void paint(Graphics g){
g.fillRect(xc, yc, DIAM, DIAM);
}
public Point getLoc(){
Point t_Loc = new Point(xc, yc);
return t_Loc;
}
}
// class Cursor
class Cursor extends Point{
public Cursor(int x, int y){
super(x, y);
}
public Cursor(Point p){
super(p);
}
public void paint(Graphics g){
g.drawLine(xc-5, yc, xc+5, yc);
g.drawLine(xc, yc-5, xc, yc+5);
}
}
// class Shape
abstract class Shape {
Point startPoint, endPoint;
Shape(Point t_start, Point t_end){
startPoint = t_start;
endPoint = t_end;
}
public void moveEnd(Point t_end){
endPoint = t_end.getLoc();
}
abstract public void paint(Graphics g);
}
// class Line
class Line extends Shape{
Line(Point t_start, Point t_end){
super(t_start, t_end);
}
public void paint(Graphics g)
{
g.drawLine(startPoint.xc, startPoint.yc, endPoint.xc, endPoint.yc);
}
}
// class Rect
class Rect extends Shape{
Rect(Point t_start, Point t_end){
super(t_start, t_end);
}
public void paint(Graphics g)
{ int x,y, w, h;
if ( endPoint.xc > startPoint.xc){
x = startPoint.xc;
w = endPoint.xc - startPoint.xc;
}
else {
x = endPoint.xc;
w = startPoint.xc - endPoint.xc;
}
if ( endPoint.yc > startPoint.yc){
y = startPoint.yc;
h = endPoint.yc - startPoint.yc;
}
else {
y = endPoint.yc;
h = startPoint.yc - endPoint.yc;
}
g.drawRect(x, y, w, h);
}
}
// class Cir
class Cir extends Shape{
Cir(Point t_start, Point t_end){
super(t_start, t_end);
}
public void paint(Graphics g)
{ int x,y, w, h;
if ( endPoint.xc > startPoint.xc){
x = startPoint.xc;
w = endPoint.xc - startPoint.xc;
}
else {
x = endPoint.xc;
w = startPoint.xc - endPoint.xc;
}
if ( endPoint.yc > startPoint.yc){
y = startPoint.yc;
h = endPoint.yc - startPoint.yc;
}
else {
y = endPoint.yc;
h = startPoint.yc - endPoint.yc;
}
g.drawRoundRect(x, y, w, h, w, h);
}
}
// class PaintCanvas
class PaintCanvas extends Canvas{
public static final int DELTA = 1;
public Point cursor = new Cursor(5, 5);
public boolean penUp = true;
//public Vector points = new Vector();
public Vector ShapeStack = new Vector();
private boolean DirKeyPressing = false;
private char DirKey;
private int holdWait;
Time_Tick time_tick = new Time_Tick();
Shape CurrentShape;
private char ShapeType = 'L';
public void setShapeType(char tType)
{
ShapeType = tType;
}
public void erase(){
ShapeStack.removeAllElements();
repaint();
}
public void paint(Graphics g){
g.setColor(0x00ffffff);
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(255, 0, 0);
cursor.paint(g);
for(int i = 0; i<ShapeStack.size(); i++)
{
Shape next = (Shape)ShapeStack.elementAt(i);
next.paint(g);
}
if (!penUp)
{
CurrentShape.paint(g);
}
}
public void keyPressed(int keyCode){
switch (getGameAction(keyCode)){
case Canvas.UP: // UP key
UP_keyPressed(50);
break;
case Canvas.DOWN: // DOWN key
DOWN_keyPressed(50);
break;
case Canvas.LEFT: // LEFT key
LEFT_keyPressed(50);
break;
case Canvas.RIGHT: // RIGHT key
RIGHT_keyPressed(50);
break;
case 0: //
}
//if(!penUp) points.addElement(new Point(cursor));
repaint();
}
public void UP_keyPressed(int t_holdWait)
{ holdWait = t_holdWait;
DirKey = 'U';
DirKeyPressing = true;
cursor.yc = (cursor.yc < 0) ? getHeight():(cursor.yc - DELTA);
if (!penUp)
{
CurrentShape.moveEnd(cursor);
}
}
public void DOWN_keyPressed(int t_holdWait)
{ holdWait = t_holdWait;
DirKey = 'D';
DirKeyPressing = true;
cursor.yc = (cursor.yc + DELTA) % getHeight();
if (!penUp)
{
CurrentShape.moveEnd(cursor);
}
}
public void LEFT_keyPressed(int t_holdWait)
{ holdWait = t_holdWait;
DirKey = 'L';
DirKeyPressing = true;
cursor.xc = (cursor.xc < 0) ? getWidth():(cursor.xc - DELTA);
if (!penUp)
{
CurrentShape.moveEnd(cursor);
}
}
public void RIGHT_keyPressed(int t_holdWait)
{ holdWait = t_holdWait;
DirKey = 'R';
DirKeyPressing = true;
cursor.xc = (cursor.xc + DELTA) % getWidth();
if (!penUp)
{
CurrentShape.moveEnd(cursor);
}
}
public void keyReleased(int keyCode){
DirKeyPressing = false;
}
public void createShape()
{
switch (ShapeType){
case 'L': CurrentShape = new Line(cursor.getLoc(), cursor.getLoc());
break;
case 'R': CurrentShape = new Rect(cursor.getLoc(), cursor.getLoc());
break;
case 'C': CurrentShape = new Cir(cursor.getLoc(), cursor.getLoc());
break;
}
}
public void addShape()
{
ShapeStack.addElement(CurrentShape);
}
public void undo(){
if(!ShapeStack.isEmpty()){
ShapeStack.removeElementAt(ShapeStack.size()-1);
repaint();
}
}
public void stopTime_Tick()
{
time_tick.stop();
}
class Time_Tick extends Thread
{
boolean running = true;
Time_Tick()
{
super();
start();
}
public void stop()
{
running = false;
}
public void run()
{
while(running)
{
if (DirKeyPressing)
{ if (holdWait !=0)
{
holdWait--;
}
else{
switch (DirKey)
{
case 'U': // UP key
UP_keyPressed(0);
break;
case 'D': // DOWN key
DOWN_keyPressed(0);
break;
case 'L': // LEFT key
LEFT_keyPressed(0);
break;
case 'R': // RIGHT key
RIGHT_keyPressed(0);
break;
}
//if(!penUp) points.addElement(new Point(cursor));
repaint();
}
}
try{Thread.sleep(10);}
catch(InterruptedException e){}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -