GitHub Viewer
/* -*- mode: java; c-basic-offset: 2; indent-tabs-mode: nil -*- */
/*
Part of the Processing project - http://processing.org
Copyright (c) 2015 The Processing Foundation
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.
You should have received a copy of the GNU Lesser General
Public License along with this library; if not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330,
Boston, MA 02111-1307 USA
*/
package processing.awt;
import java.awt.Paint;
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;
import java.awt.image.WritableRaster;
import processing.core.PApplet;
import processing.core.PGraphics;
import processing.core.PShapeSVG;
import processing.data.*;
/**
* Implements features for PShape that are specific to AWT and Java2D.
* At the moment, this is gradients and java.awt.Paint handling.
*/
public class PShapeJava2D extends PShapeSVG {
Paint strokeGradientPaint;
Paint fillGradientPaint;
public PShapeJava2D(XML svg) {
super(svg);
}
public PShapeJava2D(PShapeSVG parent, XML properties, boolean parseKids) {
super(parent, properties, parseKids);
}
@Override
protected void setParent(PShapeSVG parent) {
super.setParent(parent);
if (parent instanceof PShapeJava2D) {
PShapeJava2D pj = (PShapeJava2D) parent;
fillGradientPaint = pj.fillGradientPaint;
strokeGradientPaint = pj.strokeGradientPaint;
} else { // parent is null or not Java2D
fillGradientPaint = null;
strokeGradientPaint = null;
}
}
/** Factory method for subclasses. */
@Override
protected PShapeSVG createShape(PShapeSVG parent, XML properties, boolean parseKids) {
return new PShapeJava2D(parent, properties, parseKids);
}
/*
@Override
public void setColor(String colorText, boolean isFill) {
super.setColor(colorText, isFill);
if (fillGradient != null) {
fillGradientPaint = calcGradientPaint(fillGradient);
}
if (strokeGradient != null) {
strokeGradientPaint = calcGradientPaint(strokeGradient);
}
}
*/
static class LinearGradientPaint implements Paint {
float x1, y1, x2, y2;
float[] offset;
int[] color;
int count;
float opacity;
public LinearGradientPaint(float x1, float y1, float x2, float y2,
float[] offset, int[] color, int count,
float opacity) {
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
this.offset = offset;
this.color = color;
this.count = count;
this.opacity = opacity;
}
public PaintContext createContext(ColorModel cm,
Rectangle deviceBounds, Rectangle2D userBounds,
AffineTransform xform, RenderingHints hints) {
Point2D t1 = xform.transform(new Point2D.Float(x1, y1), null);
Point2D t2 = xform.transform(new Point2D.Float(x2, y2), null);
return new LinearGradientContext((float) t1.getX(), (float) t1.getY(),
(float) t2.getX(), (float) t2.getY());
}
public int getTransparency() {
return TRANSLUCENT; // why not.. rather than checking each color
}
public class LinearGradientContext implements PaintContext {
int ACCURACY = 2;
float tx1, ty1, tx2, ty2;
public LinearGradientContext(float tx1, float ty1, float tx2, float ty2) {
this.tx1 = tx1;
this.ty1 = ty1;
this.tx2 = tx2;
this.ty2 = ty2;
}
public void dispose() { }
public ColorModel getColorModel() { return ColorModel.getRGBdefault(); }
public Raster getRaster(int x, int y, int w, int h) {
WritableRaster raster =
getColorModel().createCompatibleWritableRaster(w, h);
int[] data = new int[w * h * 4];
// make normalized version of base vector
float nx = tx2 - tx1;
float ny = ty2 - ty1;
float len = (float) Math.sqrt(nx*nx + ny*ny);
if (len != 0) {
nx /= len;
ny /= len;
}
int span = (int) PApplet.dist(tx1, ty1, tx2, ty2) * ACCURACY;
if (span