【用j2me提供的低级UI:CanvasImage和Graphics对象来绘制九宫格】

2.九宫格布局为主题区和底部菜单区
    3.将一张240*320的图片设置为背景图
    4.根据屏幕宽高计算出各图标的位置,然后绘制各图标。
    具体效果,如图所示:
    核心代码如下:
    public static final String[] filenames = { "/01.png", "/02.png", "/03.png",
    "/04.png", "/05.png", "/06.png" };
    public static final String[] labels = { "公文推送", "通知公告", "日程安排", "通讯录查询",
    "会议室查询", "手机邮箱" };
    public Image[] icons = new Image[9];
    public void paint(Graphics g) {
    int cw = this.getWidth();
    int ch = this.getHeight();
    try {
    // paintHead(g);
    this.setFullScreenMode(true);
    Image img = Image.createImage("/sliderbgn.JPG");
    // g.drawImage(img,0, 0, Graphics.TOP|Graphics.LEFT);
    g.drawImage(img, 0, 0, Graphics.TOP
    | Graphics.LEFT);
    for (int i = 0; i < filenames.length; i++) {
    try {
    icons[i] = Image.createImage(filenames[i]);
    } catch (IOException ex) {
    }
    }
    int old_color = g.getColor();
    g.setColor(0xffffff);
    int startY=ch/4;
    for (int i = 0; i < 2; i++) {
    for (int j = 0; j < 3; j++) {
    g.drawImage(icons[i * 3 + j], j * cw / 3 + 10, startY+i * ch*3 / 10,
    Graphics.TOP | Graphics.LEFT);
    g.drawString(labels[i * 3 + j], j * cw / 3 + 30, startY+i * ch*3 / 10
    + icons[i].getHeight(),
    Graphics.HCENTER | Graphics.TOP);
    }
    }
    g.setColor(old_color);
    paintBottom(g);
    } catch (Exception ex) {
    System.out.println(ex.toString());
    }
    }

更多精彩教程请关注:新番茄花园

posted on 2013-08-13 14:49  挖掘者者者  阅读(170)  评论(0编辑  收藏  举报