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

📄 testcanvas.java

📁 <j2me 开发精解> 詹建光著 里所有的源码。对J2me的开发相当有帮助
💻 JAVA
字号:
package com.j2medev.chapter8.m3g;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;
import javax.microedition.m3g.*;
import javax.microedition.lcdui.Image;

public class TestCanvas extends Canvas implements Runnable{
    
    private Camera camera;	        //摄影机
    private Transform camera_Tran;      //摄影机矩阵
    
    private Light light;		//光线
    private Transform light_Tran;       //光线矩阵
    
    private Mesh arrow;			//箭
    private Transform arrow_Tran;	//箭矩阵
    
    private Mesh box;			//靶子
    private Transform box_Tran;		//靶子矩阵
    
    private Mesh floor;			//地板
    private Transform floor_Tran;	//地板矩阵
    
    private Image target=null;		//把子帖图
    private Image bg=null;		//背景图片
    
    private Background background;	//背景
    
    Graphics3D g3d;
    
    Model model;
    
    long time;
    long stTime;
    int fps;
    int pitch,yaw;
    int x,y;
    float arrow_pr[]=new float[5];
    
    public TestCanvas() {
        super();
        init();
    }
    
    private void init(){
        //获取开始时间
        time=System.currentTimeMillis();
        //初始化角度
        pitch=yaw=0;
        model=new Model();
        try{
            target=Image.createImage("/target.png");
            bg=Image.createImage("/b.png");
        }catch(java.io.IOException ioe){
            ioe.printStackTrace();
        }
        g3d=Graphics3D.getInstance();
        // 生成摄影机,以及设置摄影机初始值
        camera=new Camera();
        camera.setPerspective(30,(float)getWidth()/(float)getHeight(),1,1000);
        camera.translate(0,18,10);
        camera_Tran=new Transform();
        //  初始化光源
        light=new Light();
        light.setMode(Light.OMNI);
        light_Tran=new Transform();
        light_Tran.postTranslate(0,5,-5);
        // 初始化箭的模型
        arrow=model.creatArrow();
        arrow.translate(0,0,-10);
        arrow_Tran=new Transform();
        //初始化靶子模型
        box=model.creatBox(target);
        box.translate(0,17,-100);
        box_Tran=new Transform();
        //初始化地板
        floor=model.creatPlane();
        floor_Tran=new Transform();
        //初始化背景
        background=new Background();
        background.setImage(new Image2D(Image2D.RGB,bg));
    }
    
    protected synchronized void paint(Graphics g) {
        g.setClip(0,0,getWidth(),getHeight());;
        g3d.bindTarget(g,true,Graphics3D.DITHER|Graphics3D.TRUE_COLOR);
        g3d.setViewport(0,0,getWidth(),getHeight());   //设置3D渲染范围
        g3d.resetLights();
        g3d.clear(background);;
        //添加光源
        g3d.addLight(light,light_Tran);
        //添加摄影机
        g3d.setCamera(camera,camera_Tran);
        // 渲染物体
        g3d.render(box,box_Tran);
        g3d.render(arrow,arrow_Tran);
        g3d.render(floor,floor_Tran);
        g3d.releaseTarget();
        //绘制帧率
        g.setColor(0x000000);
        g.drawString(Float.toString(fps), 0, 0, 0);
        g.drawImage(target,getWidth()-32,getHeight()-32,Graphics.LEFT|Graphics.TOP);
    }
    
    public void run(){
        while(true){
            updata();
            repaint();
            try{
                Thread.sleep(50);
            }catch(Exception e){}
        }
    }
    private synchronized void updata(){
        //计算桢率
        fps=(int)(1000/(System.currentTimeMillis()-time));
        time=System.currentTimeMillis();
        float max[]=new float[16];
        //旋转中间矩阵
        Transform temp=new Transform();
        temp.postTranslate(0,17,10);
        temp.postRotate(pitch,1,0,0);
        temp.postRotate(yaw,  0,1,0);
        // 旋转摄影机
        camera.setOrientation(0,0,0,0);
        camera.postRotate(pitch,1,0,0);
        camera.postRotate(yaw,  0,1,0);
        camera.getCompositeTransform(camera_Tran);
        // 对箭矩阵进行运算
        arrow.getCompositeTransform(arrow_Tran);
        temp.postMultiply(arrow_Tran);
        arrow_Tran=new Transform(temp);
        box.getCompositeTransform(box_Tran);
    }
    
    public void  keyPressed(int keycode){
        int action = this.getGameAction(keycode);
        switch(action){
            case UP:
                pitch--;
                break;
            case DOWN:
                pitch++;
                break;
            case LEFT:
                yaw--;
                break;
            case RIGHT:
                yaw++;
                break;
            case FIRE:
                break;
        }
    }

}

⌨️ 快捷键说明

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