CSDN博客

img cuipengfei1

对Graphic的应用,实现类似九宫格的绘制字符串、制定圆心,半径绘制圆形、用火柴棍拼字[Java ME]

发表于2008/10/3 10:26:00  1075人阅读

 

 

 

 

  1. import javax.microedition.midlet.*;
  2. import javax.microedition.lcdui.*;
  3. public class HomeWork extends MIDlet implements CommandListener {
  4.     
  5.     Display d=Display.getDisplay(this);
  6.     
  7.     Form f=new Form("主窗口");
  8.     
  9.     Command back=new Command("返回",Command.BACK,1);
  10.      Command exit=new Command("退出",Command.EXIT,1);
  11.     Command drs=new Command("绘制字符串",Command.OK,1);
  12.     Command drc=new Command("画圆",Command.OK,1);
  13.     Command drn=new Command("火柴棍拼字",Command.OK,1);
  14.    
  15.     //用于画圆的
  16.     public class CircleCanvas extends Canvas
  17.     {
  18.         int pointX,pointY,len;
  19.         public void paint(Graphics g)
  20.         {
  21.             g.setColor(25500);
  22.             g.fillArc(pointX, pointY, len, len, 0360);
  23.            
  24.             g.drawRect(pointX, pointY, len, len);//框住了就代表对了
  25.         }
  26.         public void drawCircle(int x,int y,int r)
  27.         {
  28.             pointX=x-r;
  29.             pointY=y-r;
  30.             len=2*r;
  31.         }
  32.     }
  33.     
  34.     //用于在指定位置绘制字符串
  35.     public class MyCanvas extends Canvas
  36.     {
  37.         String printword="";
  38.         int[] myenum={    00, Graphics.LEFT|Graphics.TOP, 
  39.                         getWidth()/2,0,Graphics.HCENTER|Graphics.TOP,
  40.                         getWidth(), 0, Graphics.RIGHT|Graphics.TOP,
  41.                         
  42.                         0, getHeight()/2, Graphics.LEFT|Graphics.TOP
  43.                         , getWidth()/2,getHeight()/2,  Graphics.HCENTER|Graphics.TOP
  44.                         , getWidth(), getHeight()/2, Graphics.RIGHT|Graphics.TOP
  45.                         
  46.                         , 0, getHeight(), Graphics.LEFT|Graphics.BOTTOM
  47.                         , getWidth()/2,getHeight(),  Graphics.HCENTER|Graphics.BOTTOM
  48.                         , getWidth(), getHeight(), Graphics.RIGHT|Graphics.BOTTOM
  49.         };
  50.         int[] position=new int[3];
  51.        public void paint(Graphics g)
  52.        { 
  53.            g.setColor(25500);
  54.            g.drawString(printword,position[0],position[1],position[2]);
  55.        }
  56.          public void DrawMyStrin(String s,int pos)
  57.          {
  58.              printword=s;
  59.              position[0]=myenum[pos*3-3];
  60.              position[1]=myenum[pos*3-2];
  61.              position[2]=myenum[pos*3-1];
  62.          }
  63.        } 
  64.     
  65.     //用火柴棍拼字
  66.     public class MatchNum extends Canvas
  67.   {
  68.         int[][] lines={
  69.         {100,100,120,100},//0
  70.         {100,120,120,120},//1
  71.         {100,140,120,140},//2 //三个横
  72.         
  73.         {100,100,100,120},//3
  74.         {100,120,100,140},//4//左边的两竖
  75.         
  76.         {120,100,120,120},//5
  77.         {120,120,120,140}};//6//右边的两竖
  78.         
  79.         int[][] nums={
  80.         {0,2,3,4,5,6},//0
  81.         {5,6},//1
  82.         {0,1,2,5,4},//2
  83.         {0,1,2,5,6},//3
  84.         {5,6,1,3},//4
  85.         {0,1,2,3,6},//5
  86.         {3,4,0,1,2,6},//6
  87.         {0,5,6},//7
  88.         {0,1,2,3,4,5,6},//8
  89.         {0,1,3,5,6,2},//9
  90.         };
  91.         int[] temp;
  92.      public void drawMyNum(int n)
  93.      {
  94.          temp=nums[n];
  95.      }
  96.      public void paint(Graphics g)
  97.      {g.setColor(255,0,0);
  98.       int i=temp.length;
  99.       for(int j=0;j<=i-1;j++)
  100.       {
  101.           int[] a=lines[temp[j]];
  102.           g.drawLine(a[0],a[1],a[2],a[3]);
  103.       }
  104.         
  105.      }
  106.   }
  107.    
  108.     
  109.     public void startApp() {
  110.             f.addCommand(exit);
  111.             f.addCommand(drs);
  112.             f.addCommand(drc);
  113.             f.addCommand(drn);
  114.             f.setCommandListener(this);
  115.             d.setCurrent(f);
  116.     }
  117.     
  118.     public void commandAction(Command c,Displayable dis)
  119.     {
  120.         if(c==exit)
  121.            destroyApp(false); 
  122.         
  123.         else if(c==drs)
  124.         {   MyCanvas m=new MyCanvas();
  125.             m.addCommand(back);
  126.             m.setCommandListener(this);
  127.             m.DrawMyStrin("字符串"new java.util.Random().nextInt(8)+1);
  128.             d.setCurrent(m);}
  129.         
  130.         else if(c==drc)
  131.         {
  132.             CircleCanvas cc=new CircleCanvas();
  133.             cc.addCommand(back);
  134.             cc.setCommandListener(this);
  135.             cc.drawCircle(606040);
  136.             d.setCurrent(cc);
  137.         }
  138.         
  139.         else if(c==drn)
  140.         {
  141.             MatchNum mn=new MatchNum();
  142.             mn.addCommand(back);
  143.             mn.setCommandListener(this);
  144.             mn.drawMyNum(new java.util.Random().nextInt(9));
  145.             d.setCurrent(mn);
  146.         }
  147.         
  148.         else if(c==back)
  149.         {
  150.             d.setCurrent(f);
  151.         }
  152.     }
  153.     public void pauseApp() {
  154.     }
  155.     public void destroyApp(boolean unconditional) {
  156.         notifyDestroyed();
  157.     }   
  158. }
0 0

相关博文

我的热门文章

img
取 消
img