KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > swingwt > awt > Graphics


1 /*
2    SwingWT
3    Copyright(c)2003-2004, R. Rawson-Tetley
4
5    For more information on distributing and using this program, please
6    see the accompanying "COPYING" file.
7
8    Contact me by electronic mail: bobintetley@users.sourceforge.net
9
10    $Log: Graphics.java,v $
11    Revision 1.8 2004/04/30 23:18:24 dannaab
12    List selection support, misc bug fixes
13
14    Revision 1.7 2004/01/15 16:02:15 bobintetley
15    Using deprecated String methods fixedd
16
17    Revision 1.6 2004/01/15 15:20:29 bobintetley
18    Java2D work
19
20    Revision 1.5 2004/01/15 10:11:14 bobintetley
21    Fixed AWT constructors/hierarchy
22
23    Revision 1.4 2003/12/14 09:13:38 bobintetley
24    Added CVS log to source headers
25
26 */

27
28 package swingwt.awt;
29
30
31 public abstract class Graphics {
32     
33     protected Graphics() {
34     }
35
36     public abstract Graphics create();
37     public Graphics create(int x, int y, int width, int height) {
38     Graphics g = create();
39     if (g == null) return null;
40     g.translate(x, y);
41     g.clipRect(0, 0, width, height);
42     return g;
43     }
44     public abstract void translate(int x, int y);
45     public abstract Color getColor();
46     public abstract void setColor(Color c);
47     public abstract void setPaintMode();
48     public abstract void setXORMode(Color c1);
49     public abstract Font getFont();
50     public abstract void setFont(Font font);
51     public FontMetrics getFontMetrics() {
52     return getFontMetrics(getFont());
53     }
54     public abstract FontMetrics getFontMetrics(Font f);
55     public abstract Rectangle getClipBounds();
56     public abstract void clipRect(int x, int y, int width, int height);
57     public abstract void setClip(int x, int y, int width, int height);
58     public abstract Shape getClip();
59     public abstract void setClip(Shape clip);
60     public abstract void copyArea(int x, int y, int width, int height,
61                   int dx, int dy);
62     public abstract void drawLine(int x1, int y1, int x2, int y2);
63
64     public void drawRect(int x, int y, int width, int height) {
65     if ((width < 0) || (height < 0)) {
66         return;
67     }
68     if (height == 0 || width == 0) {
69         drawLine(x, y, x + width, y + height);
70     } else {
71         drawLine(x, y, x + width - 1, y);
72         drawLine(x + width, y, x + width, y + height - 1);
73         drawLine(x + width, y + height, x + 1, y + height);
74         drawLine(x, y + height, x, y + 1);
75     }
76     }
77     public abstract void clearRect(int x, int y, int width, int height);
78     public abstract void drawRoundRect(int x, int y, int width, int height,
79                        int arcWidth, int arcHeight);
80     public abstract void fillRect(int x, int y, int width, int height);
81     public abstract void fillRoundRect(int x, int y, int width, int height,
82                        int arcWidth, int arcHeight);
83     public void draw3DRect(int x, int y, int width, int height,
84                boolean raised) {
85     Color c = getColor();
86     Color brighter = c.brighter();
87     Color darker = c.darker();
88
89     setColor(raised ? brighter : darker);
90     drawLine(x, y, x, y + height);
91     drawLine(x + 1, y, x + width - 1, y);
92     setColor(raised ? darker : brighter);
93     drawLine(x + 1, y + height, x + width, y + height);
94     drawLine(x + width, y, x + width, y + height - 1);
95     setColor(c);
96     }
97     public void fill3DRect(int x, int y, int width, int height,
98                boolean raised) {
99     Color c = getColor();
100     Color brighter = c.brighter();
101     Color darker = c.darker();
102
103     if (!raised) {
104         setColor(darker);
105     }
106     fillRect(x+1, y+1, width-2, height-2);
107     setColor(raised ? brighter : darker);
108     drawLine(x, y, x, y + height - 1);
109     drawLine(x + 1, y, x + width - 2, y);
110     setColor(raised ? darker : brighter);
111     drawLine(x + 1, y + height - 1, x + width - 1, y + height - 1);
112     drawLine(x + width - 1, y, x + width - 1, y + height - 2);
113     setColor(c);
114     }
115
116     public abstract void drawOval(int x, int y, int width, int height);
117     public abstract void fillOval(int x, int y, int width, int height);
118     public abstract void drawArc(int x, int y, int width, int height,
119                  int startAngle, int arcAngle);
120     public abstract void fillArc(int x, int y, int width, int height,
121                  int startAngle, int arcAngle);
122     public abstract void drawPolyline(int xPoints[], int yPoints[],
123                       int nPoints);
124     public abstract void drawPolygon(int xPoints[], int yPoints[],
125                      int nPoints);
126     public void drawPolygon(Polygon p) {
127     drawPolygon(p.xpoints, p.ypoints, p.npoints);
128     }
129     public abstract void fillPolygon(int xPoints[], int yPoints[],
130                      int nPoints);
131     public void fillPolygon(Polygon p) {
132     fillPolygon(p.xpoints, p.ypoints, p.npoints);
133     }
134     public abstract void drawString(String JavaDoc str, int x, int y);
135     public void drawChars(char data[], int offset, int length, int x, int y) {
136     drawString(new String JavaDoc(data, offset, length), x, y);
137     }
138     public void drawBytes(byte data[], int offset, int length, int x, int y) {
139     drawString(new String JavaDoc(data).substring(offset, offset+length), x, y);
140     }
141     public abstract boolean drawImage(Image img, int x, int y,
142                       ImageObserver observer);
143     public abstract boolean drawImage(Image img, int x, int y,
144                       int width, int height,
145                       ImageObserver observer);
146     public abstract boolean drawImage(Image img, int x, int y,
147                       Color bgcolor,
148                       ImageObserver observer);
149     public abstract boolean drawImage(Image img, int x, int y,
150                       int width, int height,
151                       Color bgcolor,
152                       ImageObserver observer);
153     public abstract boolean drawImage(Image img,
154                       int dx1, int dy1, int dx2, int dy2,
155                       int sx1, int sy1, int sx2, int sy2,
156                       ImageObserver observer);
157
158     public abstract boolean drawImage(Image img,
159                       int dx1, int dy1, int dx2, int dy2,
160                       int sx1, int sy1, int sx2, int sy2,
161                       Color bgcolor,
162                       ImageObserver observer);
163     public abstract void dispose();
164     public String JavaDoc toString() {
165     return getClass().getName() + "[font=" + getFont() + ",color=" + getColor() + "]";
166     }
167     public Rectangle getClipRect() {
168     return getClipBounds();
169     }
170     public boolean hitClip(int x, int y, int width, int height) {
171     Rectangle clipRect = getClipBounds();
172     if (clipRect == null) {
173         return true;
174     }
175     return clipRect.intersects(x, y, width, height);
176     }
177     public Rectangle getClipBounds(Rectangle r) {
178         Rectangle clipRect = getClipBounds();
179     if (clipRect != null) {
180         r.x = clipRect.x;
181         r.y = clipRect.y;
182         r.width = clipRect.width;
183         r.height = clipRect.height;
184     } else if (r == null) {
185         throw new NullPointerException JavaDoc("null rectangle parameter");
186     }
187         return r;
188     }
189
190     public abstract void redrawUnder(int x, int y, int width, int height);
191 }
192
Popular Tags