Java Reference
In-Depth Information
LISTING 5.5
Continued
19: y1 = topLeft.y;
20: x2 = bottomRight.x;
21: y2 = bottomRight.y;
22: return this;
23: }
24:
25: Box buildBox(Point topLeft, int w, int h) {
26: x1 = topLeft.x;
27: y1 = topLeft.y;
28: x2 = (x1 + w);
29: y2 = (y1 + h);
30: return this;
31: }
32:
33: void printBox(){
34: System.out.print(“Box: <” + x1 + “, “ + y1);
35: System.out.println(“, “ + x2 + “, “ + y2 + “>”);
36: }
37:
38: public static void main(String[] arguments) {
39: Box rect = new Box();
40:
41: System.out.println(“Calling buildBox with coordinates “
42: + “(25,25) and (50,50):”);
43: rect.buildBox(25, 25, 50, 50);
44: rect.printBox();
45:
46: System.out.println(“\nCalling buildBox with points “
47: + “(10,10) and (20,20):”);
48: rect.buildBox(new Point(10, 10), new Point(20, 20));
49: rect.printBox();
50:
51: System.out.println(“\nCalling buildBox with 1 point “
52: + “(10,10), width 50 and height 50:”);
53:
54: rect.buildBox(new Point(10, 10), 50, 50);
55: rect.printBox();
56: }
57: }
5
The following is this program's output:
Calling buildBox with coordinates (25,25) and (50,50):
Box: <25, 25, 50, 50>
Calling buildBox with points (10,10) and (20,20):
Box: <10, 10, 20, 20>
 
Search WWH ::




Custom Search