libgdx学习记录12——圆角矩形CircleRect
libgdx提供了ShapeRenderer这个工具,用它可以画点、画线、画圆、画矩形、画椭圆、画扇形,但是没有提供画圆角矩形的方法。
刚开始自己尝试分成8端,4端画直线,4端画扇形,发现多了半径几部分,于是又改成全部画线。
4端弧采用逐个描点实现。
具体代码:
package com.fxb.newtest; import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType; public class Lib011_CicleRect extends ApplicationAdapter{ ShapeRenderer rend; @Override
public void create() {
// TODO Auto-generated method stub
rend = new ShapeRenderer();
} public void DrawCircleRect( float x, float y, float width, float height, float radius, ShapeRenderer rend, Color color ){
rend.setColor( color );
rend.begin(ShapeType.Line); //rend.rect( x, y, width, height );
rend.line( x+radius, y, x+width-radius, y );
rend.line( x+radius, y+height, x+width-radius, y+height );
rend.line( x, y+radius, x, y+height-radius );
rend.line( x+width, y+radius, x+width, y+height-radius );
/*
rend.arc( x+width-radius, y+height-radius, radius, 0, 90 );
rend.arc( x+radius, y+height-radius, radius, 90, 90 );
rend.arc( x+radius, y+radius, radius, 180, 90 );
rend.arc( x+width-radius, y+radius, radius, 270, 90 );
*/ int segments = (int)( *(float)Math.cbrt( radius ) ); for( int i=; i<segments; i++ ){
float x0 = x+width-radius;
float y0 = y+height-radius;
//float angle1 = (float)Math.PI*i*9f/180f;
//float angle2 = (float)Math.PI*(i+1)*9f/180f;
float angle1 = (float)Math.PI*i/(*segments);
float angle2 = (float)Math.PI*(i+)/(*segments);
rend.line( x0+radius*(float)Math.cos(angle1), y0+radius*(float)Math.sin(angle1), x0+radius*(float)Math.cos(angle2), y0+radius*(float)Math.sin(angle2) );
} for( int i=; i<segments; i++ ){
float x0 = x+radius;
float y0 = y+height-radius;
float angle1 = (float)Math.PI*(i+segments)/(*segments);
float angle2 = (float)Math.PI*(i+segments+)/(*segments);
rend.line( x0+radius*(float)Math.cos(angle1), y0+radius*(float)Math.sin(angle1), x0+radius*(float)Math.cos(angle2), y0+radius*(float)Math.sin(angle2) );
} for( int i=; i<segments; i++ ){
float x0 = x+radius;
float y0 = y+radius;
float angle1 = (float)Math.PI*(i+segments*)/(*segments);
float angle2 = (float)Math.PI*(i+segments*+)/(*segments);
rend.line( x0+radius*(float)Math.cos(angle1), y0+radius*(float)Math.sin(angle1), x0+radius*(float)Math.cos(angle2), y0+radius*(float)Math.sin(angle2) );
}
for( int i=; i<segments; i++ ){
float x0 = x+width-radius;
float y0 = y+radius;
float angle1 = (float)Math.PI*(i+segments*)/(*segments);
float angle2 = (float)Math.PI*(i+segments*+)/(*segments);
rend.line( x0+radius*(float)Math.cos(angle1), y0+radius*(float)Math.sin(angle1), x0+radius*(float)Math.cos(angle2), y0+radius*(float)Math.sin(angle2) );
} rend.end();
} @Override
public void render() {
// TODO Auto-generated method stub
Gdx.gl.glClearColor( 0.5f, 0.5f, 0.5f, );
Gdx.gl.glClear( GL10.GL_COLOR_BUFFER_BIT ); DrawCircleRect( , , , , , rend, Color.RED );
DrawCircleRect( , , , , , rend, Color.BLUE );
DrawCircleRect( , , , , , rend, Color.CYAN );
DrawCircleRect( , , , , , rend, Color.YELLOW );
} @Override
public void dispose() {
// TODO Auto-generated method stub
rend.dispose();
super.dispose();
} }
运行结果:

