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的更多相关文章

  1. libgdx学习记录24——九宫格NinePatch

    NinePatch用于图片纹理拉伸显示.当图片拉伸时,4个角不会拉伸,而只有中间的部分会拉伸,适合做圆角矩形类的Button. 简单示例: package com.fxb.newtest; impor ...

  2. libgdx学习记录13——矩形CD进度条绘制

    利用ShapeRenderer可进行矩形进度条的绘制,多变形的填充等操作. 这是根据角度获取矩形坐标的函数. public Vector2 GetPoint( float x, float y, fl ...

  3. libgdx学习记录11——平铺地图TiledMap

    地图对于游戏场景十分重要,很多游戏都需要对地图进行编辑,可使用TileMap进行编辑并生成对应的tmx格式地图文件. 编辑好后,可通过TmxMapLoader来读取地图文件.可通过一个正交相机Otho ...

  4. libgdx学习记录2——文字显示BitmapFont

    libgdx对中文支持不是太好,主要通过Hireo和ttf字库两种方式实现.本文简单介绍最基本的bitmapfont的用法. 代码如下: package com.fxb.newtest; import ...

  5. libgdx学习记录3——动画Animation

    libgdx动画采用Animation实现,即通过帧动画实现. 代码如下: package com.fxb.newtest; import com.badlogic.gdx.ApplicationAd ...

  6. libgdx学习记录26——Polygon多边形碰撞检测

    libgdx中Math封装了Polygon这个类,它是由多个定点进行描述实现的,在进行物体间的碰撞时,物体轮廓有时候是不规则的,这时候可以用一个多边形勾勒出其大概的轮廓,对其进行模拟. Polygon ...

  7. libgdx学习记录22——3d物体创建

    libgdx是一个强大的游戏框架,不仅支持2d部分,同时还支持3d部分. libgdx的3d部分投影主要通过PerspectiveCamera实现. 物体的显示过程: 1. 创建远景相机,角度一般设为 ...

  8. libgdx学习记录20——多线程MultiThread资源处理

    在libgdx中,一般的逻辑流程都在rende()函数中执行,这个函数是由opengl的渲染线程调用的,一般的图形显示和逻辑处理都在这个线程中. 一般情形下,在这个线程中处理就行了.但是当某些逻辑处理 ...

  9. libgdx学习记录19——图片动态打包PixmapPacker

    libgdx中,opengl 1.x要求图片长宽必须为2的整次幂,一般有如下解决方法 1. 将opengl 1.x改为opengl 2.0.(libgdx 1.0版本后不支持1.x,当然不存在这个问题 ...

随机推荐

  1. python appium笔记(一):appium android环境配置及示例

     (一) 简介 Appium是一个开源的自动化测试框架,可以用来测试基于iOS.Android和Firefox OS平台的原生和混合应用.该框架使用Selenium Webdriver,在执行测试时和 ...

  2. Leetcode题解之Valid Palindrome II

    1.题目描述 2.问题分析 使用两个下标,检测下标对应的字符是否相等,若不相等,则考察子串. 3.代码 bool validPalindrome(string s) { , j = s.size()- ...

  3. Prometheus Node_exporter 之 Memory Detail Vmstat

    Memory Detail Vmstat 查看/proc/vmstat 文件的内容 1. Memory Pages In / Out type: GraphUnit: shortLabel: Page ...

  4. 光杆mdf文件的导入

    场景,准备学习SSAS的时候,按照教程在微软下载了示例数据库AdventureWorksDW2012,下载来才发现只有一个mdf文件. 正好今天群里有位兄弟也碰到差不多的问题,客户数据库里的ldf文件 ...

  5. 配置jboss EAP 6.4 数据库连接超时时间

    Environment Red Hat JBoss Enterprise Application Platform (EAP) 6.x 7.x Issue Server throws followin ...

  6. 安装SQL sever2008时显示重新启动计算机规则失败,应该怎么解决?

    1.删除注册表:在HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager中找到 PendingFileRenameOpe ...

  7. python基础学习11----函数

    一.函数的定义 def 函数名(参数列表): 函数体 return语句 return语句不写或后边不加任何对象即为return None 二.函数的参数 无参数 def func1(): print( ...

  8. Software Engineer’s path to the best annual performance review

    http://michaelscodingspot.com/2017/06/04/software-engineers-path-best-annual-performance-review/ How ...

  9. python面试题(一)

    1.通过代码实现如下转换: 二进制转换成十进制:v = “0b1111011” #先将其转换为字符串,再使用int函数,指定进制转换为十进制. print(int("0b1111011&qu ...

  10. SDOI 2018 R2 游记

    一篇真正的“游记”. DAY -3 下午: 今天老师批准了我去省选划水的请假要求. 批准之后感觉学习非常有动力,顺便忽悠别的同学和我一起去,然而wzx是唯一一个表示可以考虑一下的同学,其他同学直接一口 ...