summaryrefslogtreecommitdiffstats
path: root/awt/java/awt/Graphics2D.java
blob: 04a7319de851d915cadc8b9e47a50014f15ede05 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
/*
 *  Licensed to the Apache Software Foundation (ASF) under one or more
 *  contributor license agreements.  See the NOTICE file distributed with
 *  this work for additional information regarding copyright ownership.
 *  The ASF licenses this file to You under the Apache License, Version 2.0
 *  (the "License"); you may not use this file except in compliance with
 *  the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package java.awt;

import java.awt.font.GlyphVector;
import java.awt.font.FontRenderContext;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.awt.image.BufferedImageOp;
import java.awt.image.ImageObserver;
import java.awt.image.RenderedImage;
import java.awt.image.renderable.RenderableImage;
import java.text.AttributedCharacterIterator;
import java.util.Map;

/**
 * The Graphics2D class extends Graphics class and provides more capabilities
 * for rendering text, images, shapes. This provides methods to perform
 * transformation of coordinate system, color management, and text layout. The
 * following attributes exist for rendering:
 * <ul>
 * <li>Color - current Graphics2D color;</li>
 * <li>Font - current Graphics2D font;</li>
 * <li>Stroke - pen with a width of 1 pixel;</li>
 * <li>Transform - current Graphics2D Transformation;</li>
 * <li>Composite - alpha compositing rules for combining source and destination
 * colors.</li>
 * </ul>
 * 
 * @since Android 1.0
 */
public abstract class Graphics2D extends Graphics {

    /**
     * Instantiates a new Graphics2D object. This constructor should never be
     * called directly.
     */
    protected Graphics2D() {
        super();
    }

    /**
     * Adds preferences for the rendering algorithms. The preferences are
     * arbitrary and specified by Map objects. All specified by Map object
     * preferences can be modified.
     * 
     * @param hints
     *            the rendering hints.
     */
    public abstract void addRenderingHints(Map<?, ?> hints);

    /**
     * Intersects the current clipping area with the specified Shape and the
     * result becomes a new clipping area. If current clipping area is not
     * defined, the Shape becomes the new clipping area. No rendering operations
     * are allowed outside the clipping area.
     * 
     * @param s
     *            the specified Shape object which will be intersected with
     *            current clipping area.
     */
    public abstract void clip(Shape s);

    /**
     * Draws the outline of the specified Shape.
     * 
     * @param s
     *            the Shape which outline is drawn.
     */
    public abstract void draw(Shape s);

    /**
     * Draws the specified GlyphVector object's text at the point x, y.
     * 
     * @param g
     *            the GlyphVector object to be drawn.
     * @param x
     *            the X position where the GlyphVector's text should be
     *            rendered.
     * @param y
     *            the Y position where the GlyphVector's text should be
     *            rendered.
     */
    public abstract void drawGlyphVector(GlyphVector g, float x, float y);

    /**
     * Draws the BufferedImage -- modified according to the operation
     * BufferedImageOp -- at the point x, y.
     * 
     * @param img
     *            the BufferedImage to be rendered.
     * @param op
     *            the filter to be applied to the image before rendering.
     * @param x
     *            the X coordinate of the point where the image's upper left
     *            corner will be placed.
     * @param y
     *            the Y coordinate of the point where the image's upper left
     *            corner will be placed.
     */
    public abstract void drawImage(BufferedImage img, BufferedImageOp op, int x, int y);

    /**
     * Draws BufferedImage transformed from image space into user space
     * according to the AffineTransform xform and notifies the ImageObserver.
     * 
     * @param img
     *            the BufferedImage to be rendered.
     * @param xform
     *            the affine transformation from the image to the user space.
     * @param obs
     *            the ImageObserver to be notified about the image conversion.
     * @return true, if the image is successfully loaded and rendered, or it's
     *         null, otherwise false.
     */
    public abstract boolean drawImage(Image img, AffineTransform xform, ImageObserver obs);

    /**
     * Draws a RenderableImage which is transformed from image space into user
     * according to the AffineTransform xform.
     * 
     * @param img
     *            the RenderableImage to be rendered.
     * @param xform
     *            the affine transformation from image to user space.
     */
    public abstract void drawRenderableImage(RenderableImage img, AffineTransform xform);

    /**
     * Draws a RenderedImage which is transformed from image space into user
     * according to the AffineTransform xform.
     * 
     * @param img
     *            the RenderedImage to be rendered.
     * @param xform
     *            the affine transformation from image to user space.
     */
    public abstract void drawRenderedImage(RenderedImage img, AffineTransform xform);

    /**
     * Draws the string specified by the AttributedCharacterIterator. The first
     * character's position is specified by the X, Y parameters.
     * 
     * @param iterator
     *            whose text is drawn.
     * @param x
     *            the X position where the first character is drawn.
     * @param y
     *            the Y position where the first character is drawn.
     */
    public abstract void drawString(AttributedCharacterIterator iterator, float x, float y);

