您的位置:首页 > 其它

libgdx的基本使用(2)——绘制图形

2014-03-01 09:46 246 查看
除了FirstGame以外的类的代码,都是和上一遍博客一样的。。。

package com.njupt.helloworld1;

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;

public class FirstGame implements ApplicationListener {

private SpriteBatch batch;// 用于绘制图片
private Texture texture;// 纹理
private TextureRegion region;//用于显示纹理的一部分

@Override
public void create() {
batch = new SpriteBatch();

/**
* 实例化texture,texture=new Texture(Gdx.files.internal("image1.jpg"));
* 然后来说一下为什么要将图片放在assets文件夹中。 Gdx.files是libgdx的文件模块,主要提供以下5大功能。 读取文件 写文件
* 复制文件 移动文件 列出文件和目录 而获取操作文件的FileHandle有4种方法。
*
* 1.Classpath 路径相对于classpath,文件通常为只读。
*
* 2.Internal 内部文件路径相对于程序根目录或者android 的assets文件夹。
*
* 3.External 外部文件路径是相对于SD卡根目录
*
* 4.Absolute
*
* assets文件夹本身就是存储资源的文件夹,而且相比resource文件夹,它其中的资源不会生成R中的ID,用来放图片很是合适。
*
* 所以用Gdx.files.internal("image1.jpg")获取图片,然后调用batch.draw(texture,20,10)
* ;绘制图形,20,10是坐标,笛卡尔座标,以左下角为原点。
*/
texture = new Texture(Gdx.files.internal("image2.jpg"));

/**
* 这5个参数分别为:纹理、初始点的坐标、要显示的宽度、要现实的高度
*/
region = new TextureRegion(texture,100,100,200,200);
}

@Override
public void dispose() {
// TODO Auto-generated method stub

}

@Override
public void pause() {
// TODO Auto-generated method stub

}

@Override
public void render() {
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);//清屏
batch.begin();//开始绘图

//		batch.draw(texture, 100, 100);//这三个参数分别为:纹理,X轴的坐标,Y轴的坐标(一左下角为原点)
batch.draw(region, 0, 0);
batch.end();//结束绘图
}

@Override
public void resize(int arg0, int arg1) {
// TODO Auto-generated method stub

}

@Override
public void resume() {
// TODO Auto-generated method stub

}

}


在这一篇博客中,关键要理解以下几条语句:

texture = new Texture(Gdx.files.internal("image2.jpg"));

region = new TextureRegion(texture,100,100,200,200);

batch.draw(texture, 100, 100);//这三个参数分别为:纹理,X轴的坐标,Y轴的坐标(以左下角为原点)


本demo的下载链接:http://download.csdn.net/detail/caihongshijie6/6977607
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: