Adds possibility to color marks in scatter plots
[scilab.git] / scilab / modules / scirenderer / src / org / scilab / forge / scirenderer / implementation / g2d / texture / G2DTextureDrawingTools.java
1 /*
2  * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3  * Copyright (C) 2009-2011 - DIGITEO - Pierre Lando
4  *
5  * This file must be used under the terms of the CeCILL.
6  * This source file is licensed as described in the file COPYING, which
7  * you should have received as part of this distribution.  The terms
8  * are also available at
9  * http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt
10  */
11
12 package org.scilab.forge.scirenderer.implementation.g2d.texture;
13
14 import org.scilab.forge.scirenderer.implementation.g2d.motor.G2DStroke;
15 import org.scilab.forge.scirenderer.implementation.g2d.texture.G2DTextureManager;
16 import org.scilab.forge.scirenderer.shapes.appearance.Appearance;
17 import org.scilab.forge.scirenderer.shapes.appearance.Color;
18 import org.scilab.forge.scirenderer.texture.Texture;
19 import org.scilab.forge.scirenderer.texture.TextureDrawer;
20 import org.scilab.forge.scirenderer.texture.TextureDrawingTools;
21 import org.scilab.forge.scirenderer.texture.TextEntity;
22
23 import javax.swing.Icon;
24 import javax.swing.JLabel;
25 import java.awt.Dimension;
26 import java.awt.Graphics2D;
27 import java.awt.RenderingHints;
28 import java.awt.font.TextLayout;
29 import java.awt.geom.Ellipse2D;
30 import java.awt.geom.Rectangle2D;
31
32 /**
33  *
34  * Implementation of {@link TextureDrawingTools}.
35  * This implementation create a {@link TextureBufferedImage} an fill it with sprite drawing.
36  *
37  * @author Pierre Lando
38  */
39 public class G2DTextureDrawingTools implements TextureDrawingTools {
40
41     private Graphics2D g2d;
42     private int width;
43     private int height;
44     private java.awt.Color fillColor;
45
46     /**
47      * Default constructor.
48      * @param width the image width.
49      * @param height the image height.
50      * @param useSquareTexture true if square texture is needed.
51      */
52     public G2DTextureDrawingTools(Graphics2D g2d) {
53         this.g2d = g2d;
54     }
55
56     public void setGraphics(Graphics2D g2d) {
57         this.g2d = g2d;
58     }
59
60     public void accept(Texture texture) {
61         accept(texture, null);
62     }
63
64     public void accept(Texture texture, java.awt.Color fillColor) {
65         G2DTextureManager.G2DTexture t = (G2DTextureManager.G2DTexture) texture;
66         TextureDrawer drawer = t.getDrawer();
67         Dimension d = drawer.getTextureSize();
68         this.width = (int) d.getWidth();
69         this.height = (int) d.getHeight();
70         accept(drawer, this.width, this.height, fillColor);
71     }
72
73     /**
74      * Ask this image to accept a sprite drawer.
75      * This image will contain the drawing of the given drawer.
76      * @param spriteDrawer the given sprite drawer.
77      */
78     public void accept(TextureDrawer textureDrawer, int width, int height, java.awt.Color fillColor) {
79         // Change center coordinate to (0, 0).
80         if (textureDrawer.getOriginPosition() == TextureDrawer.OriginPosition.CENTER) {
81             g2d.translate(width / 2.0, height / 2.0);
82         }
83
84         boolean aa = g2d.getRenderingHint(RenderingHints.KEY_ANTIALIASING) == RenderingHints.VALUE_ANTIALIAS_ON;
85         if (!aa) {
86             g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
87         }
88         this.fillColor = fillColor;
89         textureDrawer.draw(this);
90         this.fillColor = null;
91
92         if (!aa) {
93             g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
94         }
95     }
96
97     @Override
98     public void drawPlus(int size, Appearance appearance) {
99         if (size > 1) {
100             int r = size / 2;
101             int[] coords1 = new int[] { -r, 0, r, 0};
102             int[] coords2 = new int[] {0, -r, 0, r};
103
104             drawPolyline(coords1, appearance);
105             drawPolyline(coords2, appearance);
106         } else {
107             fillDisc(0, 0, 1, appearance.getLineColor());
108         }
109     }
110
111     @Override
112     public void drawPolyline(int[] coordinates, Appearance appearance) {
113         if (coordinates.length == 2) {
114
115         }
116
117         int nbPoint = coordinates.length / 2;
118
119         int[] xCoordinates = new int[nbPoint];
120         int[] yCoordinates = new int[nbPoint];
121
122         int k = 0;
123         for (int i = 0; i < coordinates.length; i += 2) {
124             xCoordinates[k] = coordinates[i];
125             yCoordinates[k] = coordinates[i + 1];
126             k++;
127         }
128
129         g2d.setColor(getColor(appearance.getLineColor()));
130         g2d.setStroke(G2DStroke.getStroke(appearance, 0));
131
132         g2d.drawPolyline(xCoordinates, yCoordinates, nbPoint);
133     }
134
135     @Override
136     public void fillPolygon(int[] coordinates, Appearance appearance) {
137         int nbPoint = coordinates.length / 2;
138
139         int[] xCoordinates = new int[nbPoint];
140         int[] yCoordinates = new int[nbPoint];
141
142         int k = 0;
143         for (int i = 0; i < coordinates.length; i += 2) {
144             xCoordinates[k] = coordinates[i];
145             yCoordinates[k] = coordinates[i + 1];
146             k++;
147         }
148
149         if (appearance.getFillColor().getAlphaAsFloat() != 0) {
150             g2d.setColor(getColor(appearance.getFillColor()));
151             g2d.fillPolygon(xCoordinates, yCoordinates, nbPoint);
152         }
153
154         if (!appearance.getLineColor().equals(appearance.getFillColor())) {
155             int usedLength = coordinates.length - (coordinates.length % 2);
156             int[] borderCoordinate = new int[usedLength + 2];
157             System.arraycopy(coordinates, 0, borderCoordinate, 0, usedLength);
158             borderCoordinate[usedLength]     = coordinates[0];
159             borderCoordinate[usedLength + 1] = coordinates[1];
160
161             drawPolyline(borderCoordinate, appearance);
162         }
163     }
164
165     @Override
166     public void drawCircle(int x, int y, int diameter, Appearance appearance) {
167         g2d.setColor(getColor(appearance.getLineColor()));
168         g2d.setStroke(G2DStroke.getStroke(appearance, 0));
169         double r = ((double) diameter) / 2;
170         g2d.draw(new Ellipse2D.Double(x - r, y - r, diameter, diameter));
171     }
172
173     @Override
174     public void fillDisc(int x, int y, int diameter, Color color) {
175         if (color.getAlphaAsFloat() != 0) {
176             g2d.setColor(getColor(color));
177             double r = ((double) diameter) / 2;
178             g2d.fill(new Ellipse2D.Double(x - r, y - r, diameter, diameter));
179         }
180     }
181
182     @Override
183     public void draw(TextEntity textEntity, int x, int y) {
184         if (textEntity != null && textEntity.isValid()) {
185             if (textEntity.isTextAntiAliased()) {
186                 g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
187             } else {
188                 g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);
189             }
190
191             if (textEntity.isTextUseFractionalMetrics()) {
192                 g2d.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
193             } else {
194                 g2d.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_OFF);
195             }
196             g2d.setColor(getColor(textEntity.getTextColor()));
197             TextLayout textLayout = new TextLayout(textEntity.getText(), textEntity.getFont(), g2d.getFontRenderContext());
198             Rectangle2D bounds = textLayout.getBounds();
199             g2d.setFont(textEntity.getFont());
200             g2d.drawString(textEntity.getText(), (float) (x - bounds.getX()), y + textLayout.getAscent());
201         }
202     }
203
204     @Override
205     public void draw(Icon icon, int x, int y) {
206         icon.paintIcon(new JLabel(), g2d, x, y);
207     }
208
209     @Override
210     public void clear(Color color) {
211         g2d.setColor(getColor(color));
212         g2d.fillRect(0, 0, width, height);
213     }
214
215     public java.awt.Color getColor(Color color) {
216         if (fillColor == null) {
217             return color;
218         } else {
219             if (color.equals(Color.WHITE)) {
220                 return fillColor;
221             } else {
222                 return color;
223             }
224         }
225     }
226 }