    /**
     * Draws the string specified by the AttributedCharacterIterator. The first
     * character's position is specified by the X, Y parameters.
     * 
     * @param iterator
     *            whose text is drawn.
     * @param x
     *            the X position where the first character is drawn.
     * @param y
     *            the Y position where the first character is drawn.
     * @see java.awt.Graphics#drawString(AttributedCharacterIterator, int, int)
     */
    @Override
    public abstract void drawString(AttributedCharacterIterator iterator, int x, int y);

    /**
     * Draws the String whose the first character position is specified by the
     * parameters X, Y.
     * 
     * @param s
     *            the String to be drawn.
     * @param x
     *            the X position of the first character.
     * @param y
     *            the Y position of the first character.
     */
    public abstract void drawString(String s, float x, float y);

    /**
     * Draws the String whose the first character coordinates are specified by
     * the parameters X, Y.
     * 
     * @param str
     *            the String to be drawn.
     * @param x
     *            the X coordinate of the first character.
     * @param y
     *            the Y coordinate of the first character.
     * @see java.awt.Graphics#drawString(String, int, int)
     */
    @Override
    public abstract void drawString(String str, int x, int y);

    /**
     * Fills the interior of the specified Shape.
     * 
     * @param s
     *            the Shape to be filled.
     */
    public abstract void fill(Shape s);

    /**
     * Gets the background color.
     * 
     * @return the current background color.
     */
    public abstract Color getBackground();

    /**
     * Gets the current composite of the Graphics2D.
     * 
     * @return the current composite which specifies the compositing style.
     */
    public abstract Composite getComposite();

    /**
     * Gets the device configuration.
     * 
     * @return the device configuration.
     */
    public abstract GraphicsConfiguration getDeviceConfiguration();

    /**
     * Gets the rendering context of the Font.
     * 
     * @return the FontRenderContext.
     */
    public abstract FontRenderContext getFontRenderContext();

    /**
     * Gets the current Paint of Graphics2D.
     * 
     * @return the current Paint of Graphics2D.
     */
    public abstract Paint getPaint();

    /**
     * Gets the value of single preference for specified key.
     * 
     * @param key
     *            the specified key of the rendering hint.
     * @return the value of rendering hint for specified key.
     */
    public abstract Object getRenderingHint(RenderingHints.Key key);

    /**
     * Gets the set of the rendering preferences as a collection of key/value
     * pairs.
     * 
     * @return the RenderingHints which contains the rendering preferences.
     */
    public abstract RenderingHints getRenderingHints();

    /**
     * Gets current stroke of the Graphics2D.
     * 
     * @return current stroke of the Graphics2D.
     */
    public abstract Stroke getStroke();

    /**
     * Gets current affine transform of the Graphics2D.
     * 
     * @return current AffineTransform of the Graphics2D.
     */
    public abstract AffineTransform getTransform();

    /**
     * Determines whether or not the specified Shape intersects the specified
     * Rectangle. If the onStroke parameter is true, this method checks whether
     * or not the specified Shape outline intersects the specified Rectangle,
     * otherwise this method checks whether or not the specified Shape's
     * interior intersects the specified Rectangle.
     * 
     * @param rect
     *            the specified Rectangle.
     * @param s
     *            the Shape to check for intersection.
     * @param onStroke
     *            the parameter determines whether or not this method checks for
     *            intersection of the Shape outline or of the Shape interior
     *            with the Rectangle.
     * @return true, if there is a hit, false otherwise.
     */
    public abstract boolean hit(Rectangle rect, Shape s, boolean onStroke);

    /**
     * Performs a rotation transform relative to current Graphics2D Transform.
     * The coordinate system is rotated by the specified angle in radians
     * relative to current origin.
     * 
     * @param theta
     *            the angle of rotation in radians.
     */
    public abstract void rotate(double theta);

    /**
     * Performs a translated rotation transform relative to current Graphics2D
     * Transform. The coordinate system is rotated by the specified angle in
     * radians relative to current origin and then moved to point (x, y). Is
     * this right?
     * 
     * @param theta
     *            the angle of rotation in radians.
     * @param x
     *            the X coordinate.
     * @param y
     *            the Y coordinate.
     */
    public abstract void rotate(double theta, double x, double y);

    /**
     * Performs a linear scale transform relative to current Graphics2D
     * Transform. The coordinate system is rescaled vertically and horizontally
     * by the specified parameters.
     * 
     * @param sx
     *            the scaling factor by which the X coordinate is multiplied.
     * @param sy
     *            the scaling factor by which the Y coordinate is multiplied.
     */
    public abstract void scale(double sx, double sy);

    /**
     * Sets a new background color for clearing rectangular areas. The clearRect
     * method uses the current background color.
     * 
     * @param color
     *            the new background color.
     */
    public abstract void setBackground(Color color);

