Bug 371 : gradients / lines that change stroke color not supported in the JAVA2D renderer
Last modified: 2009-03-12 08:25




Status:
RESOLVED
Resolution:
LATER -
Priority:
P4
Severity:
enhancement

 

Reporter:
liminal
Assigned To:
fry

Attachment Type Created Size Actions

Description:   Opened: 2006-07-12 15:42
using p3d, if i create a line with vertex colours, the expected gradient
isn't exported to pdf.

e.g.:

PGraphics3 g;
g.beginShape(PConstants.LINES);
g.vertex(0, 0, 0);
g.stroke(someOtherColor);
g.vertex(1, 0, 0);
g.endShape();
Additional Comment #1 From fry 2006-07-15 15:32
this is a general issue for all renderers that are part of the Java2D
rendering system, which includes PDF. we've opted not to include gradients
for 1.0, but it's a feature that may be added later.
Additional Comment #2 From winne 2009-03-12 08:15
Plotting gradient lines with Java2D is so easy, that it should be
implemented in both Java2D as well as PDF renderer.

Code example:

import java.awt.geom.*;

size(200, 200);

strokeWeight(5);

Graphics2D g2 = ((PGraphicsJava2D)g).g2;
GradientPaint gp = new GradientPaint(10, 10, Color.red, 180, 140,
Color.yellow, false);
g2.setPaint(gp);
g2.draw(new Line2D.Float(10, 10, 180, 140));

One would also expect gradients in Java2D when using code like this:

beginShape(LINES);
stroke(0,255,0,128);
strokeWeight(10);
vertex(fromX, fromY);
stroke(255,0,0,128);
vertex(toX, toY);
endShape();

(works in OpenGL mode)

GradientPaint has been around since Java 1.3.1 or earlier so this should
not result in compatibility problems. Exporting gradient lines to PDF is no
problem either. Simply change code above to:

Graphics2D g2;
if (g.recorder == null) g2 = ((PGraphicsJava2D)g.g).g2;
else g2 = ((PGraphicsPDF)g.recorder).g2;

Another fancy thing are radial gradients:

import java.awt.geom.*;

size(200, 200);

Graphics2D g2 = ((PGraphicsJava2D)g).g2;
RadialGradientPaint rgp = new RadialGradientPaint(
new Point2D.Float(50, 150),
40,
new float[]{0.0f, 1.0f},
new Color[]{Color.blue, Color.green});
g2.setPaint(rgp);
g2.fill(new Ellipse2D.Float(10, 110, 80, 80));

Unfortunately, for RadialGradientPaint Java 6 is needed.
Additional Comment #3 From fry 2009-03-12 08:25
Yes, I'm aware of how they work--they're actually implemented with SVG when
using the default (Java2D-based) renderer.

The issue is more complicated, because in Java2D/PDF/Postscript a gradient
is per-shape, not per-vertex, which contradicts how they're implemented in
the P2D, P3D, and OPENGL renderer settings. For lines, we could do it,
though it would require creating many gradient paint objects (and we'd lose
caps/joins, because the gradient would change along the line). But gradient
lines are of limited utility versus shapes that support gradients.

The bottom line is that making it work consistently with other renderers is
fairly tricky (we'd need to break up shapes so that the gradient can be
implemented like the other renderers) which means time-consuming to
implement, but also that the result would likely to have poor performance
because we'd be mixing two rendering models. As such the tradeoffs don't
make it a high priority.