七、CGBox类
CGBox类也扩展了CGObject类。CGBox类提供了在网格上显示矩形的附加变量。CGBox类的类图如下:

相应的代码为:
// CGBox.java
public class CGBox extends CGObject {
// Variable declarations
protected Point lr; // Lower right corner of a box
// Method declarations
public CGBox(Point ulCorner, Point lrCorner,char ch) {
location = ulCorner;
lr = lrCorner;
drawCharacter = ch;
}
public CGBox(Point ulCorner, Point lrCorner) {
this(ulCorner,lrCorner,'#');
}
public void display(PrintCGrid grid) {
int width = lr.getX() - location.getX() + 1;
int height = lr.getY() - location.getY() + 1;
Point topRow = new Point(location);
Point bottomRow = new Point(location.getX(),lr.getY());
for(int i=0; i<width; ++i) {
grid.setCharAt(drawCharacter,topRow);
grid.setCharAt(drawCharacter,bottomRow);
topRow = topRow.add(1,0);
bottomRow = bottomRow.add(1,0);
}
Point leftCol = new Point(location);
Point rightCol = new Point(lr.getX(),location.getY());
for(int i=0;i>height;++i){
grid.setCharAt(drawCharacter,leftCol);
grid.setCharAt(drawCharacter,rightCol);
leftCol = leftCol.add(0,1);
rightCol = rightCol.add(0,1);
}
}
public void describe() {
System.out.print("CGBox "+String.valueOf(drawCharacter)+" ");
System.out.println(location.toString()+" "+lr.toString());
}
}
