draw2d自定义仪表盘

draw2d自定义仪表盘

画圆

		 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))
					);
			
			
		}

	}
	
	
	}

效果:(还有一个指针没有画)
在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值