changes to canvas and graphicsenv to support additional methods fillPolygon and mergeImages
1.1 --- a/javaquery/canvas/src/main/java/net/java/html/canvas/GraphicsContext.java Sat Sep 07 18:10:35 2013 +0200
1.2 +++ b/javaquery/canvas/src/main/java/net/java/html/canvas/GraphicsContext.java Sat Sep 07 18:25:09 2013 +0200
1.3 @@ -151,6 +151,19 @@
1.4 Object nativeImage = graphicsEnvironmentImpl.drawImage(image, sx, sy, sWidth, sHeight, x, y, width, height, image.getCached());
1.5 image.cache(nativeImage);
1.6 }
1.7 +
1.8 + public Image merge(Image a, Image b){
1.9 + if(a.getCached()==null){
1.10 + drawImage(a, 0, 0);
1.11 + }
1.12 + if(b.getCached()==null){
1.13 + drawImage(b, 0, 0);
1.14 + }
1.15 + Object nativeImage = graphicsEnvironmentImpl.mergeImages(a,b,a.getCached(),b.getCached());
1.16 + Image merged = Image.create("should add real path here");
1.17 + merged.cache(nativeImage);
1.18 + return merged;
1.19 + }
1.20
1.21 public void setShadowColor(String color){
1.22 graphicsEnvironmentImpl.setShadowColor(color);
1.23 @@ -322,10 +335,6 @@
1.24 return new Style.Color(webColor);
1.25 }
1.26
1.27 - public Image getImageForPath(String path){
1.28 - return new net.java.html.canvas.Image(path);
1.29 - }
1.30 -
1.31 public int getHeight(){
1.32 return graphicsEnvironmentImpl.getHeight();
1.33 }
1.34 @@ -341,4 +350,23 @@
1.35 public void setWidth(int width){
1.36 graphicsEnvironmentImpl.setWidth(width);
1.37 }
1.38 +
1.39 + public void fillCircle(float centerX, float centerY, float radius) {
1.40 + graphicsEnvironmentImpl.arc(centerX, centerY, radius, 0, Math.PI*2, false);
1.41 + }
1.42 +
1.43 + public void fillPolygon(double[] x_coord, double[] y_coord, int vertexCount) {
1.44 + if (vertexCount >=1&&x_coord!=null && x_coord.length>=vertexCount && y_coord!=null && y_coord.length>=vertexCount)
1.45 + graphicsEnvironmentImpl.beginPath();
1.46 + graphicsEnvironmentImpl.moveTo(x_coord[0], y_coord[0]);
1.47 + for (int i = 1; i < vertexCount; i++) {
1.48 + graphicsEnvironmentImpl.lineTo(x_coord[i], y_coord[i]);
1.49 +
1.50 + }
1.51 + graphicsEnvironmentImpl.closePath();
1.52 + graphicsEnvironmentImpl.fill();
1.53 + graphicsEnvironmentImpl.stroke();
1.54 +
1.55 +
1.56 + }
1.57 }
2.1 --- a/javaquery/canvas/src/main/java/net/java/html/canvas/Image.java Sat Sep 07 18:10:35 2013 +0200
2.2 +++ b/javaquery/canvas/src/main/java/net/java/html/canvas/Image.java Sat Sep 07 18:25:09 2013 +0200
2.3 @@ -18,6 +18,8 @@
2.4 package net.java.html.canvas;
2.5
2.6 import java.util.Objects;
2.7 +import java.util.ServiceLoader;
2.8 +import net.java.html.canvas.spi.GraphicsEnvironment;
2.9
2.10 /**
2.11 * Image represents an Image Resource defined by a path.
2.12 @@ -30,13 +32,17 @@
2.13 private Object cached;
2.14 private int cacheHash;
2.15
2.16 - Image(String src) {
2.17 + public static Image create(String src) {
2.18 + return new Image(src);
2.19 + }
2.20 +
2.21 + private Image(String src) {
2.22 this.src = src;
2.23 }
2.24
2.25 void cache(Object toCache) {
2.26 + this.cached = toCache;
2.27 cacheHash = hashCode();
2.28 - this.cached = toCache;
2.29 }
2.30
2.31 private boolean isCached() {
2.32 @@ -51,10 +57,30 @@
2.33 return src;
2.34 }
2.35
2.36 + public int getWidth() {
2.37 + ServiceLoader<GraphicsEnvironment> loader = ServiceLoader.load(GraphicsEnvironment.class);
2.38 + GraphicsEnvironment ge = null;
2.39 + for (GraphicsEnvironment graphicsEnvironment : loader) {
2.40 + ge = graphicsEnvironment;
2.41 + break;
2.42 + }
2.43 + return ge.getWidth(this, cached);
2.44 + }
2.45 +
2.46 + public int getHeight() {
2.47 + ServiceLoader<GraphicsEnvironment> loader = ServiceLoader.load(GraphicsEnvironment.class);
2.48 + GraphicsEnvironment ge = null;
2.49 + for (GraphicsEnvironment graphicsEnvironment : loader) {
2.50 + ge = graphicsEnvironment;
2.51 + break;
2.52 + }
2.53 + return ge.getHeight(this, cached);
2.54 + }
2.55 +
2.56 @Override
2.57 public int hashCode() {
2.58 int hash = 7;
2.59 - hash = 59 * hash + Objects.hashCode(this.src) ^ (cached==null? 1231 : 1237);
2.60 + hash = 59 * hash + Objects.hashCode(this.src) ^ (cached == null ? 1231 : 1237);
2.61 return hash;
2.62 }
2.63
2.64 @@ -70,10 +96,9 @@
2.65 if (!Objects.equals(this.src, other.src)) {
2.66 return false;
2.67 }
2.68 - if ((cached==null) != (other.getCached()==null)){
2.69 + if ((cached == null) != (other.getCached() == null)) {
2.70 return false;
2.71 }
2.72 return true;
2.73 }
2.74 -
2.75 }
3.1 --- a/javaquery/canvas/src/main/java/net/java/html/canvas/spi/GraphicsEnvironment.java Sat Sep 07 18:10:35 2013 +0200
3.2 +++ b/javaquery/canvas/src/main/java/net/java/html/canvas/spi/GraphicsEnvironment.java Sat Sep 07 18:25:09 2013 +0200
3.3 @@ -90,7 +90,13 @@
3.4
3.5 public Object drawImage(Image image, double sx, double sy, double sWidth, double sHeight, double x, double y, double width, double height, Object nativeImage);
3.6
3.7 -
3.8 +
3.9 + public int getWidth(Image image, Object nativeImage);
3.10 +
3.11 + public int getHeight(Image image, Object nativeImage);
3.12 +
3.13 +
3.14 +
3.15 /**
3.16 * When implementing you can return an Object of your choice to enable
3.17 * caching. Returning null means no caching. When caching is enabled, and
3.18 @@ -196,4 +202,6 @@
3.19 public void setHeight(int height);
3.20
3.21 public void setWidth(int width);
3.22 +
3.23 + public Object mergeImages(Image a, Image b, Object cachedA, Object cachedB);
3.24 }