From 1b0ab430b9296abeaa16d301b7acda9380a0d16e Mon Sep 17 00:00:00 2001 From: benfry Date: Wed, 13 Dec 2006 21:36:18 +0000 Subject: [PATCH] fixes for gradients --- .../processing/candy/LinearGradientPaint.java | 17 +++++++++- .../processing/candy/RadialGradientPaint.java | 17 +++++++++- candy/src/processing/candy/SVG.java | 33 +++++++++++++++---- 3 files changed, 58 insertions(+), 9 deletions(-) diff --git a/candy/src/processing/candy/LinearGradientPaint.java b/candy/src/processing/candy/LinearGradientPaint.java index f4ad98407..e3f0081fd 100644 --- a/candy/src/processing/candy/LinearGradientPaint.java +++ b/candy/src/processing/candy/LinearGradientPaint.java @@ -1,3 +1,19 @@ +/* + Candy 2 - SVG Importer for Processing - http://processing.org + + Code to handle linear and radial gradients + Copyright (c) 2006- Ben Fry and Casey Reas + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License version 2.1 as published by the Free Software Foundation. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. +*/ + package processing.candy; import java.awt.Paint; @@ -6,7 +22,6 @@ import java.awt.Rectangle; import java.awt.RenderingHints; import java.awt.geom.AffineTransform; import java.awt.geom.Point2D; -//import java.awt.geom.Point2D; import java.awt.geom.Rectangle2D; import java.awt.image.ColorModel; import java.awt.image.Raster; diff --git a/candy/src/processing/candy/RadialGradientPaint.java b/candy/src/processing/candy/RadialGradientPaint.java index 8474952bb..a38371262 100644 --- a/candy/src/processing/candy/RadialGradientPaint.java +++ b/candy/src/processing/candy/RadialGradientPaint.java @@ -1,3 +1,19 @@ +/* + Candy 2 - SVG Importer for Processing - http://processing.org + + Code to handle linear and radial gradients + Copyright (c) 2006- Ben Fry and Casey Reas + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License version 2.1 as published by the Free Software Foundation. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. +*/ + package processing.candy; import java.awt.Paint; @@ -5,7 +21,6 @@ import java.awt.PaintContext; import java.awt.Rectangle; import java.awt.RenderingHints; import java.awt.geom.AffineTransform; -//import java.awt.geom.Point2D; import java.awt.geom.Rectangle2D; import java.awt.image.ColorModel; import java.awt.image.Raster; diff --git a/candy/src/processing/candy/SVG.java b/candy/src/processing/candy/SVG.java index 902d034f2..b2f08b4f3 100755 --- a/candy/src/processing/candy/SVG.java +++ b/candy/src/processing/candy/SVG.java @@ -21,8 +21,7 @@ package processing.candy; import java.awt.*; import java.awt.geom.AffineTransform; -//import java.awt.geom.*; -//import java.awt.image.*; +import java.awt.geom.Point2D; import java.util.Hashtable; import processing.core.*; @@ -860,6 +859,8 @@ public class SVG { abstract private class Gradient extends BaseObject { + AffineTransform transform; + float[] offset; int[] color; int count; @@ -905,6 +906,7 @@ public class SVG { abstract protected void drawShape(); } + static protected Hashtable parseStyleAttributes(String style) { Hashtable table = new Hashtable(); String[] pieces = style.split(";"); @@ -918,7 +920,6 @@ public class SVG { private class LinearGradient extends Gradient { float x1, y1, x2, y2; - AffineTransform transform; public LinearGradient(XMLElement properties) { super(properties); @@ -932,7 +933,14 @@ public class SVG { properties.getStringAttribute("gradientTransform"); if (transformStr != null) { this.transform = parseTransform(transformStr); - System.out.println(transform); + + Point2D t1 = transform.transform(new Point2D.Float(x1, y1), null); + Point2D t2 = transform.transform(new Point2D.Float(x2, y2), null); + this.x1 = (float) t1.getX(); + this.y1 = (float) t1.getY(); + this.x2 = (float) t2.getX(); + this.y2 = (float) t2.getY(); + } } @@ -958,13 +966,24 @@ public class SVG { private class RadialGradient extends Gradient { float cx, cy, r; - public RadialGradient(XMLElement properties) { super(properties); this.cx = properties.getFloatAttribute("cx"); this.cy = properties.getFloatAttribute("cy"); this.r = properties.getFloatAttribute("r"); + + String transformStr = + properties.getStringAttribute("gradientTransform"); + if (transformStr != null) { + this.transform = parseTransform(transformStr); + + Point2D t1 = transform.transform(new Point2D.Float(cx, cy), null); + Point2D t2 = transform.transform(new Point2D.Float(cx + r, cy), null); + this.cx = (float) t1.getX(); + this.cy = (float) t1.getY(); + this.r = (float) (t2.getX() - t1.getX()); + } } protected void drawShape() { @@ -975,7 +994,7 @@ public class SVG { // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . - private class Line extends VectorObject{ + private class Line extends VectorObject { float x1, y1, x2, y2; @@ -996,7 +1015,7 @@ public class SVG { // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . - private class Circle extends VectorObject{ + private class Circle extends VectorObject { float x, y, radius;