TA的每日心情 | 开心 2021-3-12 23:18 |
---|
签到天数: 2 天 [LV.1]初来乍到
|
- 一、MistifyMIDlet.java
- import javax.microedition.lcdui.Display;
- import javax.microedition.midlet.MIDlet;
- import javax.microedition.midlet.MIDletStateChangeException;
- /**
- * 太好看了。这是显示有点象windows屏幕保护一样的东西。
- * @author fb
- *
- */
- public class MistifyMIDlet extends MIDlet {
- private Display display;
-
- public MistifyMIDlet() {
- display=Display.getDisplay(this);
- }
-
- public void startApp() {
- MistifyCanvas t=new MistifyCanvas();
- display.setCurrent(t);
- }
-
- public void pauseApp() {
- }
-
- public void destroyApp(boolean unconditional) {
- }
- }
复制代码
二、MistifyCanvas.java
- import java.util.*;
- import javax.microedition.lcdui.*;
- public class MistifyCanvas extends Canvas {
- Random rnd=new Random();
- private Timer tt=new Timer();
- private MistShape ms[]=new MistShape[2];
- private MistColor mc=new MistColor(2,3,4);
- int k=10;
-
- public MistifyCanvas() {
- super();
- ms[0]=new MistShape(new Rectangle(0,0,super.getWidth(),super.getHeight()));
- ms[1]=ms[0].Copy();
- tt.scheduleAtFixedRate(new MyTask(this),100,100);
- }
-
- public void paint(Graphics g) {
- try {
- if (k==0) {
- g.setColor(255,255,255);
- ms[1].Paint(g);
- }
- g.setColor(mc.ColorRed(),mc.ColorGreen(),mc.ColorBlue());
- ms[0].Paint(g);
- }
- catch (Exception e) {
- g.drawString(e.toString(),10,10,0);
- }
- }
-
- protected void keyPressed(int keyCode) {
- }
-
- protected void keyReleased(int keyCode) {
- }
-
- protected void keyRepeated(int keyCode) {
- }
-
- protected void pointerDragged(int x, int y) {
- }
-
- protected void pointerPressed(int x, int y) {
- }
-
- protected void pointerReleased(int x, int y) {
- }
-
- private class MyTask extends TimerTask
- {
- private MistifyCanvas m;
-
- public MyTask(MistifyCanvas mistify) {
- m=mistify;
- }
-
- public void run() {
- ms[0].Delocate();
- if (k==0)
- ms[1].Delocate();
- else
- k--;
-
- mc.Delocate();
- m.repaint();
- }
-
- }
-
- private class MistShape {
- private MistPoint pt[]=new MistPoint[3];
-
- public MistShape(Rectangle rect) {
- pt[0]=new MistPoint(rect,1,-2);
- pt[1]=new MistPoint(rect,2,-3);
- pt[2]=new MistPoint(rect,3,-1);
- }
- public MistShape(MistPoint mp0,MistPoint mp1,MistPoint mp2) {
- pt[0]=mp0;
- pt[1]=mp1;
- pt[2]=mp2;
- }
-
- public void Delocate() {
- pt[0].Delocate();
- pt[1].Delocate();
- pt[2].Delocate();
- }
-
- public void Paint(Graphics g) {
- g.drawLine(pt[0].Left(),pt[0].Top(),pt[1].Left(),pt[1].Top());
- g.drawLine(pt[1].Left(),pt[1].Top(),pt[2].Left(),pt[2].Top());
- g.drawLine(pt[2].Left(),pt[2].Top(),pt[0].Left(),pt[0].Top());
- }
-
- public MistShape Copy() {
- return new MistShape(pt[0].Copy(),pt[1].Copy(),pt[2].Copy());
- }
- }
- private class MistPoint {
- private int x,y,x0,y0;
- private Rectangle r;
-
- public MistPoint(Rectangle rect,int deltaX,int deltaY) {
- r=rect;
- x0=deltaX;
- y0=deltaY;
- x=Math.abs(rnd.nextInt()) % (r.Right()-r.Left()) + r.Left();
- y=Math.abs(rnd.nextInt()) % (r.Bottom()-r.Top()) + r.Top();
- }
- public MistPoint(Rectangle rect,int left,int top,int deltaX,int deltaY) {
- r=rect;
- x=left;
- y=top;
- x0=deltaX;
- y0=deltaY;
- }
-
- public int Left() { return x; }
- public int Top() { return y; }
-
- public void Delocate() {
- if (x+x0< r.Left() || x+x0>r.Right()) x0=-x0;
- if (y+y0< r.Top() || y+y0>r.Bottom()) y0=-y0;
- x+=x0;
- y+=y0;
- }
- public MistPoint Copy() {
- return new MistPoint(r,x,y,x0,y0);
- }
- }
- public class MistColor {
- private int r,g,b,r0,g0,b0;
-
- public MistColor(int deltaR,int deltaG, int deltaB) {
- this.r0=deltaR;
- this.g0=deltaG;
- this.b0=deltaB;
- }
-
- public int ColorRed() { return r; }
- public int ColorGreen() { return g; }
- public int ColorBlue() { return b; }
-
- public void Delocate() {
- if (r+r0<0 || r+r0>255) r0=-r0;
- if (g+g0<0 || g+g0>255) g0=-g0;
- if (b+b0<0 || b+b0>255) b0=-b0;
- r+=r0;
- g+=g0;
- b+=b0;
- }
- }
-
- public class Rectangle {
- private int x0,y0,x1,y1;
-
- public Rectangle(int x0,int y0,int x1,int y1) {
- this.x0=x0;
- this.y0=y0;
- this.x1=x1;
- this.y1=y1;
- }
-
- public int Left() { return x0; }
- public int Top() { return y0; }
- public int Right() { return x1; }
- public int Bottom() { return y1; }
- }
- }
复制代码 三、运行图:
源码下载:http://203.93.208.26/kj/cwb/dir7/J2MEMistify.zip |
|