2 * Back 2 Browser Bytecode Translator Copyright (C) 2012 Jaroslav Tulach
3 * <jaroslav.tulach@apidesign.org>
5 * This program is free software: you can redistribute it and/or modify it under
6 * the terms of the GNU General Public License as published by the Free Software
7 * Foundation, version 2 of the License.
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14 * You should have received a copy of the GNU General Public License along with
15 * this program. Look for COPYING file in the top folder. If not, see
16 * http://opensource.org/licenses/GPL-2.0.
18 package net.java.html.canvas;
20 import java.util.HashMap;
22 import java.util.Objects;
25 * Style for Stroke and Fill of GraphicsContext. Styles are created using
26 * GraphicsContexts factory methods. If the Implementation supports it, native
27 * Styles will be cached for performance reasons. This happens the first time
28 * the Style is actually used.
35 private int cacheHash;
40 void cache(Object toCache) {
41 cacheHash = hashCode();
42 this.cached = toCache;
45 private boolean isCached() {
46 return cacheHash == hashCode();
50 return isCached() ? cached : null;
54 * A Fill Pattern using an Image Resource to create a fill style supporting
55 * different repeat styles repeat, repeat-x, repeat-y, or no-repeat. Default
58 public static final class Pattern extends Style {
60 private final Image imageResource;
61 private final String repeat;
65 * @param imageResource the base image of thsi pattern
66 * @param repeat the repeat pattern, possible values are repeat,
67 * repeat-x, repeat-y, or no-repeat.
69 public Pattern(Image imageResource, String repeat) {
70 this.imageResource = imageResource;
75 * Get the base image of this pattern
77 * @return the base image of this pattern
79 public Image getImageResource() {
84 * Get the repeat style for this pattern
86 * @return return the repeat style
88 public String getRepeat() {
97 public static final class Color extends Style {
102 * Creates an RGB color specified with an HTML or CSS attribute string.
104 * @param webColor Color defined as web color (e.g. #ff0000)
106 public Color(String webColor) {
113 * @return the Color value as a Web Color (e.g. #ff0000)
115 public String getAsString() {
120 private static class Gradient extends Style {
122 protected final Map<Double, String> stops;
123 protected final double x0, y0, x1, y1;
127 * @param x0 the x coordinate of the start point for this gradient
128 * @param y0 the y coordinate of the start point for this gradient
129 * @param x1 the x coordinate of the end point for this gradient
130 * @param y1 the y coordinate of the end point for this gradient
131 * @param stops the stops of this gradient
133 private Gradient(double x0, double y0, double x1, double y1,Map<Double, String> stops) {
138 this.stops = new HashMap<>(stops);
142 * Get the X coordinate of the Gradients start point
144 * @return x coordinate
146 public double getX0() {
151 * Set the X coordinate of the Gradients start point
153 * @param x0 x coordinate public void setX0(double x0) { this.x0 = x0; }
156 * Get the Y coordinate of the Gradients start point
158 * @return y coordinate
160 public double getY0() {
165 * Set the Y coordinate of the Gradients start point
167 * @param y0 y coordinate public void setY0(double y0) { this.y0 = y0; }
170 * Set the X coordinate of the Gradients end point
172 * @return x coordinate
174 public double getX1() {
179 * Set the X coordinate of the Gradients end point
181 * @param X coordinate public void setX1(double x1) { this.x1 = x1; }
184 * Get the Y coordinate of the Gradients end point
186 * @return y coordinate
188 public double getY1() {
194 * Get the stops of this gradient.
196 * @return the stops of this gradient
198 public Map<Double, String> getStops() {
199 return new HashMap<>(stops);
203 public int hashCode() {
205 hash = 29 * hash + Objects.hashCode(this.stops);
206 hash = 29 * hash + (int) (Double.doubleToLongBits(this.x0) ^ (Double.doubleToLongBits(this.x0) >>> 32));
207 hash = 29 * hash + (int) (Double.doubleToLongBits(this.y0) ^ (Double.doubleToLongBits(this.y0) >>> 32));
208 hash = 29 * hash + (int) (Double.doubleToLongBits(this.x1) ^ (Double.doubleToLongBits(this.x1) >>> 32));
209 hash = 29 * hash + (int) (Double.doubleToLongBits(this.y1) ^ (Double.doubleToLongBits(this.y1) >>> 32));
214 public boolean equals(Object obj) {
218 if (getClass() != obj.getClass()) {
221 final LinearGradient other = (LinearGradient) obj;
222 if (!Objects.equals(this.stops, other.stops)) {
225 if (Double.doubleToLongBits(this.x0) != Double.doubleToLongBits(other.x0)) {
228 if (Double.doubleToLongBits(this.y0) != Double.doubleToLongBits(other.y0)) {
231 if (Double.doubleToLongBits(this.x1) != Double.doubleToLongBits(other.x1)) {
234 if (Double.doubleToLongBits(this.y1) != Double.doubleToLongBits(other.y1)) {
242 * A Linear Gradient. The Gradient has a direction defined by two
243 * coordinates and stops defining the Color at a specific position.
245 public static class LinearGradient extends Gradient {
247 private LinearGradient(double x0, double y0, double x1, double y1,Map<Double, String> stops) {
248 super(x0, y0, x1, y1, stops);
253 * @param x0 the x coordinate of the start point for this gradient
254 * @param y0 the y coordinate of the start point for this gradient
255 * @param x1 the x coordinate of the end point for this gradient
256 * @param y1 the y coordinate of the end point for this gradient
257 * @param stops the stops of this gradient
258 * @return linearGradient the gradient
260 public static LinearGradient create(double x0, double y0, double x1, double y1,Map<Double, String> stops) {
261 return new LinearGradient(x0, y0, x1, y1, stops);
267 * A Radial Gradient. Radial gradients are defined with two imaginary
268 * circles, a starting circle and an ending circle. The gradient starts with
269 * the start circle and moves towards the end circle.
271 public static final class RadialGradient extends Gradient {
273 final private double r0, r1;
276 private RadialGradient(double x0, double y0, double r0, double x1, double y1, double r1, Map<Double, String> stops) {
277 super(x0, y0, x1, y1,stops);
283 * Create a new RadialGradient
285 * @param x0 x Coordinate of starting circle
286 * @param y0 y Coordinate of starting circle
287 * @param r0 radius of starting circle
288 * @param x1 x coordinate of ending circle
289 * @param y1 y coordinate of ending circle
290 * @param r1 radius of ending circle
291 * @param stops the stops of this gradient
292 * @return radialGradient the gradient
294 public static RadialGradient create(double x0, double y0, double r0, double x1, double y1, double r1, Map<Double, String> stops){
295 return new RadialGradient(x0, y0, r0, x1, y1, r1, stops);
299 * get the radius of the start circle.
303 public double getR0() {
308 * set the radius of the start circle.
310 * @param r0 the radius
312 * public void setR0(double r0) { this.r0 = r0; }
315 * get the radius of the end circle
319 public double getR1() {
324 * set the radius of the end circle.
326 * @param r1 the radius.
328 * public void setR1(double r1) { this.r1 = r1; }
331 public int hashCode() {
332 int hash = super.hashCode();
333 hash = 17 * hash + (int) (Double.doubleToLongBits(this.r0) ^ (Double.doubleToLongBits(this.r0) >>> 32));
334 hash = 17 * hash + (int) (Double.doubleToLongBits(this.r1) ^ (Double.doubleToLongBits(this.r1) >>> 32));
340 public boolean equals(Object obj) {
344 if (getClass() != obj.getClass()) {
347 if (!super.equals(obj)) {
350 final RadialGradient other = (RadialGradient) obj;
351 if (Double.doubleToLongBits(this.r0) != Double.doubleToLongBits(other.r0)) {
354 if (Double.doubleToLongBits(this.r1) != Double.doubleToLongBits(other.r1)) {
357 if ((this.getCached() == null) != (other.getCached() == null)) {