博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

J2ME游戏开发之图片绘制

Posted on 2011-08-18 09:37  zhangweia  阅读(509)  评论(0编辑  收藏  举报

1. 方法

      a. 获得图片

        img = Image.createImage(str); //J2ME里面代价比较高

b. 绘制图片

       drawImage(Image image,int left, int top, Graphics.LEFT | Graphics.TOP);

 

c 安装区域绘制:

   drawRegion(Image src, int x_src, int y_src, int width, int height, int transform, int x_dest, int y_dest, int anchor)
    Image src, 图片
    int x_src, 从这个图片的x坐标开始截取
    int y_src,
    int width, 截取宽度
    int height,
    int transform, 旋转方式(0:不旋转,1...   一直到7,你可以自己试下)
    int x_dest, 画到屏幕的位置的x坐标
    int y_dest,
    int anchor Graphics.LEFT|Graphics.TOP

2. code

package com.sliw.graphics;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.xml.rpc.Type;

public class ImageCanvas extends Canvas{

    private int topY = 10;
    private int left = 10;
    private Image image = null;
    private Image bgImage = null;
    
    public ImageCanvas(){
        image = getImage("/8.png");
    }
    
    protected void paint(Graphics g) {
        // TODO Auto-generated method stub
        clear(g);
        
        drawImage(g);
        
        topY = topY + 40;
        
        drawRegion(g);
        
        topY = topY + 80;
        drawDoubleBuffer(g);
    }

    private void clear(Graphics g){
        g.setColor(255,255,255);
        g.fillRect(0, 0, getWidth(), getHeight());
    }
    
    
    void drawDoubleBuffer(Graphics g){
        
        bgImage = Image.createImage(100, 100);
        
        /** 以背景图产生绘制对象,并在图片上绘制2个位图**/
        Graphics offg = bgImage.getGraphics();
        offg.setColor(255,0,0);
        offg.drawRect(0, 0, 90, 90);
        offg.drawImage(image, 0, 0, Graphics.LEFT | Graphics.TOP);        
        offg.drawRegion(image, 0, 0, 24, image.getHeight(), 1, 10, 50, Graphics.LEFT | Graphics.TOP);
        
        //把背景绘制到图片上
        g.drawImage(bgImage, left, topY, Graphics.LEFT | Graphics.TOP);
        
    }
    
    /**
     * drawImage(Image image,int left, int top, Graphics.LEFT | Graphics.TOP);
     * @param g
     */
    private void drawImage(Graphics g){
    
        g.drawImage(image, left, topY, Graphics.LEFT | Graphics.TOP);
    }
    
    
    /**
     * drawRegion(Image src, int x_src, int y_src, int width, int height, int transform, int x_dest, int y_dest, int anchor) 
     *  Image src, 图片
     *  int x_src, 从这个图片的x坐标开始截取
        int y_src, 
        int width, 截取宽度
        int height, 
        int transform, 旋转方式(0:不旋转,1...   一直到7,你可以自己试下)
        int x_dest, 画到屏幕的位置的x坐标
        int y_dest, 
        int anchor Graphics.LEFT|Graphics.TOP
     */
    private void drawRegion(Graphics g){
        g.drawRegion(image, 0, 0, 24, image.getHeight(), 0, left, topY, Graphics.LEFT | Graphics.TOP);
        
        g.drawRegion(image, 0, 0, 24, image.getHeight(), 1, left + 30, topY, Graphics.LEFT | Graphics.TOP);
        
        g.drawRegion(image, 0, 0, 24, image.getHeight(), 2, left + 60, topY, Graphics.LEFT | Graphics.TOP);
    }
    
    /**
     * 载入一张图片
     * @param str 图片路径
     * @return
     */
    public Image getImage(String str){
        Image img=null;
        try {
          img = Image.createImage(str);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
       return img;
    }
    
    
}