/Users/lyon/j4p/src/bookExamples/ch26Graphics/draw2d/Rect2d.java

1    package bookExamples.ch26Graphics.draw2d; 
2     
3    import java.awt.*; 
4     
5    public class Rect2d extends Shape { 
6        int x1 = 0; 
7        int y1 = 0; 
8        int h = 1; 
9        int w = 1; 
10       int xc = 0; 
11       int yc = 0; 
12    
13       public int getX() { 
14           return x1; 
15       } 
16    
17       public int getY() { 
18           return y1; 
19       } 
20    
21       public int getW() { 
22           return w; 
23       } 
24    
25       public int getH() { 
26           return h; 
27       } 
28    
29    
30       public Rect2d(int _x1, int _y1, int _x2, int _y2) { 
31           x1 = _x1; 
32           y1 = _y1; 
33           w = Math.abs(_x2 - x1); 
34           h = Math.abs(_y2 - y1); 
35           if (_x1 > _x2) x1 = _x2; 
36           if (_y1 > _y2) y1 = _y2; 
37           xc = x1 + w / 2; 
38           yc = y1 + h / 2; 
39    
40       } 
41    
42       public void paint(Graphics g) { 
43           g.drawRect(x1, y1, w, h); 
44           g.fillOval(xc, yc, 2, 2); 
45           g.drawString("(" + xc + "," + yc + ")", xc + 3, yc + 3); 
46       } 
47   } 
48