WAP之家:为您提供最全最新的WAP技术,CP.SP.3G等行业资讯。 WAP之家交流论坛全新开放 点击进入>>
WAP资讯 | 3G动态 | SP动态 | 运营商动态 | 内容商动态 | 制造商动态 | 论坛讨论>> 每次自动访问
WAP技术 | WAP源码 | 手机编程 | 手机源码 | 无线技术 | J2ME技术 | 手机软件 添加到收藏夹
IVR技术 | SP资料 | SMS MMS技术 | 商业方案 | IVR下载 | 书籍教程 | 工具软件 语言:繁體中文

WAP之家技术文章J2ME技术进阶教程个图形用户界面绘画和事件处理的案例

个图形用户界面绘画和事件处理的案例
作者:j2medev  来源:j2medev.com  发布时间:2006-7-25 8:47:10

    int     fh;             // height of the font
    int     w, h;           // width and height of the canvas
    int     titleHeight;    // Height of the title
    int     pieSize;        // Size of the Pie chart used for width and height
    int     barSize;        // Size of the Bar chart used for width and height
    int     eventHeight;    // Size of the event region
    int     pad;            // Padding used between items

    SampleCanvas() {
        w = getWidth();
        h = getHeight();
        font = Font.getFont(Font.FACE_SYSTEM,
          Font.STYLE_PLAIN, Font.SIZE_SMALL);
        fh = font.getHeight();

        /* Compute the sizes of the bar and pie charts   * It should use all the space except for the title
   * and event regions.
         * Don't let the charts get too small
         */
        pad = 2;
        titleHeight = fh + pad * 2;
        eventHeight = fh * 3;
        barSize = h - (titleHeight + pad(eventHeight + pad);
        if (barSize < 20)               // Don't let them get too small
            barSize = 20;
        if (barSize > (w - pad2)    // Shrink to 1/2 width
            barSize = (w - pad2;
        pieSize = barSize;
    }

    protected void keyPressed(int key) {
        keyCode = key;
        event = "Pressed";
        handleActions(key);
        repaint();
    }

    protected void keyRepeated(int key) {
        keyCode = key;
        event = "Repeated";
        handleActions(key);
        repaint();
    }

    protected void keyReleased(int key) {
        keyCode = key;
        event = "Released";
        repaint();
    }

    protected void pointerPressed(int x, int y) {
        this.x = x;
        this.y = y;
        keyCode = 0;
        event = "Pressed";
        repaint();
    }
    protected void pointerReleased(int x, int y) {
        this.x = x;
        this.y = y;
        keyCode = 0;
        event = "Released";
        repaint();
    }

    protected void pointerDragged(int x, int y) {
        this.x = x;
        this.y = y;
        keyCode = 0;
        event = "Dragged";
    }

    void handleActions(int keyCode) {
        int action = getGameAction(keyCode);
        switch (action) {
            case LEFT:
            x -= 1;
            break;
            case RIGHT:
            x += 1;
            break;
            case UP:
            y -= 1;
            break;
            case DOWN:
            y += 1;
            break;
        }
    }

    protected void paint(Graphics g) {

        g.setFont(font);
        g.setGrayScale(255);
        g.fillRect(00, w, h);

        x = (x < 0? w - : x;
        y = (y < 0? h - : y;
        x = x % w;
        y = y % h;

        // Draw Fill and outline for background of title Text
        int swidth = pad * + font.stringWidth("Pie and Bar Samples");
        int title_x = (w - swidth)/2;

        g.setGrayScale(128);
        g.fillRoundRect(title_x, 0, swidth, fh, 55);
        g.setGrayScale(0);
        g.drawRoundRect(title_x, 0, swidth, fh, 55);

        // Sample Text
        g.setColor(000);
        g.drawString("Pie and Bar Samples"
         title_x + pad, pad, Graphics.TOP|Graphics.LEFT);

  // Translate to below title text
        g.translate(0, titleHeight + pad);

        /*
   * Draw pie chart on the left side
   * using the barSize for width and height
   */
        g.setColor(25500);
        g.fillArc(00, pieSize, pieSize, 45270);
        g.setColor(02550);
        g.fillArc(00, pieSize, pieSize, 045);
        g.setColor(00255);
        g.fillArc(00, pieSize, pieSize, 0, -45);
        g.setColor(0);
        g.drawArc(00, pieSize, pieSize, 0360);

        // Draw Bar chart on right side of the display
        // scale the values to the pieSize maximum value
        int yorig = barSize;
        int h1 = barSize / 3, h2 = barSize / 2, h3 = barSize;
        int avg = (h1 + h2 + h33;

        // Move over to draw Bar chart
        g.translate((w + pad20);

        int bw = pieSize / 7;
        if (bw < 2)
             bw = 2;
        g.setColor(25500);
        g.fillRect(bw*1, yorig-h1, bw+1, h1);
        g.setColor(02550);
        g.fillRect(bw*3, yorig-h2, bw+1, h2);
        g.setColor(00255);
        g.fillRect(bw*5, yorig-h3, bw+1, h3);
        g.setColor(0);
        g.drawRect(bw*1, yorig-h1, bw, h1);
        g.drawRect(bw*3, yorig-h2, bw, h2);
        g.drawRect(bw*5, yorig-h3, bw, h3);

        // Draw axis for bar chart.
        g.setGrayScale(0);
        g.drawLine(000, yorig);
        g.drawLine(0, yorig, barSize, yorig);
        g.setStrokeStyle(Graphics.DOTTED);
        g.drawLine(0, yorig - avg, barSize, yorig-avg);
        g.setStrokeStyle(Graphics.SOLID);

  // Restore to left and move down
        g.translate(-(w + pad2, pieSize + pad);

        // Draw the key and pointer status
        g.setColor(128128128);
        int col1 = font.stringWidth("Action:");
        g.drawString("Key: ",      col1,     0,
         Graphics.TOP|Graphics.RIGHT);
        g.drawString(keyString(keyCode), col1, 0,
         Graphics.TOP|Graphics.LEFT);
        g.drawString("Action:",    col1,     fh,
         Graphics.TOP|Graphics.RIGHT);
        g.drawString(actionString(keyCode), col1, fh,
         Graphics.TOP|Graphics.LEFT);
        g.drawString("Event:",     col1,     fh*2,
         Graphics.TOP|Graphics.RIGHT);
        g.drawString(event,        col1,     fh*2,
         Graphics.TOP|Graphics.LEFT);
        int col2 = 80;
        g.drawString("x:",         col2,     0,
         Graphics.TOP|Graphics.RIGHT);
        g.drawString(Integer.toString(x), col2, 0,
         Graphics.TOP|Graphics.LEFT);
        g.drawString("y:",         col2,     fh,
         Graphics.TOP|Graphics.RIGHT);
        g.drawString(Integer.toString(y), col2, fh,
         Graphics.TOP|Graphics.LEFT);

        // Restore the origin and draw the crosshairs on top
        g.translate(-g.getTranslateX(), -g.getTranslateY());

        g.setColor(000);
        g.drawLine(x, y - 5, x, y + 5);
        g.drawLine(x - 5, y, x + 5, y);
    }
    String keyString(int keyCode) {
        if (keyCode == 0) {
            return "";
        }
        return Integer.toString(keyCode);
    }

    String actionString(int keyCode) {
        if (keyCode == 0) {
            return "";
        }

  int action = getGameAction(keyCode);
  switch (action) {
  case FIRE:
      return "Fire";
  case LEFT:
      return "Left";
  case RIGHT:
      return "Right";
  case DOWN:
      return "Down";
  case UP:
      return "Up";
  case GAME_A:
      return "Game A";
  case GAME_B:
      return "Game B";
  case GAME_C:
      return "Game C";
  case GAME_D:
      return "Game D";
  case 0:
      return "";
  default:
      return Integer.toString(action);
  }
    }
}

上一页  [1] [2] 

[] [返回上一页] [打 印]
文章评论

用户名: 查看更多评论

分 值:100分 85分 70分 55分 40分 25分 10分 0分

内 容:

         (注“”为必填内容。) 验证码: 验证码,看不清楚?请点击刷新验证码