现有一块画布上(Canvas)它有如下功能: 定义addShape(Shape s)在画布上新增并绘制出其形状; 定义removeShape(Shape s)删除画布上已存在的形状 定义clone(Shape s)克隆一个已存在的形状增加到画布上 定义Shape Max()方法返回画布上面积最大的形状(如果有相等的几个返回第一个) 定义public double getArea()方法求画布上所有形状的面积和 定义printAllShapes()方法打印画布上所有形状信息 每一个形状(Shape)类,都有创建时的日期数据域(createdDate),创建的颜色(color),求面积和绘制图形的方法等等,每一个形状都是能被比较和复制(复制的时候,形状的时间应该是被复制的时间而不是被复制的形状的创建时间)。长方形和圆形是形状类的子类,它们会重写父类求面积和绘制图形的方法。为了程序实现方便,我们这里只定义两种图形。
时间: 2024-01-22 16:21:33 浏览: 132
好的,这是一个比较完整的画布类,需要实现以下几个方法:
```java
public class Canvas {
private List<Shape> shapes;
public Canvas() {
shapes = new ArrayList<>();
}
// 在画布上新增并绘制出一个形状
public void addShape(Shape s) {
shapes.add(s);
s.draw();
}
// 删除画布上已存在的形状
public void removeShape(Shape s) {
shapes.remove(s);
}
// 克隆一个已存在的形状增加到画布上
public void clone(Shape s) {
Shape clonedShape = s.clone();
shapes.add(clonedShape);
clonedShape.draw();
}
// 返回画布上面积最大的形状
public Shape max() {
if (shapes.isEmpty()) {
return null;
}
Shape maxShape = shapes.get(0);
double maxArea = maxShape.getArea();
for (Shape s : shapes) {
if (s.getArea() > maxArea) {
maxShape = s;
maxArea = s.getArea();
}
}
return maxShape;
}
// 求画布上所有形状的面积和
public double getArea() {
double totalArea = 0;
for (Shape s : shapes) {
totalArea += s.getArea();
}
return totalArea;
}
// 打印画布上所有形状信息
public void printAllShapes() {
for (Shape s : shapes) {
System.out.println(s.toString());
}
}
}
```
其中,Shape、Rectangle和Circle类的实现如下:
```java
public abstract class Shape implements Cloneable {
private Date createdDate;
private String color;
public Shape(String color) {
this.createdDate = new Date();
this.color = color;
}
public abstract double getArea();
public abstract void draw();
@Override
public Shape clone() {
try {
Shape clonedShape = (Shape) super.clone();
clonedShape.createdDate = new Date();
return clonedShape;
} catch (CloneNotSupportedException e) {
return null;
}
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public Date getCreatedDate() {
return createdDate;
}
public void setCreatedDate(Date createdDate) {
this.createdDate = createdDate;
}
@Override
public String toString() {
return "Shape{" +
"createdDate=" + createdDate +
", color='" + color + '\'' +
'}';
}
}
public class Rectangle extends Shape {
private double width;
private double height;
public Rectangle(String color, double width, double height) {
super(color);
this.width = width;
this.height = height;
}
@Override
public double getArea() {
return width * height;
}
@Override
public void draw() {
System.out.println("Drawing a rectangle with color " + getColor());
}
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
@Override
public String toString() {
return "Rectangle{" +
"createdDate=" + getCreatedDate() +
", color='" + getColor() + '\'' +
", width=" + width +
", height=" + height +
'}';
}
}
public class Circle extends Shape {
private double radius;
public Circle(String color, double radius) {
super(color);
this.radius = radius;
}
@Override
public double getArea() {
return Math.PI * radius * radius;
}
@Override
public void draw() {
System.out.println("Drawing a circle with color " + getColor());
}
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
@Override
public String toString() {
return "Circle{" +
"createdDate=" + getCreatedDate() +
", color='" + getColor() + '\'' +
", radius=" + radius +
'}';
}
}
```
阅读全文