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,当然不存在这个问题 ...
随机推荐
- 【python】python常用函数
urlencode与urldecode 当url中包含中文或者参数包含中文,需要对中文或者特殊字符(/.&)做编码转换. urlencode的本质:把字符串转为gbk编码,再把\x替换成%.如 ...
- LeetCode 题解之 Two Sum
1.题目描述 2.问题分析 使用hashTable 寻找,target - num[i] ,将时间复杂度降低到 O(n): 3.代码 vector<int> twoSum(vector ...
- sqlio
http://www.cnblogs.com/Amaranthus/archive/2011/09/16/2178747.html Each line in the param.txt file lo ...
- 转:双向链表dblinklist
数据结构C#版笔记--双向链表(DbLinkList) 这是数据结构C#版笔记--线性表(Data Structure)之单链表(LinkList)的继续,对于双向链接,节点上除了Next属性外, ...
- NFS 系统的搭建
问题: 由于工作,需要,不断得进行挂在硬盘重装系统,NFS 系统给了我一个很好的解决方案.于是决定写一篇博客,防止以后再次使用的时候,能够很快得重新建立NFS 文件系统. 调研: NFS(Networ ...
- 关于QT的QCombox的掉坑出坑
最近项目中开发在用到QCombox,然而在开发中,踩到了一个坑,花了一个晚上,一直在想,好在最后找到问题所在了. 这是业务的流程.直接说重点:QCombox在下拉窗更新数据的时候,会默认把下拉窗的第一 ...
- 【转】Java学习---volatile 关键字
[原文]https://www.toutiao.com/i6591422029323305480/ 前言 不管是在面试还是实际开发中 volatile 都是一个应该掌握的技能. 首先来看看为什么会出现 ...
- Jenkins 角色 项目权限管理
插件名称: Role-based Authorization Strategy 新建 两用户 配置项目安全策略 在系统管理页面点击Manage and Assign Roles进入角色管理页面: 进 ...
- 有关于分布式缓存Hazelcast
如果在内网段中部署或者启动缓存服务.不能存在相同的组名称.如同使用dubbo一样,会导致无法连接到缓存节点
- kafka服务无法启动的原因
kafka集群中一台服务器kill掉后再启动时报错. java.io.IOException: Map failed Caused by: java.lang.OutOfMemoryError: Ma ...