    /**
     * Sets the current composite for Graphics2D.
     * 
     * @param comp
     *            the Composite object.
     */
    public abstract void setComposite(Composite comp);

    /**
     * Sets the paint for Graphics2D.
     * 
     * @param paint
     *            the Paint object.
     */
    public abstract void setPaint(Paint paint);

    /**
     * Sets a key-value pair in the current RenderingHints map.
     * 
     * @param key
     *            the key of the rendering hint to set.
     * @param value
     *            the value to set for the rendering hint.
     */
    public abstract void setRenderingHint(RenderingHints.Key key, Object value);

    /**
     * Replaces the current rendering hints with the specified rendering
     * preferences.
     * 
     * @param hints
     *            the new Map of rendering hints.
     */
    public abstract void setRenderingHints(Map<?, ?> hints);

    /**
     * Sets the stroke for the Graphics2D.
     * 
     * @param s
     *            the Stroke object.
     */
    public abstract void setStroke(Stroke s);

    /**
     * Overwrite the current Transform of the Graphics2D. The specified
     * Transform should be received from the getTransform() method and should be
     * used only for restoring the original Graphics2D transform after calling
     * draw or fill methods.
     * 
     * @param Tx
     *            the specified Transform.
     */
    public abstract void setTransform(AffineTransform Tx);

    /**
     * Performs a shear transform relative to current Graphics2D Transform. The
     * coordinate system is shifted by the specified multipliers relative to
     * current position.
     * 
     * @param shx
     *            the multiplier by which the X coordinates shift position along
     *            X axis as a function of Y coordinates.
     * @param shy
     *            the multiplier by which the Y coordinates shift position along
     *            Y axis as a function of X coordinates.
     */
    public abstract void shear(double shx, double shy);

    /**
     * Concatenates the AffineTransform object with current Transform of this
     * Graphics2D. The transforms are applied in reverse order with the last
     * specified transform applied first and the next transformation applied to
     * the result of previous transformation. More precisely, if Cx is the
     * current Graphics2D transform, the transform method's result with Tx as
     * the parameter is the transformation Rx, where Rx(p) = Cx(Tx(p)), for p -
     * a point in current coordinate system. Rx becomes the current Transform
     * for this Graphics2D.
     * 
     * @param Tx
     *            the AffineTransform object to be concatenated with current
     *            Transform.
     */
    public abstract void transform(AffineTransform Tx);

    /**
     * Performs a translate transform relative to current Graphics2D Transform.
     * The coordinate system is moved by the specified distance relative to
     * current position.
     * 
     * @param tx
     *            the translation distance along the X axis.
     * @param ty
     *            the translation distance along the Y axis.
     */
    public abstract void translate(double tx, double ty);

    /**
     * Moves the origin Graphics2D Transform to the point with x, y coordinates
     * in current coordinate system. The new origin of coordinate system is
     * moved to the (x, y) point accordingly. All rendering and transform
     * operations are performed relative to this new origin.
     * 
     * @param x
     *            the X coordinate.
     * @param y
     *            the Y coordinate.
     * @see java.awt.Graphics#translate(int, int)
     */
    @Override
    public abstract void translate(int x, int y);

    /**
     * Fills a 3D rectangle with the current color. The rectangle is specified
     * by its width, height, and top left corner coordinates.
     * 
     * @param x
     *            the X coordinate of the rectangle's top left corner.
     * @param y
     *            the Y coordinate of the rectangle's top left corner.
     * @param width
     *            the width of rectangle.
     * @param height
     *            the height of rectangle.
     * @param raised
     *            a boolean value that determines whether the rectangle is drawn
     *            as raised or indented.
     * @see java.awt.Graphics#fill3DRect(int, int, int, int, boolean)
     */
    @Override
    public void fill3DRect(int x, int y, int width, int height, boolean raised) {
        // According to the spec, color should be used instead of paint,
        // so Graphics.fill3DRect resets paint and
        // it should be restored after the call
        Paint savedPaint = getPaint();
        super.fill3DRect(x, y, width, height, raised);
        setPaint(savedPaint);
    }

    /**
     * Draws the highlighted outline of a rectangle.
     * 
     * @param x
     *            the X coordinate of the rectangle's top left corner.
     * @param y
     *            the Y coordinate of the rectangle's top left corner.
     * @param width
     *            the width of rectangle.
     * @param height
     *            the height of rectangle.
     * @param raised
     *            a boolean value that determines whether the rectangle is drawn
     *            as raised or indented.
     * @see java.awt.Graphics#draw3DRect(int, int, int, int, boolean)
     */
    @Override
    public void draw3DRect(int x, int y, int width, int height, boolean raised) {
        // According to the spec, color should be used instead of paint,
        // so Graphics.draw3DRect resets paint and
        // it should be restored after the call
        Paint savedPaint = getPaint();
        super.draw3DRect(x, y, width, height, raised);
        setPaint(savedPaint);
    }
}