当前位置:网站首页>draw2d自定义仪表盘
draw2d自定义仪表盘
2022-04-22 00:34:00 【哭哭啼】
画圆
Ellipse1 ellipse = new Ellipse1();
//设置坐标
ellipse.setBounds(copy);
//打开背景色设置
ellipse.setOpaque(true);
//设置背景色
ellipse.setBackgroundColor(ColorConstants.white);
//将圆添加到当前figure
add(ellipse);
画分割线
import org.eclipse.cdt.codan.core.model.cfg.IExitNode;
import org.eclipse.draw2d.ColorConstants;
import org.eclipse.draw2d.Ellipse;
import org.eclipse.draw2d.Graphics;
import org.eclipse.draw2d.Polyline;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.draw2d.geometry.PointList;
import org.eclipse.draw2d.geometry.Rectangle;
/** * @author jg * 此类继承draw2d封装好的画圆的类 * 2022年4月15日 */
public class Ellipse1 extends Ellipse {
@Override
public void paintFigure(Graphics graphics) {
// TODO Auto-generated method stub
super.paintFigure(graphics);
//得到圆的位置
Rectangle copy = getBounds().getCopy();
drawScale(graphics,copy);
}
/** * 渲染内容 */
public void drawScale(Graphics graphics,Rectangle copy) {
//减2是因为圆的父控件边框宽度
int x = copy.x-2;
int y = copy.y-2;
int width = copy.width;
int height = copy.height;
//圆心位置
int circleCenterX= x + (width/2);
int circleCenterY= y + (height/2);
graphics.setBackgroundColor(ColorConstants.black);
graphics.fillRoundRectangle(new Rectangle(circleCenterX,circleCenterY,10,10), 100, 100);
//半径 -15 为了分割线不在圆上
int r = height/2 -15;
SplitObj splitObj = meter.getSplitObj();
//线宽
graphics.setLineWidth(splitObj.getSplitPointWidth());
ArrayList<Point> coordinate = new ArrayList<>();
//TextLayout layout = new TextLayout(null);
// 分割线上显示的数值计算
//起点值、终点值、分割线数量、跨度 ,如: 起点值 0、终点值100、分割线数量41、跨度4
int interValValue = (100-0)/(40 /4);
//显示值
int num = 0;
// 自定义刻度,如:只显示45度到315度区间 ,分割点41个
double angle = 45;
for (int i = 0; i < 41; i++) {
// 计算每个刻度的弧度
// 先计算角度
if (i == 0) {
// 固定起点角度
angle = 45;
} else if (i == 40) {
// 固定终点角度(可以设置也可以不设置)
angle = 315;
} else {
// 此处一定要加小数点,负责运算结果时钟得到的是整数,41个刻度代表需要40等分角度
angle += ((315.0 - 45.0) / 40.0);
}
double radian = angle * (Math.PI / 180);
//设置大分割点属性,分别是跨度,线宽、线长、颜色 如: 跨度 4 、线宽4、线长15 、颜色0x0
int len = 10;
if (i % 4 ==0) {
//线宽
graphics.setLineWidth(4);
// 分割点线长
len = 15;
// 大分割点位置设置数值
// graphics.drawLine( circleCenterX + (int) (Math.cos(radian) * (r-20)),
// circleCenterY + (int) (Math.sin(radian) * (r-20)),
// circleCenterX + (int) (Math.cos(radian) * (r - 25)),
// circleCenterY + (int) (Math.sin(radian) * (r - 25)));
// graphics.drawRectangle( circleCenterX + (int) (Math.cos(radian) * (r-15)),
// circleCenterY + (int) (Math.sin(radian) * (r-15))
// ,40,30);
//当角度在0到180之间 将坐标往上15点
if (0.0 < angle && angle< 180.0) {
graphics.drawString(""+num, circleCenterX + (int) (Math.cos(radian) * (r -len-10)),
circleCenterY + (int) (Math.sin(radian) * (r -len-30)));
// layout.setText(""+num);
// graphics.drawTextLayout(layout, circleCenterX + (int) (Math.cos(radian) * (r -len-10)),
// circleCenterY + (int) (Math.sin(radian) * (r -len-10)),
// 0, -1, ColorConstants.red,
// ColorConstants.black);
}else {
graphics.drawString(""+num,circleCenterX + (int) (Math.cos(radian) * (r - len-5)),
circleCenterY + (int) (Math.sin(radian) * (r - len-5))
);
}
//添加弧度
//判断开始点数值
if (num == 0 ) {
graphics.setForegroundColor(Converter.Int2Color(Converter.Hex2Int("#ff0000")));
//添加弧度
/*for (int i = 0; i < coordinate.size(); i+=2) { graphics.drawLine(coordinate.get(i), coordinate.get(i+1)); }*/
//内圈圆
Rectangle rectangle = new Rectangle();
//15根据上面 int r = height/2 -15; 得
rectangle.x = x + 15;
rectangle.y = y + 15;
//30为中间圆两边减15得
rectangle.width =width -30;
rectangle.height =height-30;
// 0起始角度,90弧长
//graphics.drawArc(rectangle, 0, 90);
//注意此处的角度是从3点开始,逆时针增加角度,所以一切取反
graphics.drawArc(rectangle, (int)(360-angle), -(270/10*4));
}else if (num == 80) {
graphics.setForegroundColor(Converter.Int2Color(Converter.Hex2Int("#0000ff")));
//内圈圆
Rectangle rectangle = new Rectangle();
//15根据上面 int r = height/2 -15; 得
rectangle.x = x + 15;
rectangle.y = y + 15;
//30为中间圆两边减15得
rectangle.width =width -30;
rectangle.height =height-30;
// 0起始角度,90弧长
//graphics.drawArc(rectangle, 0, 90);
//注意此处的角度是从3点开始,逆时针增加角度
graphics.drawArc(rectangle, (int)(360-angle), -(270/10*2));
}
// 颜色 0xaaaaaa
graphics.setForegroundColor(Converter.Int2Color(0));
num += interValValue;
}else {
//线宽
graphics.setLineWidth(2);
// 颜色 0xaaaaaa
graphics.setForegroundColor(Converter.Int2Color(Converter.Hex2Int("#aaaaaa")));
// 分割点线长
len = 10;
}
// 注意:此处我是以3点为起点,角度顺时针增加
graphics.drawLine(circleCenterX + (int) (Math.cos(radian) * r),
circleCenterY + (int) (Math.sin(radian) * r),
circleCenterX + (int) (Math.cos(radian) * (r - len)),
circleCenterY + (int) (Math.sin(radian) * (r - len))
);
}
}
}
效果:(还有一个指针没有画)

