1/*
2 * ProGuard -- shrinking, optimization, obfuscation, and preverification
3 *             of Java bytecode.
4 *
5 * Copyright (c) 2002-2014 Eric Lafortune (eric@graphics.cornell.edu)
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the Free
9 * Software Foundation; either version 2 of the License, or (at your option)
10 * any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21package proguard.gui.splash;
22
23import java.awt.*;
24import java.awt.RenderingHints.Key;
25import java.awt.font.*;
26import java.awt.geom.AffineTransform;
27import java.awt.image.*;
28import java.awt.image.renderable.RenderableImage;
29import java.text.AttributedCharacterIterator;
30import java.util.Map;
31
32/**
33 * This Graphics2D allows to fix some basic settings (Color, Font, Paint, Stroke,
34 * XORMode) of a delegate Graphics2D, overriding any subsequent attempts to
35 * change those settings.
36 *
37 * @author Eric Lafortune
38 * @noinspection deprecation
39 */
40final class OverrideGraphics2D extends Graphics2D
41{
42    private final Graphics2D graphics;
43
44    private Color  overrideColor;
45    private Font   overrideFont;
46    private Paint  overridePaint;
47    private Stroke overrideStroke;
48    private Color  overrideXORMode;
49
50    private Color  color;
51    private Font   font;
52    private Paint  paint;
53    private Stroke stroke;
54
55
56    /**
57     * Creates a new OverrideGraphics2D.
58     * @param graphics the delegate Graphics2D.
59     */
60    public OverrideGraphics2D(Graphics2D graphics)
61    {
62        this.graphics = graphics;
63        this.color    = graphics.getColor();
64        this.font     = graphics.getFont();
65        this.paint    = graphics.getPaint();
66        this.stroke   = graphics.getStroke();
67    }
68
69
70    /**
71     * Fixes the Color of the Graphics2D.
72     *
73     * @param color the fixed Color, or <code>null</code> to undo the fixing.
74     */
75    public void setOverrideColor(Color color)
76    {
77        this.overrideColor = color;
78        graphics.setColor(color != null ? color : this.color);
79    }
80
81    /**
82     * Fixes the Font of the Graphics2D.
83     *
84     * @param font the fixed Font, or <code>null</code> to undo the fixing.
85     */
86    public void setOverrideFont(Font font)
87    {
88        this.overrideFont = font;
89        graphics.setFont(font != null ? font : this.font);
90    }
91
92    /**
93     * Fixes the Paint of the Graphics2D.
94     *
95     * @param paint the fixed Paint, or <code>null</code> to undo the fixing.
96     */
97    public void setOverridePaint(Paint paint)
98    {
99        this.overridePaint = paint;
100        graphics.setPaint(paint != null ? paint : this.paint);
101    }
102
103    /**
104     * Fixes the Stroke of the Graphics2D.
105     *
106     * @param stroke the fixed Stroke, or <code>null</code> to undo the fixing.
107     */
108    public void setOverrideStroke(Stroke stroke)
109    {
110        this.overrideStroke = stroke;
111        graphics.setStroke(stroke != null ? stroke : this.stroke);
112    }
113
114    /**
115     * Fixes the XORMode of the Graphics2D.
116     *
117     * @param color the fixed XORMode Color, or <code>null</code> to undo the fixing.
118     */
119    public void setOverrideXORMode(Color color)
120    {
121        this.overrideXORMode = color;
122        if (color != null)
123        {
124            graphics.setXORMode(color);
125        }
126        else
127        {
128            graphics.setPaintMode();
129        }
130    }
131
132
133    // Implementations for Graphics2D.
134
135    public void setColor(Color color)
136    {
137        this.color = color;
138        if (overrideColor == null)
139        {
140            graphics.setColor(color);
141        }
142    }
143
144    public void setFont(Font font)
145    {
146        this.font = font;
147        if (overrideFont == null)
148        {
149            graphics.setFont(font);
150        }
151    }
152
153    public void setPaint(Paint paint)
154    {
155        this.paint = paint;
156        if (overridePaint == null)
157        {
158            graphics.setPaint(paint);
159        }
160    }
161
162    public void setStroke(Stroke stroke)
163    {
164        this.stroke = stroke;
165        if (overrideStroke == null)
166        {
167            graphics.setStroke(stroke);
168        }
169    }
170
171    public void setXORMode(Color color)
172    {
173        if (overrideXORMode == null)
174        {
175            graphics.setXORMode(color);
176        }
177    }
178
179    public void setPaintMode()
180    {
181        if (overrideXORMode == null)
182        {
183            graphics.setPaintMode();
184        }
185    }
186
187
188    public Color getColor()
189    {
190        return overrideColor != null ? color : graphics.getColor();
191    }
192
193    public Font getFont()
194    {
195        return overrideFont != null ? font : graphics.getFont();
196    }
197
198    public Paint getPaint()
199    {
200        return overridePaint != null ? paint : graphics.getPaint();
201    }
202
203    public Stroke getStroke()
204    {
205        return overrideStroke != null ? stroke : graphics.getStroke();
206    }
207
208
209    public Graphics create()
210    {
211        OverrideGraphics2D g = new OverrideGraphics2D((Graphics2D)graphics.create());
212        g.setOverrideColor(overrideColor);
213        g.setOverrideFont(overrideFont);
214        g.setOverridePaint(overridePaint);
215        g.setOverrideStroke(overrideStroke);
216
217        return g;
218    }
219
220    public Graphics create(int x, int y, int width, int height)
221    {
222        OverrideGraphics2D g = new OverrideGraphics2D((Graphics2D)graphics.create(x, y, width, height));
223        g.setOverrideColor(overrideColor);
224        g.setOverrideFont(overrideFont);
225        g.setOverridePaint(overridePaint);
226        g.setOverrideStroke(overrideStroke);
227
228        return g;
229    }
230
231
232    // Delegation for Graphics2D
233
234    public void addRenderingHints(Map hints)
235    {
236        graphics.addRenderingHints(hints);
237    }
238
239    public void clearRect(int x, int y, int width, int height)
240    {
241        graphics.clearRect(x, y, width, height);
242    }
243
244    public void clip(Shape s)
245    {
246        graphics.clip(s);
247    }
248
249    public void clipRect(int x, int y, int width, int height)
250    {
251        graphics.clipRect(x, y, width, height);
252    }
253
254    public void copyArea(int x, int y, int width, int height, int dx, int dy)
255    {
256        graphics.copyArea(x, y, width, height, dx, dy);
257    }
258
259    public void dispose()
260    {
261        graphics.dispose();
262    }
263
264    public void draw(Shape s)
265    {
266        graphics.draw(s);
267    }
268
269    public void draw3DRect(int x, int y, int width, int height, boolean raised)
270    {
271        graphics.draw3DRect(x, y, width, height, raised);
272    }
273
274    public void drawArc(int x, int y, int width, int height, int startAngle, int arcAngle)
275    {
276        graphics.drawArc(x, y, width, height, startAngle, arcAngle);
277    }
278
279    public void drawBytes(byte[] data, int offset, int length, int x, int y)
280    {
281        graphics.drawBytes(data, offset, length, x, y);
282    }
283
284    public void drawChars(char[] data, int offset, int length, int x, int y)
285    {
286        graphics.drawChars(data, offset, length, x, y);
287    }
288
289    public void drawGlyphVector(GlyphVector g, float x, float y)
290    {
291        graphics.drawGlyphVector(g, x, y);
292    }
293
294    public boolean drawImage(Image img, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2, Color bgcolor, ImageObserver observer)
295    {
296        return graphics.drawImage(img, dx1, dy1, dx2, dy2, sx1, sy1, sx2, sy2, bgcolor, observer);
297    }
298
299    public boolean drawImage(Image img, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2, ImageObserver observer)
300    {
301        return graphics.drawImage(img, dx1, dy1, dx2, dy2, sx1, sy1, sx2, sy2, observer);
302    }
303
304    public boolean drawImage(Image img, int x, int y, int width, int height, Color bgcolor, ImageObserver observer)
305    {
306        return graphics.drawImage(img, x, y, width, height, bgcolor, observer);
307    }
308
309    public boolean drawImage(Image img, int x, int y, int width, int height, ImageObserver observer)
310    {
311        return graphics.drawImage(img, x, y, width, height, observer);
312    }
313
314    public boolean drawImage(Image img, int x, int y, Color bgcolor, ImageObserver observer)
315    {
316        return graphics.drawImage(img, x, y, bgcolor, observer);
317    }
318
319    public boolean drawImage(Image img, int x, int y, ImageObserver observer)
320    {
321        return graphics.drawImage(img, x, y, observer);
322    }
323
324    public boolean drawImage(Image img, AffineTransform xform, ImageObserver obs)
325    {
326        return graphics.drawImage(img, xform, obs);
327    }
328
329    public void drawImage(BufferedImage img, BufferedImageOp op, int x, int y)
330    {
331        graphics.drawImage(img, op, x, y);
332    }
333
334    public void drawLine(int x1, int y1, int x2, int y2)
335    {
336        graphics.drawLine(x1, y1, x2, y2);
337    }
338
339    public void drawOval(int x, int y, int width, int height)
340    {
341        graphics.drawOval(x, y, width, height);
342    }
343
344    public void drawPolygon(int[] xPoints, int[] yPoints, int nPoints)
345    {
346        graphics.drawPolygon(xPoints, yPoints, nPoints);
347    }
348
349    public void drawPolygon(Polygon p)
350    {
351        graphics.drawPolygon(p);
352    }
353
354    public void drawPolyline(int[] xPoints, int[] yPoints, int nPoints)
355    {
356        graphics.drawPolyline(xPoints, yPoints, nPoints);
357    }
358
359    public void drawRect(int x, int y, int width, int height)
360    {
361        graphics.drawRect(x, y, width, height);
362    }
363
364    public void drawRenderableImage(RenderableImage img, AffineTransform xform)
365    {
366        graphics.drawRenderableImage(img, xform);
367    }
368
369    public void drawRenderedImage(RenderedImage img, AffineTransform xform)
370    {
371        graphics.drawRenderedImage(img, xform);
372    }
373
374    public void drawRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight)
375    {
376        graphics.drawRoundRect(x, y, width, height, arcWidth, arcHeight);
377    }
378
379    public void drawString(String s, float x, float y)
380    {
381        graphics.drawString(s, x, y);
382    }
383
384    public void drawString(String str, int x, int y)
385    {
386        graphics.drawString(str, x, y);
387    }
388
389    public void drawString(AttributedCharacterIterator iterator, float x, float y)
390    {
391        graphics.drawString(iterator, x, y);
392    }
393
394    public void drawString(AttributedCharacterIterator iterator, int x, int y)
395    {
396        graphics.drawString(iterator, x, y);
397    }
398
399    public boolean equals(Object obj)
400    {
401        return graphics.equals(obj);
402    }
403
404    public void fill(Shape s)
405    {
406        graphics.fill(s);
407    }
408
409    public void fill3DRect(int x, int y, int width, int height, boolean raised)
410    {
411        graphics.fill3DRect(x, y, width, height, raised);
412    }
413
414    public void fillArc(int x, int y, int width, int height, int startAngle, int arcAngle)
415    {
416        graphics.fillArc(x, y, width, height, startAngle, arcAngle);
417    }
418
419    public void fillOval(int x, int y, int width, int height)
420    {
421        graphics.fillOval(x, y, width, height);
422    }
423
424    public void fillPolygon(int[] xPoints, int[] yPoints, int nPoints)
425    {
426        graphics.fillPolygon(xPoints, yPoints, nPoints);
427    }
428
429    public void fillPolygon(Polygon p)
430    {
431        graphics.fillPolygon(p);
432    }
433
434    public void fillRect(int x, int y, int width, int height)
435    {
436        graphics.fillRect(x, y, width, height);
437    }
438
439    public void fillRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight)
440    {
441        graphics.fillRoundRect(x, y, width, height, arcWidth, arcHeight);
442    }
443
444    public Color getBackground()
445    {
446        return graphics.getBackground();
447    }
448
449    public Shape getClip()
450    {
451        return graphics.getClip();
452    }
453
454    public Rectangle getClipBounds()
455    {
456        return graphics.getClipBounds();
457    }
458
459    public Rectangle getClipBounds(Rectangle r)
460    {
461        return graphics.getClipBounds(r);
462    }
463
464    public Rectangle getClipRect()
465    {
466        return graphics.getClipRect();
467    }
468
469    public Composite getComposite()
470    {
471        return graphics.getComposite();
472    }
473
474    public GraphicsConfiguration getDeviceConfiguration()
475    {
476        return graphics.getDeviceConfiguration();
477    }
478
479    public FontMetrics getFontMetrics()
480    {
481        return graphics.getFontMetrics();
482    }
483
484    public FontMetrics getFontMetrics(Font f)
485    {
486        return graphics.getFontMetrics(f);
487    }
488
489    public FontRenderContext getFontRenderContext()
490    {
491        return graphics.getFontRenderContext();
492    }
493
494    public Object getRenderingHint(Key hintKey)
495    {
496        return graphics.getRenderingHint(hintKey);
497    }
498
499    public RenderingHints getRenderingHints()
500    {
501        return graphics.getRenderingHints();
502    }
503
504    public AffineTransform getTransform()
505    {
506        return graphics.getTransform();
507    }
508
509    public int hashCode()
510    {
511        return graphics.hashCode();
512    }
513
514    public boolean hit(Rectangle rect, Shape s, boolean onStroke)
515    {
516        return graphics.hit(rect, s, onStroke);
517    }
518
519    public boolean hitClip(int x, int y, int width, int height)
520    {
521        return graphics.hitClip(x, y, width, height);
522    }
523
524    public void rotate(double theta)
525    {
526        graphics.rotate(theta);
527    }
528
529    public void rotate(double theta, double x, double y)
530    {
531        graphics.rotate(theta, x, y);
532    }
533
534    public void scale(double sx, double sy)
535    {
536        graphics.scale(sx, sy);
537    }
538
539    public void setBackground(Color color)
540    {
541        graphics.setBackground(color);
542    }
543
544    public void setClip(int x, int y, int width, int height)
545    {
546        graphics.setClip(x, y, width, height);
547    }
548
549    public void setClip(Shape clip)
550    {
551        graphics.setClip(clip);
552    }
553
554    public void setComposite(Composite comp)
555    {
556        graphics.setComposite(comp);
557    }
558
559    public void setRenderingHint(Key hintKey, Object hintValue)
560    {
561        graphics.setRenderingHint(hintKey, hintValue);
562    }
563
564    public void setRenderingHints(Map hints)
565    {
566        graphics.setRenderingHints(hints);
567    }
568
569    public void setTransform(AffineTransform Tx)
570    {
571        graphics.setTransform(Tx);
572    }
573
574    public void shear(double shx, double shy)
575    {
576        graphics.shear(shx, shy);
577    }
578
579    public String toString()
580    {
581        return graphics.toString();
582    }
583
584    public void transform(AffineTransform Tx)
585    {
586        graphics.transform(Tx);
587    }
588
589    public void translate(double tx, double ty)
590    {
591        graphics.translate(tx, ty);
592    }
593
594    public void translate(int x, int y)
595    {
596        graphics.translate(x, y);
597    }
598}
599