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. SpringBoot中redis的使用介绍

    REmote DIctionary Server(Redis) 是一个由Salvatore Sanfilippo写的key-value存储系统. Redis是一个开源的使用ANSI C语言编写.遵守B ...

  2. 前端需要掌握的后台基础:HTTP协议

    什么是HTTP? 以下来自度娘最为专业的解释: 超文本传输协议(HTTP,HyperText Transfer Protocol)是互联网上应用最为广泛的一种网络协议.所有的WWW文件都必须遵守这个标 ...

  3. LeetCode题解之 Search in a Binary Search Tree

    1.题目描述 2.问题分析 利用递归遍历二叉查找树. 3.代码 TreeNode* searchBST(TreeNode* root, int val) { if (root == NULL) ret ...

  4. Oracle EBS FA 资产编号跳号

  5. Css基础笔记(部分)

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  6. oracle 使用绑定变量极大的提升性能

    初始化操作 SQL> alter system flush shared_pool; SQL> set timing on; 1. 未使用绑定变量的时候代码如下 declare type ...

  7. Python抓取zabbix性能监控图

    一.通过查询zabbix db的方式通过主机IP获取到所需要的graphid(比如CPU监控图.内存监控图等,每个图对应一个graphid),最后将图片保存到本地 注:该graph必须要在 scree ...

  8. 【Alpha 冲刺】 10/12

    今日任务总结 人员 今日原定任务 完成情况 遇到问题 贡献值 胡武成 完成app端api编写 未完成 Json格式出了点问题,修复中 孙浩楷 图片在线编辑器插件引入 未完成 耦合了,结果另外一个那边做 ...

  9. 方法(method)和函数(function)的区别

    函数是一段代码,通过名字来进行调用.它能将一些数据(参数)传递进去进行处理,然后返回一些数据(返回值),也可以没有返回值. 所有传递给函数的数据都是显式传递的. 方法也是一段代码,通过一个与对象相关联 ...

  10. 利用Python爬取豆瓣电影

    目标:使用Python爬取豆瓣电影并保存MongoDB数据库中 我们先来看一下通过浏览器的方式来筛选某些特定的电影: 我们把URL来复制出来分析分析: https://movie.douban.com ...