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

除了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

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

帅气的东哥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值