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() {
121 * A Linear Gradient. The Gradient has a direction defined by two
122 * coordinates and stops defining the Color at a specific position.
124 public static class LinearGradient extends Style {
126 private Map<Double, String> stops;
127 private double x0, y0, x1, y1;
131 * @param x0 the x coordinate of the start point for this gradient
132 * @param y0 the y coordinate of the start point for this gradient
133 * @param x1 the x coordinate of the end point for this gradient
134 * @param y1 the y coordinate of the end point for this gradient
136 LinearGradient(double x0, double y0, double x1, double y1) {
144 * Add a new Color stop. A color stop defines a fixed color at a
145 * position along the coordinates.
147 * @param position the position of this stop in percent [0.0-1.0]
148 * @param color A Color defined in web format (e.g. #ff0000)
150 void addColorStop(double position, String color) {
152 stops = new HashMap<>();
154 stops.put(position, color);
158 * Get the stops of this gradient.
160 * @return the stops of this gradient
162 public Map<Double, String> getStops() {
163 return new HashMap<>(stops);
167 * Set the stops as Position/Color pairs
169 * @param stops the stops for thsi Gradient
171 public void setStops(Map<Double, String> stops) {
172 this.stops = new HashMap<>(stops);
176 * Get the X coordinate of the Gradients start point
178 * @return x coordinate
180 public double getX0() {
185 * Set the X coordinate of the Gradients start point
187 * @param x0 x coordinate
189 public void setX0(double x0) {
194 * Get the Y coordinate of the Gradients start point
196 * @return y coordinate
198 public double getY0() {
203 * Set the Y coordinate of the Gradients start point
205 * @param y0 y coordinate
207 public void setY0(double y0) {
212 * Set the X coordinate of the Gradients end point
214 * @return x coordinate
216 public double getX1() {
221 * Set the X coordinate of the Gradients end point
223 * @param X coordinate
225 public void setX1(double x1) {
230 * Get the Y coordinate of the Gradients end point
232 * @return y coordinate
234 public double getY1() {
239 * Set the Y coordinate of the Gradients end point
241 * @param y1 coordinate
243 public void setY1(double y1) {
248 public int hashCode() {
250 hash = 29 * hash + Objects.hashCode(this.stops);
251 hash = 29 * hash + (int) (Double.doubleToLongBits(this.x0) ^ (Double.doubleToLongBits(this.x0) >>> 32));
252 hash = 29 * hash + (int) (Double.doubleToLongBits(this.y0) ^ (Double.doubleToLongBits(this.y0) >>> 32));
253 hash = 29 * hash + (int) (Double.doubleToLongBits(this.x1) ^ (Double.doubleToLongBits(this.x1) >>> 32));
254 hash = 29 * hash + (int) (Double.doubleToLongBits(this.y1) ^ (Double.doubleToLongBits(this.y1) >>> 32));
259 public boolean equals(Object obj) {
263 if (getClass() != obj.getClass()) {
266 final LinearGradient other = (LinearGradient) obj;
267 if (!Objects.equals(this.stops, other.stops)) {
270 if (Double.doubleToLongBits(this.x0) != Double.doubleToLongBits(other.x0)) {
273 if (Double.doubleToLongBits(this.y0) != Double.doubleToLongBits(other.y0)) {
276 if (Double.doubleToLongBits(this.x1) != Double.doubleToLongBits(other.x1)) {
279 if (Double.doubleToLongBits(this.y1) != Double.doubleToLongBits(other.y1)) {
287 * A Radial Gradient. Radial gradients are defined with two imaginary
288 * circles, a starting circle and an ending circle. The gradient starts with
289 * the start circle and moves towards the end circle.
291 public static final class RadialGradient extends LinearGradient {
293 private double r0, r1;
296 * Create a new RadialGradient
298 * @param x0 x Coordinate of starting circle
299 * @param y0 y Coordinate of starting circle
300 * @param r0 radius of starting circle
301 * @param x1 x coordinate of ending circle
302 * @param y1 y coordinate of ending circle
303 * @param r1 radius of ending circle
305 RadialGradient(double x0, double y0, double r0, double x1, double y1, double r1) {
306 super(x0, y0, x1, y1);
312 * get the radius of the start circle.
315 public double getR0() {
320 * set the radius of the start circle.
321 * @param r0 the radius
323 public void setR0(double r0) {
328 * get the radius of the end circle
331 public double getR1() {
336 * set the radius of the end circle.
337 * @param r1 the radius.
339 public void setR1(double r1) {
344 public int hashCode() {
345 int hash = super.hashCode();
346 hash = 17 * hash + (int) (Double.doubleToLongBits(this.r0) ^ (Double.doubleToLongBits(this.r0) >>> 32));
347 hash = 17 * hash + (int) (Double.doubleToLongBits(this.r1) ^ (Double.doubleToLongBits(this.r1) >>> 32));
353 public boolean equals(Object obj) {
357 if (getClass() != obj.getClass()) {
360 if (!super.equals(obj)) {
363 final RadialGradient other = (RadialGradient) obj;
364 if (Double.doubleToLongBits(this.r0) != Double.doubleToLongBits(other.r0)) {
367 if (Double.doubleToLongBits(this.r1) != Double.doubleToLongBits(other.r1)) {
370 if ((this.getCached() == null) != (other.getCached() == null)) {