1 /*******************************************************************************
2 * Copyright (c) 2020 Association for Decentralized Information Management in
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * Semantum Oy - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.scenegraph.g2d.color;
14 import java.awt.Color;
15 import java.awt.Composite;
17 import java.awt.FontMetrics;
18 import java.awt.Graphics;
19 import java.awt.Graphics2D;
20 import java.awt.GraphicsConfiguration;
21 import java.awt.Image;
22 import java.awt.Paint;
23 import java.awt.Polygon;
24 import java.awt.Rectangle;
25 import java.awt.RenderingHints;
26 import java.awt.RenderingHints.Key;
27 import java.awt.Shape;
28 import java.awt.Stroke;
29 import java.awt.font.FontRenderContext;
30 import java.awt.font.GlyphVector;
31 import java.awt.geom.AffineTransform;
32 import java.awt.image.BufferedImage;
33 import java.awt.image.BufferedImageOp;
34 import java.awt.image.ImageObserver;
35 import java.awt.image.RenderedImage;
36 import java.awt.image.renderable.RenderableImage;
37 import java.text.AttributedCharacterIterator;
40 import com.kitfox.svg.batik.LinearGradientPaint;
41 import com.kitfox.svg.batik.RadialGradientPaint;
44 * Modified methods: setPaint, setColor.
45 * Colors of bitmap images must be handled externally.
47 public class Graphics2DWithColorFilter extends Graphics2D {
48 private Graphics2D g2d;
49 private ColorFilter colorFilter;
51 public Graphics2DWithColorFilter(Graphics2D g2d, ColorFilter colorFilter) {
53 this.colorFilter = colorFilter;
56 public void setColorFilter(ColorFilter colorFilter) {
57 this.colorFilter = colorFilter;
60 public Color filter(Color c) {
61 if (colorFilter != null) {
62 return colorFilter.filter(c);
68 public void setColor(Color c) {
69 g2d.setColor(filter(c));
72 public void setPaint(Paint paint) {
73 if (paint instanceof Color) {
74 g2d.setColor(filter((Color)paint));
75 } else if (paint instanceof LinearGradientPaint) {
76 LinearGradientPaint lgp = (LinearGradientPaint)paint;
77 Color[] colors = new Color[lgp.getColors().length];
78 for (int i = 0; i < lgp.getColors().length; i++) {
79 colors[i] = filter(lgp.getColors()[i]);
81 LinearGradientPaint lgp2 = new LinearGradientPaint(lgp.getStartPoint(), lgp.getEndPoint(), lgp.getFractions(), colors);
83 } else if (paint instanceof RadialGradientPaint) {
84 RadialGradientPaint rgp = (RadialGradientPaint)paint;
85 Color[] colors = new Color[rgp.getColors().length];
86 for (int i = 0; i < rgp.getColors().length; i++) {
87 colors[i] = filter(rgp.getColors()[i]);
89 RadialGradientPaint rgp2 = new RadialGradientPaint(rgp.getCenterPoint(), rgp.getRadius(), rgp.getFractions(), colors);
92 // Not implemented yet. Show red!
93 Color c2 = new Color(255, 0, 0, 255);
98 public int hashCode() {
99 return g2d.hashCode();
101 public boolean equals(Object obj) {
102 return g2d.equals(obj);
104 public Graphics create() {
107 public Graphics create(int x, int y, int width, int height) {
108 return g2d.create(x, y, width, height);
110 public Color getColor() {
111 return g2d.getColor();
113 public void setPaintMode() {
116 public void setXORMode(Color c1) {
119 public Font getFont() {
120 return g2d.getFont();
122 public void setFont(Font font) {
125 public FontMetrics getFontMetrics() {
126 return g2d.getFontMetrics();
128 public FontMetrics getFontMetrics(Font f) {
129 return g2d.getFontMetrics(f);
131 public Rectangle getClipBounds() {
132 return g2d.getClipBounds();
134 public void clipRect(int x, int y, int width, int height) {
135 g2d.clipRect(x, y, width, height);
137 public void setClip(int x, int y, int width, int height) {
138 g2d.setClip(x, y, width, height);
140 public Shape getClip() {
141 return g2d.getClip();
143 public void setClip(Shape clip) {
146 public void copyArea(int x, int y, int width, int height, int dx, int dy) {
147 g2d.copyArea(x, y, width, height, dx, dy);
149 public void drawLine(int x1, int y1, int x2, int y2) {
150 g2d.drawLine(x1, y1, x2, y2);
152 public void fillRect(int x, int y, int width, int height) {
153 g2d.fillRect(x, y, width, height);
155 public void drawRect(int x, int y, int width, int height) {
156 g2d.drawRect(x, y, width, height);
158 public void draw3DRect(int x, int y, int width, int height, boolean raised) {
159 g2d.draw3DRect(x, y, width, height, raised);
161 public void clearRect(int x, int y, int width, int height) {
162 g2d.clearRect(x, y, width, height);
164 public void drawRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight) {
165 g2d.drawRoundRect(x, y, width, height, arcWidth, arcHeight);
167 public void fill3DRect(int x, int y, int width, int height, boolean raised) {
168 g2d.fill3DRect(x, y, width, height, raised);
170 public void fillRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight) {
171 g2d.fillRoundRect(x, y, width, height, arcWidth, arcHeight);
173 public void draw(Shape s) {
176 public boolean drawImage(Image img, AffineTransform xform, ImageObserver obs) {
177 return g2d.drawImage(img, xform, obs);
179 public void drawImage(BufferedImage img, BufferedImageOp op, int x, int y) {
180 g2d.drawImage(img, op, x, y);
182 public void drawOval(int x, int y, int width, int height) {
183 g2d.drawOval(x, y, width, height);
185 public void drawRenderedImage(RenderedImage img, AffineTransform xform) {
186 g2d.drawRenderedImage(img, xform);
188 public void fillOval(int x, int y, int width, int height) {
189 g2d.fillOval(x, y, width, height);
191 public void drawRenderableImage(RenderableImage img, AffineTransform xform) {
192 g2d.drawRenderableImage(img, xform);
194 public void drawArc(int x, int y, int width, int height, int startAngle, int arcAngle) {
195 g2d.drawArc(x, y, width, height, startAngle, arcAngle);
197 public void drawString(String str, int x, int y) {
198 g2d.drawString(str, x, y);
200 public void fillArc(int x, int y, int width, int height, int startAngle, int arcAngle) {
201 g2d.fillArc(x, y, width, height, startAngle, arcAngle);
203 public void drawString(String str, float x, float y) {
204 g2d.drawString(str, x, y);
206 public void drawString(AttributedCharacterIterator iterator, int x, int y) {
207 g2d.drawString(iterator, x, y);
209 public void drawPolyline(int[] xPoints, int[] yPoints, int nPoints) {
210 g2d.drawPolyline(xPoints, yPoints, nPoints);
212 public void drawPolygon(int[] xPoints, int[] yPoints, int nPoints) {
213 g2d.drawPolygon(xPoints, yPoints, nPoints);
215 public void drawString(AttributedCharacterIterator iterator, float x, float y) {
216 g2d.drawString(iterator, x, y);
218 public void drawPolygon(Polygon p) {
221 public void drawGlyphVector(GlyphVector g, float x, float y) {
222 g2d.drawGlyphVector(g, x, y);
224 public void fillPolygon(int[] xPoints, int[] yPoints, int nPoints) {
225 g2d.fillPolygon(xPoints, yPoints, nPoints);
227 public void fill(Shape s) {
230 public void fillPolygon(Polygon p) {
233 public boolean hit(Rectangle rect, Shape s, boolean onStroke) {
234 return g2d.hit(rect, s, onStroke);
236 public GraphicsConfiguration getDeviceConfiguration() {
237 return g2d.getDeviceConfiguration();
239 public void setComposite(Composite comp) {
240 g2d.setComposite(comp);
242 public void drawChars(char[] data, int offset, int length, int x, int y) {
243 g2d.drawChars(data, offset, length, x, y);
245 public void drawBytes(byte[] data, int offset, int length, int x, int y) {
246 g2d.drawBytes(data, offset, length, x, y);
248 public void setStroke(Stroke s) {
251 public void setRenderingHint(Key hintKey, Object hintValue) {
252 g2d.setRenderingHint(hintKey, hintValue);
254 public boolean drawImage(Image img, int x, int y, ImageObserver observer) {
255 return g2d.drawImage(img, x, y, observer);
257 public Object getRenderingHint(Key hintKey) {
258 return g2d.getRenderingHint(hintKey);
260 public void setRenderingHints(Map<?, ?> hints) {
261 g2d.setRenderingHints(hints);
263 public boolean drawImage(Image img, int x, int y, int width, int height, ImageObserver observer) {
264 return g2d.drawImage(img, x, y, width, height, observer);
266 public void addRenderingHints(Map<?, ?> hints) {
267 g2d.addRenderingHints(hints);
269 public RenderingHints getRenderingHints() {
270 return g2d.getRenderingHints();
272 public void translate(int x, int y) {
275 public boolean drawImage(Image img, int x, int y, Color bgcolor, ImageObserver observer) {
276 return g2d.drawImage(img, x, y, bgcolor, observer);
278 public void translate(double tx, double ty) {
279 g2d.translate(tx, ty);
281 public void rotate(double theta) {
284 public void rotate(double theta, double x, double y) {
285 g2d.rotate(theta, x, y);
287 public boolean drawImage(Image img, int x, int y, int width, int height, Color bgcolor, ImageObserver observer) {
288 return g2d.drawImage(img, x, y, width, height, bgcolor, observer);
290 public void scale(double sx, double sy) {
293 public void shear(double shx, double shy) {
296 public void transform(AffineTransform Tx) {
299 public boolean drawImage(Image img, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2,
300 ImageObserver observer) {
301 return g2d.drawImage(img, dx1, dy1, dx2, dy2, sx1, sy1, sx2, sy2, observer);
303 public void setTransform(AffineTransform Tx) {
304 g2d.setTransform(Tx);
306 public AffineTransform getTransform() {
307 return g2d.getTransform();
309 public Paint getPaint() {
310 return g2d.getPaint();
312 public Composite getComposite() {
313 return g2d.getComposite();
315 public void setBackground(Color color) {
316 g2d.setBackground(color);
318 public boolean drawImage(Image img, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2,
319 Color bgcolor, ImageObserver observer) {
320 return g2d.drawImage(img, dx1, dy1, dx2, dy2, sx1, sy1, sx2, sy2, bgcolor, observer);
322 public Color getBackground() {
323 return g2d.getBackground();
325 public Stroke getStroke() {
326 return g2d.getStroke();
328 public void clip(Shape s) {
331 public FontRenderContext getFontRenderContext() {
332 return g2d.getFontRenderContext();
334 public void dispose() {
337 @SuppressWarnings("deprecation")
338 public void finalize() {
341 public String toString() {
342 return g2d.toString();
344 @SuppressWarnings("deprecation")
345 public Rectangle getClipRect() {
346 return g2d.getClipRect();
348 public boolean hitClip(int x, int y, int width, int height) {
349 return g2d.hitClip(x, y, width, height);
351 public Rectangle getClipBounds(Rectangle r) {
352 return g2d.getClipBounds(r);