⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cimage.java

📁 J2ME飞机设计游戏,希望对学习J2ME的程序员有所帮助
💻 JAVA
字号:
/*
 * CImage.java
 *
 * Created on 2007年1月16日, 上午11:29
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */
import javax.microedition.lcdui.*;

//import com.nokia.mid.ui.DirectGraphics;
//import com.nokia.mid.ui.DirectUtils;
/**
 *
 * @author wangquan
 */
public class CImage {
    
    /** Creates a new instance of CImage */
    public CImage(String fileName)
    {
        try{
            image = Image.createImage(fileName);
            shortWidth = (short)image.getWidth();
            shortHeight = (short)image.getHeight();
        }catch(Exception e){
            e.printStackTrace();
        }
    }

    public CImage(String fileName, int clipwidth, int clipheight,int cols,
			int rows) {
		try {
			shortWidth = (short) clipwidth;
			shortHeight = (short) clipheight;
			image = Image.createImage(fileName);
			this.cols = cols;
			this.rows = rows;
			// offx = cols * clipwidth;
			// offy = rows * clipheight;
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
    
    public CImage(CImage cimage, int cols, int rows, int clipwidth, int clipheight)
    {
        shortWidth = (short)clipwidth;
        shortHeight = (short)clipheight;
        image = cimage.image;
        offx = cols * clipwidth;
        offy = rows * clipheight;
    }
    
    
    public CImage[] getClipImages(int tileWidth, int tileHeight) {
		int cols = shortWidth / tileWidth;
		int rows = shortHeight / tileHeight;

		CImage cimage[] = new CImage[rows * cols];
		int index = 0;
		for (int i = 0; i < rows; i++) {
			for (int j = 0; j < cols; j++)
				cimage[index++] = new CImage(this, j, i, tileWidth, tileHeight);
		}
		return cimage;
	}
   
   public void drawGetClipImage(Graphics g, int x, int y,int anchor){
       g.setClip(x, y, shortWidth, shortHeight);
       g.drawImage(image, x - offx, y - offy, anchor);
       g.setClip(0,0,GameConstant.iSCREEN_WIDTH,GameConstant.iSCREEN_HEIGHT);
   }

//    /**
//     * 
//     * @param g 画笔
//     * @param x 描绘位置x
//     * @param y 描绘位置y
//     * @param anchor 锚点
//     */
//    public void drawClipImage(Graphics g, int x, int y, int anchor) {
//		g.setClip(x, y, shortWidth, shortHeight);
//		g.drawImage(image, x - offx, y - offy, anchor);
//		g.setClip(x, y, GameConstant.iSCREEN_WIDTH, GameConstant.iSCREEN_HEIGHT);
//	}
    
    /**
     * 
     * @param g 画笔
     * @param x 描绘位置x
     * @param y 描绘位置y
     * @param anchor 锚点
     */
    public void drawClipImage(Graphics g, int x, int y, int anchor) {
    	
		g.setClip(x, y, shortWidth, shortHeight);
		g.drawImage(image, x - cCount * shortWidth, y - rCount * shortHeight,
				anchor);
		g.setClip(0, 0, GameConstant.iSCREEN_WIDTH,
						GameConstant.iSCREEN_HEIGHT);
		
		// 如果x轴上分割的图片
		if (cols != 0) {
			cCount++;
			cCount = cCount % cols;
		}

		// 如果y轴上有分割的图片
		if (rows != 0) {
			rCount++;
			rCount = rCount % rows;
		}
		
		g.setClip(0, 0, GameConstant.iSCREEN_WIDTH, GameConstant.iSCREEN_HEIGHT);
	}

    /** 一般性图片绘制 */
    public void drawImage(Graphics g, int x, int y,int anchor)
    {
        g.drawImage(image, x, y, anchor);
    }
    
    /**需要翻转的图片绘制,MIDP2.0*/
//    public void drawImage(Graphics g,int offx,int offy, int width,int height,int transform,int x, int y,int anchor)
//    {
//        //g.drawRegion(image, offx, offy, width,height,transform,x,y, anchor);
//    	//DirectUtils.getDirectGraphics(g).
//    	int off_width = 0;
//        int off_height = 0;
//        //int nokia_anchor = 0;
//    	
//    	switch (transform) {
//		case DirectGraphics.FLIP_HORIZONTAL: // 镜象
//			g.setClip(x, y - (height >> 1), width, height);
//			break;
//		case 0: // 不翻转
//			g.setClip(x, y, width, height);
//			off_width = offx;
//			off_height = offy;
//			
//			transform = 0;
//			break;
//		case 5: // 顺时针90度
//			g.setClip(x, y, height, width);
//			off_width = shortHeight - offy - height;
//			off_height = offx;
//			transform = 270;
//			break;
//		case 3: // 顺时针180度
//			g.setClip(x, y, width, height);
//			off_width = shortWidth - offx - width;
//			off_height = shortHeight - offy - height;
//			
//			transform = 180;
//			break;
//		case 6: // 顺时针270度
//			g.setClip(x, y, height, width);
//			off_width = offy;
//			off_height = shortWidth - offx - width;
//			
//			transform = 90;
//			break;
//		}
//
//    	DirectUtils.getDirectGraphics(g).drawImage(image,x - off_width,y - off_height,anchor,transform);
//    	g.setClip(0,0,GameConstant.iSCREEN_WIDTH,GameConstant.iSCREEN_HEIGHT);
//    }
    
    public void drawRegionImage(Graphics g, int offx, int offy, int offWidth,
			int offHeight, int transform, int x, int y, int anchor) {
		g.setClip(0, 0, GameConstant.iSCREEN_WIDTH,
						GameConstant.iSCREEN_HEIGHT);

		g.drawRegion(image, offx, offy, offWidth, offHeight, transform, x, y,
				anchor);

		g.setClip(0, 0, GameConstant.iSCREEN_WIDTH,
						GameConstant.iSCREEN_HEIGHT);
	}
    
    public void drawClipImage(Graphics g, int offx, int offy, int width,
			int height, int x, int y, int anchor) {
		g.setClip(x, y, width, height);
		g.drawImage(image, x - offx, y - offy, anchor);
		g.setClip(0, 0, GameConstant.iSCREEN_WIDTH,
						GameConstant.iSCREEN_HEIGHT);
	}
    
    public void clean(){
    	image = null;
    }

    private Image image;
    private short shortWidth = 0;
    private short shortHeight = 0;
    private int cols;       //图片分割后几列
    private int rows;       //图片分割后几排
    private int cCount = 0; //分割的图片列记数
    private int rCount = 0; //分割的图片排记数
    private int offx;       //便移的X距离
    private int offy;       //便移的Y距离
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -