使用Graphics.DrawLine画线时,如何控制线的宽度!

本文详细介绍了在Java中使用BasicStroke类来控制Graphics2D对象绘制线条的宽度的方法,包括如何创建和应用BasicStroke实例,并通过示例演示了如何在绘图过程中调整线条粗细。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转载自:http://topic.youkuaiyun.com/t/20030514/02/1780072.html

问:

使用Graphics.DrawLine画线时,如何控制线的宽度


答:

private   final   static   BasicStroke   stokeLine   =   new   BasicStroke(   2.0f   ); 

Graphics2D   g2d   =   (   Graphics2D   )g; 
g2d.setStroke(   stokeLine   ); 

就可以了 
后面你画线用g2d或者g都可以 
PS1:g为Graphics对象

PS2:呵呵,不好意思,上面的private   的声明是在类内部,不能放在函数里

using System.Drawing; using System.Windows.Forms; namespace ControlLib.控件 { public partial class CustomGroupBox : GroupBox { public CustomGroupBox() { InitializeComponent(); // 设置样式 this.SetStyle(ControlStyles.AllPaintingInWmPaint, true); this.SetStyle(ControlStyles.DoubleBuffer, true); this.SetStyle(ControlStyles.Selectable, true); this.SetStyle(ControlStyles.ResizeRedraw, true); this.SetStyle(ControlStyles.SupportsTransparentBackColor, true); } private Color borderColor = Color.Black; private Color textColor = Color.Black; public Color BorderColor { get { return borderColor; } set { borderColor = value; } } public Color TextColor { get { return textColor; } set { textColor = value; } } protected override void OnPaint(PaintEventArgs e) { // 创建一个新的字体,用于绘制文本 Font textFont = new Font(this.Font.FontFamily, this.Font.Size, this.Font.Style); SizeF textSize = e.Graphics.MeasureString(this.Text, textFont); // 计算文本的水平位置,使其居中 float x = (this.Width - textSize.Width) / 2; float y = 0; // 绘制文本 e.Graphics.DrawString(this.Text, textFont, new SolidBrush(textColor), x, y); // 使用 textColor 绘制文本 // 重新绘制边框,避免与文本重叠 using (Pen borderPen = new Pen(borderColor)) { // 绘制左边框 e.Graphics.DrawLine(borderPen, 0, this.Font.Height / 2, 0, this.Height - 1); // 绘制上边框,留出文本左右边距 float leftMargin = 5; // 左边距,可以根据需要调整 float rightMargin = 5; // 右边距,可以根据需要调整 e.Graphics.DrawLine(borderPen, 0, this.Font.Height / 2, x - leftMargin, this.Font.Height / 2); e.Graphics.DrawLine(borderPen, x + textSize.Width + rightMargin, this.Font.Height / 2, this.Width - 1, this.Font.Height / 2); // 绘制右边框 e.Graphics.DrawLine(borderPen, this.Width - 1, this.Font.Height / 2, this.Width - 1, this.Height - 1); // 绘制下边框 e.Graphics.DrawLine(borderPen, 0, this.Height - 1, this.Width - 1, this.Height - 1); } } } } 仔细分析此代码,在保证原有功能正常的基础上开放绘制圆角矩形的属性。
最新发布
06-08
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值