2 * Back 2 Browser Bytecode Translator
3 * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, version 2 of the License.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. Look for COPYING file in the top folder.
16 * If not, see http://opensource.org/licenses/GPL-2.0.
18 package net.java.html.canvas;
20 import java.util.Collection;
21 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.
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, repeat-x, repeat-y, or no-repeat.
68 public Pattern(Image imageResource, String repeat) {
69 this.imageResource = imageResource;
74 * Get the base image of this pattern
75 * @return the base image of this pattern
77 public Image getImageResource() {
82 * Get the repeat style for this pattern
83 * @return return the repeat style
85 public String getRepeat() {
91 public static final class Color extends Style {
96 * Creates an RGB color specified with an HTML or CSS attribute string.
98 * @param webColor Color defined as web color (e.g. #ff0000)
100 public Color(String webColor) {
107 * @return the Color value as a Web Color (e.g. #ff0000)
109 public String getAsString() {
115 * A Linear Gradient. The GRadient has a direction defined by two coordinates
116 * and stops defining the Color at a specific position.
118 public static class LinearGradient extends Style {
120 private HashMap<Double, String> stops;
121 private double x0, y0, x1, y1;
125 * @param x0 the x coordinate of the start point for this gradient
126 * @param y0 the y coordinate of the start point for this gradient
127 * @param x1 the x coordinate of the end point for this gradient
128 * @param y1 the y coordinate of the end point for this gradient
130 LinearGradient(double x0, double y0, double x1, double y1) {
138 * Add a new Color stop. A color stop defines a fixed color at a position
139 * along the coordinates.
140 * @param position the position of this stop in percent [0.0-1.0]
141 * @param color A Color defined in web format (e.g. #ff0000)
143 void addColorStop(double position, String color) {
145 stops = new HashMap<>();
147 stops.put(position, color);
151 * Get the stops of this gradient.
152 * @return the stops of this gradient
154 public HashMap<Double, String> getStops(){
155 return new HashMap<>(stops);
159 * Set the stops as Position/Color pairs
160 * @param stops the stops for thsi Gradient
162 public void setStops(HashMap<Double, String> stops) {
163 this.stops = new HashMap<>(stops);
167 public double getX0() {
171 public void setX0(double x0) {
175 public double getY0() {
179 public void setY0(double y0) {
183 public double getX1() {
187 public void setX1(double x1) {
191 public double getY1() {
195 public void setY1(double y1) {
200 public int hashCode() {
202 hash = 29 * hash + Objects.hashCode(this.stops);
203 hash = 29 * hash + (int) (Double.doubleToLongBits(this.x0) ^ (Double.doubleToLongBits(this.x0) >>> 32));
204 hash = 29 * hash + (int) (Double.doubleToLongBits(this.y0) ^ (Double.doubleToLongBits(this.y0) >>> 32));
205 hash = 29 * hash + (int) (Double.doubleToLongBits(this.x1) ^ (Double.doubleToLongBits(this.x1) >>> 32));
206 hash = 29 * hash + (int) (Double.doubleToLongBits(this.y1) ^ (Double.doubleToLongBits(this.y1) >>> 32));
211 public boolean equals(Object obj) {
215 if (getClass() != obj.getClass()) {
218 final LinearGradient other = (LinearGradient) obj;
219 if (!Objects.equals(this.stops, other.stops)) {
222 if (Double.doubleToLongBits(this.x0) != Double.doubleToLongBits(other.x0)) {
225 if (Double.doubleToLongBits(this.y0) != Double.doubleToLongBits(other.y0)) {
228 if (Double.doubleToLongBits(this.x1) != Double.doubleToLongBits(other.x1)) {
231 if (Double.doubleToLongBits(this.y1) != Double.doubleToLongBits(other.y1)) {
238 public static final class RadialGradient extends LinearGradient {
240 private double r0, r1;
242 RadialGradient(double x0, double y0, double r0, double x1, double y1, double r1) {
243 super(x0, y0, x1, y1);
248 public double getR0() {
252 public void setR0(double r0) {
256 public double getR1() {
260 public void setR1(double r1) {
265 public int hashCode() {
266 int hash = super.hashCode();
267 hash = 17 * hash + (int) (Double.doubleToLongBits(this.r0) ^ (Double.doubleToLongBits(this.r0) >>> 32));
268 hash = 17 * hash + (int) (Double.doubleToLongBits(this.r1) ^ (Double.doubleToLongBits(this.r1) >>> 32));
274 public boolean equals(Object obj) {
278 if (getClass() != obj.getClass()) {
281 if (!super.equals(obj)) {
284 final RadialGradient other = (RadialGradient) obj;
285 if (Double.doubleToLongBits(this.r0) != Double.doubleToLongBits(other.r0)) {
288 if (Double.doubleToLongBits(this.r1) != Double.doubleToLongBits(other.r1)) {
291 if ((this.getCached() == null) != (other.getCached() == null)) {