使用指针事件在Canvas上绘画

王朝other·作者佚名  2008-05-31
窄屏简体版  字體: |||超大  

import Javax.microedition.midlet.*;

import javax.microedition.lcdui.*;

public class Doodle extends MIDlet

{

private Display display; // The display

private DoodleCanvas canvas; // Canvas

public Doodle()

{

display = Display.getDisplay(this);

canvas = new DoodleCanvas(this);

}

protected void startApp()

{

display.setCurrent( canvas );

}

protected void pauseApp()

{ }

protected void destroyApp( boolean unconditional )

{ }

public void exitMIDlet()

{

destroyApp(true);

notifyDestroyed();

}

}

/*--------------------------------------------------

* Class DoodleCanvas

*

* Pointer event handling

*-------------------------------------------------*/

class DoodleCanvas extends Canvas implements CommandListener

{

private Command cmExit; // Exit midlet

private Command cmClear; // Clear display

private int startx = 0, // Where pointer was clicked

starty = 0,

currentx = 0, // Current location

currenty = 0;

private Doodle midlet;

private boolean clearDisplay = false;

/*--------------------------------------------------

* ConstrUCtor

*-------------------------------------------------*/

public DoodleCanvas(Doodle midlet)

{

this.midlet = midlet;

// Create exit command & listen for events

cmExit = new Command("Exit", Command.EXIT, 1);

cmClear = new Command("Clear", Command.SCREEN, 1);

addCommand(cmExit);

addCommand(cmClear);

setCommandListener(this);

}

/*--------------------------------------------------

* Paint the text representing the key code

*-------------------------------------------------*/

protected void paint(Graphics g)

{

// Clear the background (to white)

if (clearDisplay)

{

g.setColor(255, 255, 255);

g.fillRect(0, 0, getWidth(), getHeight());

clearDisplay = false;

startx = currentx = starty = currenty = 0;

return;

}

// Draw with black pen

g.setColor(0, 0, 0);

// Draw line

g.drawLine(startx, starty, currentx, currenty);

// New starting point is the current position

startx = currentx;

starty = currenty;

}

/*--------------------------------------------------

* Command event handling

*-------------------------------------------------*/

public void commandAction(Command c, Displayable d)

{

if (c == cmExit)

midlet.exitMIDlet();

else if (c == cmClear)

{

clearDisplay = true;

repaint();

}

}

/*--------------------------------------------------

* Pointer pressed

*-------------------------------------------------*/

protected void pointerPressed(int x, int y)

{

startx = x;

starty = y;

}

/*--------------------------------------------------

* Pointer moved

*-------------------------------------------------*/

protected void pointerDragged(int x, int y)

{

currentx = x;

currenty = y;

repaint();

}

}

(出处:http://www.knowsky.com)

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航