版权声明
本文为[哭哭啼]所创,转载请带上原文链接,感谢
https://blog.csdn.net/jg_csdn/article/details/124243348
边栏推荐
- 【acwing】165. Kitten climbing (DFS pruning)
- The video course "vite Learning Guide - webify deployment project based on Tencent cloud" was launched "cloud + community"
- Ma Qianli, vice president of Babbitt: the rise of NPC in the era of meta universe, digital identity protocol may become an import-level product - 2022 yuan universe cloud Summit
- Yijingjie database will receive Chinese software investment of no more than 389 million yuan
- 2022年4月21日,第14天
- Kotlin - 高阶函数介绍
- Observe cloud landing in Alibaba cloud computing nest and build a new ISV ecosystem
- Prometheus 的使用
- Redis 的五种数据结构分析
- . net core enterprise wechat web page authorization login
猜你喜欢

DPI released the latest progress report on AI drug research and development

精彩回顾 | DEEPNOVA x Iceberg Meetup Online《基于Iceberg打造实时数据湖》

Hold these five points! On the job postgraduate entrance examination can also go ashore!

I use ehcache to improve the query performance by 100 times. It's really fragrant

How to select Bi tools? These three questions are the key!

为什么Web端登录需要验证码?

High expansion and high availability engineering practice of recommended resource bits related to short video app

hex,base64,urlencode编码方案对比

Cookie & session learning

We sincerely invite you to sign up for the first openharmony developer growth plan sharing day
随机推荐
Rpcx source code learning - server side
常见的8个概率分布公式和可视化
从概念到实现,智能家居如何逐步走进我们的生活
. net treasure API: ihostedservice, background task execution
js原型,原型链,构造函数
Youxuan database won the bid for the project of fire product conformity assessment center of Beijing Emergency Management Department
数字化时代,企业运维面临现状及挑战分析解读
Haskell Monoid(幺半群)的介绍
Ma Qianli, vice president of Babbitt: the rise of NPC in the era of meta universe, digital identity protocol may become an import-level product - 2022 yuan universe cloud Summit
使用多个可选过滤器过滤 Eloquent 模型
Is it reliable for Huishang futures to open an account? Is it safe?
【课程汇总】Hello HarmonyOS系列课程,手把手带你零基础入门
【面试普通人VS高手系列】能谈一下CAS机制吗?
2022 superstar learning link: media creative economy, entering ASEAN, career promotion, music appreciation, emergencies and self rescue
DPI released the latest progress report on AI drug research and development
[observation] purple light cloud: homogeneous hybrid cloud is upgraded to distributed cloud, making cloud and intelligence everywhere
Huawei cloud hosting experience: your software storage expert
High expansion and high availability engineering practice of recommended resource bits related to short video app
Wonderful review | deepnova x iceberg meetup online "building a real-time data Lake based on iceberg"
How to use the drawing board to draw the structure diagram easily?