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,当然不存在这个问题 ...
随机推荐
- 团队项目个人进展——Day08
一.昨天工作总结 冲刺第八天,昨天看了许多关于wx.request的知识,主要测试了OBJECT参数,同时也对https方面有了一定了解 二.遇到的问题 对文档中的内容只是熟悉,理解并运用起来还存在问 ...
- Paxos协议笔记
对Paxos协议的介绍,可以通过Leslie Lamport的<Paxos Made Simple>展开学习和了解.Paxos算法在允许失败的分布式系统环境下,实现系统一致性.失败的情况有 ...
- [Spark SQL_1] Spark SQL 配置
0. 说明 Spark SQL 的配置基于 Spark 集群搭建 && Hive 的安装&配置 1. 简介 Spark SQL 是构建在 Spark Core 模块之上的四大 ...
- 3.1Python的判断选择语句
返回总目录 目录: 1.if单分支语句 2.if else 双分支语句 3.if elif ...else多分支语句 4.if 嵌套语句 判断语句总览: (一)if单分支语句: 语法: if 条件: ...
- 内网DHCP攻击
局域网内DHCP攻击 实验环境:两个win2008 r2虚拟机(一台用作正常的DHCP服务器,另一台用作伪造DHCP服务器),两个win7虚拟机(用作客户机),一个kali虚拟机(用作攻击,耗尽DHC ...
- Javascript之DOM性能优化
原文地址:http://ce.sysu.edu.cn/hope/Item/140355.aspx 作者:陈古松 来源:本站原创 发布时间:2015-03-14 更新时间:2015-03-14 点击数 ...
- 转,敏感词过滤,PHP实现的Trie树
原文地址:http://blog.11034.org/2012-07/trie_in_php.html 项目需求,要做敏感词过滤,对于敏感词本身就是一个CRUD的模块很简单,比较麻烦的就是对各种输入的 ...
- Odoo Model内容详解
转载请注明原文地址:https://www.cnblogs.com/cnodoo/p/9390688.html 一:Odoo模型属性 1:_name 模型的唯一标识:如果没有继承其他模型 ...
- AES块加密与解密
AES块加密与解密 解密目标 在CBC和CTR两种模式下分别给出十篇加密的样例密文,求解密一篇特定的密文 解密前提 全部密文及其加密使用的key都已给出 加密的方法遵循AES的标准 解密过程分析 实验 ...
- OpenCV——凸包
#include <opencv2/opencv.hpp> #include <iostream> #include <math.h> using namespac ...