利用java继承机制创建Figure、CloseFigure、Line、Polygon、Triangle、Rectangle类、并通过throws进行违规数据的异常处理。

1.实验题目:

5-13修改例3.7各图形类的构造方法如下,若参数不符合要求,抛出无效参数异常。

// 构造点,若x<0或 y<0,则抛出无效参数异常

public Point(int x, int y) throws IllegalArgumentException

//以下构造矩形,若length<0或width<0,则抛出无效参数异常

public Rectangle(Point point1, int length, int width) throws IllegalArgumentException

// 以下由三点构造一个三角形;当三点共线不能构成一个三角形时,则抛出无效参数异常

Public Triangle(Point point1, Point point2, Point point3) throws IllegalArgumentException

//以下构造多边形,由points数组指定多点;若少于3点,或不能构成多边形,则抛出无效参数异常

public Polygon(Point[] points) throws IllegalArgumentException

2.实验目的:

掌握java异常处理机制及分析代码存在的异常类型

掌握throws的用法

掌握继承的机制和用法

3.实验内容:

  1. 实验思路

创建图形类Figure、Point

创建Figure的子类CloseFigure、Line

创建CloseFigure的子类Polygon、Triangle

创建Polygon的子类Rectangle

类的继承关系如图:

  • 2
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
创建圆角 button,可以使用 C# 的 System.Windows.Forms 命名空间中的 Button 控件,并设置其 FlatStyle 属性为 FlatStyle.Flat,同时设置其 FlatAppearance 属性中的 BorderSize 和 CornerRadius 属性来实现圆角效果。 下面是一个示例代码: ``` using System.Drawing; using System.Windows.Forms; public class RoundButton : Button { public RoundButton() { this.FlatStyle = FlatStyle.Flat; this.FlatAppearance.BorderSize = 0; this.FlatAppearance.BorderColor = Color.FromArgb(0, 255, 255, 255); this.FlatAppearance.MouseDownBackColor = Color.FromArgb(255, 192, 192, 192); this.FlatAppearance.MouseOverBackColor = Color.FromArgb(255, 224, 224, 224); this.BackColor = Color.FromArgb(255, 255, 255, 255); this.Size = new Size(100, 50); this.CornerRadius = 10; } private int cornerRadius; public int CornerRadius { get { return cornerRadius; } set { cornerRadius = value; this.Invalidate(); } } protected override void OnPaint(PaintEventArgs pevent) { base.OnPaint(pevent); Rectangle rectangle = new Rectangle(0, 0, this.Width - 1, this.Height - 1); GraphicsPath path = GetRoundedRectangle(rectangle, CornerRadius); this.Region = new Region(path); using (Pen pen = new Pen(this.FlatAppearance.BorderColor)) { pevent.Graphics.DrawPath(pen, path); } } private GraphicsPath GetRoundedRectangle(Rectangle rectangle, int cornerRadius) { GraphicsPath path = new GraphicsPath(); path.StartFigure(); path.AddArc(rectangle.X, rectangle.Y, cornerRadius * 2, cornerRadius * 2, 180, 90); path.AddLine(rectangle.X + cornerRadius, rectangle.Y, rectangle.Right - cornerRadius * 2, rectangle.Y); path.AddArc(rectangle.X + rectangle.Width - cornerRadius * 2, rectangle.Y, cornerRadius * 2, cornerRadius * 2, 270, 90); path.AddLine(rectangle.Right, rectangle.Y + cornerRadius * 2, rectangle.Right, rectangle.Bottom - cornerRadius * 2); path.AddArc(rectangle.X + rectangle.Width - cornerRadius * 2, rectangle.Y + rectangle.Height - cornerRadius * 2, cornerRadius * 2, cornerRadius * 2, 0, 90); path.AddLine(rectangle.Right - cornerRadius * 2, rectangle.Bottom, rectangle.X + cornerRadius * 2, rectangle.Bottom); path.AddArc(rectangle.X, rectangle.Y + rectangle.Height - cornerRadius * 2, cornerRadius * 2, cornerRadius * 2, 90, 90); path.CloseFigure(); return path; } } ``` 使用 RoundButton 控件时,可以像使用普通的 Button 控件一样进行操作,例如: ``` RoundButton roundButton = new RoundButton(); roundButton.Text = "Click Me"; this.Controls.Add(roundButton); ``` 这样就可以创建一个带有圆角的 button 控件了。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值