📄 rectangle.java
字号:
// Chapter 6 Exercises 1 & 2
public class Rectangle extends Shape {
// Bottom right of rectangle is defined relative to the reference point, position:
private Point bottomRight; // Bottom right of rectangle.
// Constructors:
public Rectangle(Point startDiag, Point endDiag) {
// Position of rectangle is top left corner - minimum x and y:
position = new Point(Math.min(startDiag.x,endDiag.x),Math.min(startDiag.y,endDiag.y));
// Bottom right is relative to top left:
bottomRight = new Point(Math.max(startDiag.x,endDiag.x) - position.x,
Math.max(startDiag.y,endDiag.y) - position.y);
}
// Overrides the method inherited from Object:
public String toString() {
// Return a string representation of the object:
return "Rectangle: Top Left: " + position + " Bottom Right: " +
position.add(bottomRight);
}
// Display the rectangle:
public void show() {
System.out.println("\n" + toString());
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -