001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: /**
018: * @author Alexey A. Petrenko
019: * @version $Revision$
020: */package java.awt;
021:
022: import java.awt.image.ImageObserver;
023: import java.text.AttributedCharacterIterator;
024:
025: public abstract class Graphics {
026:
027: // Constructors
028:
029: protected Graphics() {
030: }
031:
032: // Public methods
033:
034: public Graphics create(int x, int y, int width, int height) {
035: Graphics res = create();
036: res.translate(x, y);
037: res.clipRect(0, 0, width, height);
038: return res;
039: }
040:
041: public void draw3DRect(int x, int y, int width, int height,
042: boolean raised) {
043: // Note: lighter/darker colors should be used to draw 3d rect.
044: // The resulting rect is (width+1)x(height+1). Stroke and paint attributes of
045: // the Graphics2D should be reset to the default values.
046: // fillRect is used instead of drawLine to bypass stroke
047: // reset/set and rasterization.
048:
049: Color color = getColor();
050: Color colorUp, colorDown;
051: if (raised) {
052: colorUp = color.brighter();
053: colorDown = color.darker();
054: } else {
055: colorUp = color.darker();
056: colorDown = color.brighter();
057: }
058:
059: setColor(colorUp);
060: fillRect(x, y, width, 1);
061: fillRect(x, y + 1, 1, height);
062:
063: setColor(colorDown);
064: fillRect(x + width, y, 1, height);
065: fillRect(x + 1, y + height, width, 1);
066: }
067:
068: public void drawBytes(byte[] bytes, int off, int len, int x, int y) {
069: drawString(new String(bytes, off, len), x, y);
070: }
071:
072: public void drawChars(char[] chars, int off, int len, int x, int y) {
073: drawString(new String(chars, off, len), x, y);
074: }
075:
076: public void drawPolygon(Polygon p) {
077: drawPolygon(p.xpoints, p.ypoints, p.npoints);
078: }
079:
080: public void drawRect(int x, int y, int width, int height) {
081: int[] xpoints = { x, x, x + width, x + width };
082: int[] ypoints = { y, y + height, y + height, y };
083:
084: drawPolygon(xpoints, ypoints, 4);
085: }
086:
087: public void fill3DRect(int x, int y, int width, int height,
088: boolean raised) {
089: // Note: lighter/darker colors should be used to draw 3d rect.
090: // The resulting rect is (width)x(height), same as fillRect.
091: // Stroke and paint attributes of the Graphics2D should be reset
092: // to the default values. fillRect is used instead of drawLine to
093: // bypass stroke reset/set and line rasterization.
094:
095: Color color = getColor();
096: Color colorUp, colorDown;
097: if (raised) {
098: colorUp = color.brighter();
099: colorDown = color.darker();
100: setColor(color);
101: } else {
102: colorUp = color.darker();
103: colorDown = color.brighter();
104: setColor(colorUp);
105: }
106:
107: width--;
108: height--;
109: fillRect(x + 1, y + 1, width - 1, height - 1);
110:
111: setColor(colorUp);
112: fillRect(x, y, width, 1);
113: fillRect(x, y + 1, 1, height);
114:
115: setColor(colorDown);
116: fillRect(x + width, y, 1, height);
117: fillRect(x + 1, y + height, width, 1);
118: }
119:
120: public void fillPolygon(Polygon p) {
121: fillPolygon(p.xpoints, p.ypoints, p.npoints);
122: }
123:
124: @Override
125: public void finalize() {
126: }
127:
128: public Rectangle getClipBounds(Rectangle r) {
129: Shape clip = getClip();
130:
131: if (clip != null) {
132: // TODO: Can we get shape bounds without creating Rectangle object?
133: Rectangle b = clip.getBounds();
134: r.x = b.x;
135: r.y = b.y;
136: r.width = b.width;
137: r.height = b.height;
138: }
139:
140: return r;
141: }
142:
143: /**
144: * @deprecated Use {@link #getClipBounds()}
145: */
146: @Deprecated
147: public Rectangle getClipRect() {
148: return getClipBounds();
149: }
150:
151: public FontMetrics getFontMetrics() {
152: return getFontMetrics(getFont());
153: }
154:
155: public boolean hitClip(int x, int y, int width, int height) {
156: // TODO: Create package private method Rectangle.intersects(int, int, int, int);
157: return getClipBounds().intersects(
158: new Rectangle(x, y, width, height));
159: }
160:
161: @Override
162: public String toString() {
163: // TODO: Think about string representation of Graphics.
164: return "Graphics"; //$NON-NLS-1$
165: }
166:
167: // Abstract methods
168:
169: public abstract void clearRect(int x, int y, int width, int height);
170:
171: public abstract void clipRect(int x, int y, int width, int height);
172:
173: public abstract void copyArea(int sx, int sy, int width,
174: int height, int dx, int dy);
175:
176: public abstract Graphics create();
177:
178: public abstract void dispose();
179:
180: public abstract void drawArc(int x, int y, int width, int height,
181: int sa, int ea);
182:
183: public abstract boolean drawImage(Image img, int x, int y,
184: Color bgcolor, ImageObserver observer);
185:
186: public abstract boolean drawImage(Image img, int x, int y,
187: ImageObserver observer);
188:
189: public abstract boolean drawImage(Image img, int x, int y,
190: int width, int height, Color bgcolor, ImageObserver observer);
191:
192: public abstract boolean drawImage(Image img, int x, int y,
193: int width, int height, ImageObserver observer);
194:
195: public abstract boolean drawImage(Image img, int dx1, int dy1,
196: int dx2, int dy2, int sx1, int sy1, int sx2, int sy2,
197: Color bgcolor, ImageObserver observer);
198:
199: public abstract boolean drawImage(Image img, int dx1, int dy1,
200: int dx2, int dy2, int sx1, int sy1, int sx2, int sy2,
201: ImageObserver observer);
202:
203: public abstract void drawLine(int x1, int y1, int x2, int y2);
204:
205: public abstract void drawOval(int x, int y, int width, int height);
206:
207: public abstract void drawPolygon(int[] xpoints, int[] ypoints,
208: int npoints);
209:
210: public abstract void drawPolyline(int[] xpoints, int[] ypoints,
211: int npoints);
212:
213: public abstract void drawRoundRect(int x, int y, int width,
214: int height, int arcWidth, int arcHeight);
215:
216: public abstract void drawString(
217: AttributedCharacterIterator iterator, int x, int y);
218:
219: public abstract void drawString(String str, int x, int y);
220:
221: public abstract void fillArc(int x, int y, int width, int height,
222: int sa, int ea);
223:
224: public abstract void fillOval(int x, int y, int width, int height);
225:
226: public abstract void fillPolygon(int[] xpoints, int[] ypoints,
227: int npoints);
228:
229: public abstract void fillRect(int x, int y, int width, int height);
230:
231: public abstract void fillRoundRect(int x, int y, int width,
232: int height, int arcWidth, int arcHeight);
233:
234: public abstract Shape getClip();
235:
236: public abstract Rectangle getClipBounds();
237:
238: public abstract Color getColor();
239:
240: public abstract Font getFont();
241:
242: public abstract FontMetrics getFontMetrics(Font font);
243:
244: public abstract void setClip(int x, int y, int width, int height);
245:
246: public abstract void setClip(Shape clip);
247:
248: public abstract void setColor(Color c);
249:
250: public abstract void setFont(Font font);
251:
252: public abstract void setPaintMode();
253:
254: public abstract void setXORMode(Color color);
255:
256: public abstract void translate(int x, int y);
257: }
|