public class Circle extends Shape {
private int radius;
/**
* 构造函数
*/
public Circle(DrawingAPI drawingAPI, String color, int x, int y, int radius) {
super(drawingAPI, color, x, y);
this.radius = radius;
}
@Override
public void draw() {
System.out.println("🔵 绘制圆形:");
System.out.println(" " + getShapeInfo());
drawingAPI.drawCircle(x, y, radius, color);
}
@Override
public String getShapeInfo() {
return String.format("圆形 - 位置:(%d,%d), 半径:%dpx, 颜色:%s",
x, y, radius, color);
}
/**
* 改变半径
*/
public void setRadius(int radius) {
this.radius = radius;
System.out.println("📏 改变半径为: " + radius + "px");
}
/**
* 获取半径
*/
public int getRadius() {
return radius;
}
}
public class Rectangle extends Shape {
private int width, height;
/**
* 构造函数
*/
public Rectangle(DrawingAPI drawingAPI, String color, int x, int y, int width, int height) {
super(drawingAPI, color, x, y);
this.width = width;
this.height = height;
}
@Override
public void draw() {
System.out.println("🔲 绘制矩形:");
System.out.println(" " + getShapeInfo());
drawingAPI.drawRectangle(x, y, width, height, color);
}
@Override
public String getShapeInfo() {
return String.format("矩形 - 位置:(%d,%d), 尺寸:%dx%dpx, 颜色:%s",
x, y, width, height, color);
}
/**
* 改变尺寸
*/
public void setSize(int width, int height) {
this.width = width;
this.height = height;
System.out.println("📐 改变尺寸为: " + width + "x" + height + "px");
}
/**
* 获取宽度
*/
public int getWidth() {
return width;
}
/**
* 获取高度
*/
public int getHeight() {
return height;
}
}
public class Triangle extends Shape {
private int x2, y2, x3, y3;
/**
* 构造函数
*/
public Triangle(DrawingAPI drawingAPI, String color, int x1, int y1,
int x2, int y2, int x3, int y3) {
super(drawingAPI, color, x1, y1);
this.x2 = x2;
this.y2 = y2;
this.x3 = x3;
this.y3 = y3;
}
@Override
public void draw() {
System.out.println("🔺 绘制三角形:");
System.out.println(" " + getShapeInfo());
drawingAPI.drawTriangle(x, y, x2, y2, x3, y3, color);
}
@Override
public String getShapeInfo() {
return String.format("三角形 - 顶点1:(%d,%d), 顶点2:(%d,%d), 顶点3:(%d,%d), 颜色:%s",
x, y, x2, y2, x3, y3, color);
}
/**
* 改变顶点位置
*/
public void setVertices(int x1, int y1, int x2, int y2, int x3, int y3) {
this.x = x1;
this.y = y1;
this.x2 = x2;
this.y2 = y2;
this.x3 = x3;
this.y3 = y3;
System.out.println("📍 改变顶点位置");
}
/**
* 获取顶点坐标
*/
public int[][] getVertices() {
return new int[][]{{x, y}, {x2, y2}, {x3, y3}};
}
}