4个矩形的圆角半径分别为10,20,30,50像素。
分段数采用源码中的构造方式,
int segments = (int)( 6*(float)Math.cbrt( radius ) );
当然还可以做优化的,以后可以尝试。
libgdx学习记录12——圆角矩形CircleRect的更多相关文章
- libgdx学习记录24——九宫格NinePatch
NinePatch用于图片纹理拉伸显示.当图片拉伸时,4个角不会拉伸,而只有中间的部分会拉伸,适合做圆角矩形类的Button. 简单示例: package com.fxb.newtest; impor ...
- libgdx学习记录13——矩形CD进度条绘制
利用ShapeRenderer可进行矩形进度条的绘制,多变形的填充等操作. 这是根据角度获取矩形坐标的函数. public Vector2 GetPoint( float x, float y, fl ...
- libgdx学习记录11——平铺地图TiledMap
地图对于游戏场景十分重要,很多游戏都需要对地图进行编辑,可使用TileMap进行编辑并生成对应的tmx格式地图文件. 编辑好后,可通过TmxMapLoader来读取地图文件.可通过一个正交相机Otho ...
- libgdx学习记录2——文字显示BitmapFont
libgdx对中文支持不是太好,主要通过Hireo和ttf字库两种方式实现.本文简单介绍最基本的bitmapfont的用法. 代码如下: package com.fxb.newtest; import ...
- libgdx学习记录3——动画Animation
libgdx动画采用Animation实现,即通过帧动画实现. 代码如下: package com.fxb.newtest; import com.badlogic.gdx.ApplicationAd ...
- libgdx学习记录26——Polygon多边形碰撞检测
libgdx中Math封装了Polygon这个类,它是由多个定点进行描述实现的,在进行物体间的碰撞时,物体轮廓有时候是不规则的,这时候可以用一个多边形勾勒出其大概的轮廓,对其进行模拟. Polygon ...
- libgdx学习记录22——3d物体创建
libgdx是一个强大的游戏框架,不仅支持2d部分,同时还支持3d部分. libgdx的3d部分投影主要通过PerspectiveCamera实现. 物体的显示过程: 1. 创建远景相机,角度一般设为 ...
- libgdx学习记录20——多线程MultiThread资源处理
在libgdx中,一般的逻辑流程都在rende()函数中执行,这个函数是由opengl的渲染线程调用的,一般的图形显示和逻辑处理都在这个线程中. 一般情形下,在这个线程中处理就行了.但是当某些逻辑处理 ...
- libgdx学习记录19——图片动态打包PixmapPacker
libgdx中,opengl 1.x要求图片长宽必须为2的整次幂,一般有如下解决方法 1. 将opengl 1.x改为opengl 2.0.(libgdx 1.0版本后不支持1.x,当然不存在这个问题 ...
随机推荐
- c# 子窗体打开或者切换就最大化
“用MDI方式打开一个子窗口体后,总是不能最大化显示,明明子窗口体的WindowState设置为Maximized?”,相信有很多人会遇到这的样问题,请按下面的方法设置即可使MDI子窗体最大化:1.把 ...
- PHP正则自动验证传入数据
本文出至:新太潮流网络博客 /** * [is_string_regular_type 正则自动验证传入数据] * @E-mial wuliqiang_aa@163.com * @TIME 2017- ...
- Prometheus Node_exporter 之 Disk Datail /proc/diskstats
Disk Datail /proc/diskstats 1. Disk IOps Completed type: GraphUnit: I/O ops/sec(iops)Label: IO read( ...
- w3school 基础学习
http://www.w3school.com.cn/ http://www.w3school.com.cn/sql/sql_quickref.asp
- Node.js环境搭建和学习(windwos环境)
Node.js环境搭建和学习 一.环境搭建 1.下载安装文件 下载地址http://nodejs-org.qiniudn.com/下载Node.js环境安装包,根据操作系统下载对应的安装包 下载地址 ...
- ping 命令 指定特定网卡 发送 ICMP 数据包
Windows : ping -S Linux : ping -I <device> -I interface interface is either an address, or an ...
- PHP中unset,array_splice删除数组中元素的区别
php中删除数组元素是非常的简单的,但有时删除数组需要对索引进行一些排序要求我们会使用到相关的函数,这里我们来介绍使用unset,array_splice删除数组中的元素区别吧 如果要在某个数组中删除 ...
- sysbench压力工具报错:
[root@ sysbench-]# /usr/local/sysbench/bin/sysbench --version : cannot open shared object file: No s ...
- 2018.08.31 16:26 调试 Swift 和 Pycharm 与 github 之间的链接
花了一段时间调试Swift和Pycharm的链接,网上查了一下有关信息,再加上自己的摸索,一会就掌握了.
- 团队作业7——第二次项目冲刺(Beta版本)day3
项目成员: 曾海明(组长):201421122036 于波(组员):201421122058 蓝朝浩(组员):201421122048 王珏 (组员):201421122057 叶赐红(组员):20 ...