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.HashMap;
21 import java.util.Objects;
24 * Style for Stroke and Fill of GraphicsContext. Styles are created using
25 * GraphicsContexts factory methods. If the Implementation supports it, native
26 * Styles will be cached for performance reasons. This happens the first time
27 * the Style is actually used.
34 private int cacheHash;
39 void cache(Object toCache) {
40 cacheHash = hashCode();
41 this.cached = toCache;
44 private boolean isCached() {
45 return cacheHash == hashCode();
49 return isCached() ? cached : null;
52 public static final class Pattern extends Style {
54 private Image imageResource;
55 private String repeat;
57 public Pattern(Image imageResource, String repeat) {
58 this.imageResource = imageResource;
62 public Image getImageResource() {
66 public void setImageResource(Image imageResource) {
67 this.imageResource = imageResource;
70 public String getRepeat() {
74 public void setRepeat(String repeat) {
79 public static final class Color extends Style {
84 * Creates an RGB color specified with an HTML or CSS attribute string.
86 * @param webColor Colordefined as
88 public Color(String webColor) {
92 public String getAsString() {
97 public static class LinearGradient extends Style {
99 private HashMap<Double, String> stops;
100 private double x0, y0, x1, y1;
102 LinearGradient(double x0, double y0, double x1, double y1) {
109 void addColorStop(double position, String color) {
111 stops = new HashMap<>();
113 stops.put(position, color);
116 public HashMap<Double, String> getStops() {
120 public void setStops(HashMap<Double, String> stops) {
124 public double getX0() {
128 public void setX0(double x0) {
132 public double getY0() {
136 public void setY0(double y0) {
140 public double getX1() {
144 public void setX1(double x1) {
148 public double getY1() {
152 public void setY1(double y1) {
157 public int hashCode() {
159 hash = 29 * hash + Objects.hashCode(this.stops);
160 hash = 29 * hash + (int) (Double.doubleToLongBits(this.x0) ^ (Double.doubleToLongBits(this.x0) >>> 32));
161 hash = 29 * hash + (int) (Double.doubleToLongBits(this.y0) ^ (Double.doubleToLongBits(this.y0) >>> 32));
162 hash = 29 * hash + (int) (Double.doubleToLongBits(this.x1) ^ (Double.doubleToLongBits(this.x1) >>> 32));
163 hash = 29 * hash + (int) (Double.doubleToLongBits(this.y1) ^ (Double.doubleToLongBits(this.y1) >>> 32));
168 public boolean equals(Object obj) {
172 if (getClass() != obj.getClass()) {
175 final LinearGradient other = (LinearGradient) obj;
176 if (!Objects.equals(this.stops, other.stops)) {
179 if (Double.doubleToLongBits(this.x0) != Double.doubleToLongBits(other.x0)) {
182 if (Double.doubleToLongBits(this.y0) != Double.doubleToLongBits(other.y0)) {
185 if (Double.doubleToLongBits(this.x1) != Double.doubleToLongBits(other.x1)) {
188 if (Double.doubleToLongBits(this.y1) != Double.doubleToLongBits(other.y1)) {
195 public static final class RadialGradient extends LinearGradient {
197 private double r0, r1;
199 RadialGradient(double x0, double y0, double r0, double x1, double y1, double r1) {
200 super(x0, y0, x1, y1);
205 public double getR0() {
209 public void setR0(double r0) {
213 public double getR1() {
217 public void setR1(double r1) {
222 public int hashCode() {
223 int hash = super.hashCode();
224 hash = 17 * hash + (int) (Double.doubleToLongBits(this.r0) ^ (Double.doubleToLongBits(this.r0) >>> 32));
225 hash = 17 * hash + (int) (Double.doubleToLongBits(this.r1) ^ (Double.doubleToLongBits(this.r1) >>> 32));
231 public boolean equals(Object obj) {
235 if (getClass() != obj.getClass()) {
238 if (!super.equals(obj)) {
241 final RadialGradient other = (RadialGradient) obj;
242 if (Double.doubleToLongBits(this.r0) != Double.doubleToLongBits(other.r0)) {
245 if (Double.doubleToLongBits(this.r1) != Double.doubleToLongBits(other.r1)) {
248 if ((this.getCached() == null) != (other.getCached() == null)) {