以文本方式查看主题

-  课外天地 李树青  (http://njcie.com/bbs/index.asp)
--  JavaME移动开发课件  (http://njcie.com/bbs/list.asp?boardid=18)
----  程序代码——带有背景保留功能的拖拽直线  (http://njcie.com/bbs/dispbbs.asp?boardid=18&id=557)

--  作者:admin
--  发布时间:2008/10/8 22:52:21
--  程序代码——带有背景保留功能的拖拽直线

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.midlet.MIDlet;

public class Exec extends MIDlet {
        private Display display;

        public Exec() {
                display = Display.getDisplay(this);
        }

        public void startApp() {
                display.setCurrent(new DrawPanel());
        }

        public void pauseApp() {
        }

        public void destroyApp(boolean unconditional) {
        }
}

class DrawPanel extends Canvas {
        int startX = 0;
        int startY = 0;
        int endX = 0;
        int endY = 0;
        Image image = Image.createImage(getWidth(), getHeight());

        protected void paint(Graphics g) {
                g.drawImage(image, 0, 0, 0);
                g.setColor(0,0,0);
                g.drawLine(startX, startY, endX, endY);
        }

        protected void pointerPressed(int x, int y) {
                startX = x;
                startY = y;
        }

        protected void pointerReleased(int x, int y) {
                endX = x;
                endY = y;              
                Graphics gOfImage = image.getGraphics();
                gOfImage.drawLine(startX, startY, endX, endY);
                repaint();
        }

        protected void pointerDragged(int x, int y) {
                endX = x;
                endY = y;
                repaint();
        }
}

[此贴子已经被作者于2010-12-12 18:35:24编辑过]