I am trying to vary the color along a path2D from a lookup table. Has anyone else tried this or can give me some advice on this?
The path2d is basically a vector. You may want to paint it with some color.
Painting basically takes place when you draw() or fill() the path.
Take a look at the java.awt.Paint interface.
Hey, I've been looking into this using the Paint interface, however I have found that It is not suitable to solve my problem and its not realy working. The first attempt that I have tried at this has been to set an array of points, each with a color, I then use a gradientPaint to interpolate the color between the two points, the code for this is below.
public class StrokeTesting
public static void main(String input)
//Create an image and get Graphics2D
BufferedImage img = new BufferedImage(600, 600,
Graphics2D g = img.createGraphics();
//Vectors to hold points+colors
Vector points = new Vector();
Vector colors = new Vector();
g.setStroke(new BasicStroke(10, BasicStroke.CAP_ROUND,
for (int i = 0; i < points.size() - 1; i++)
path = new Path2D.Double();
Point2D.Double p1 = points.get(i);
Point2D.Double p2 = points.get(i + 1);
Color c1 = colors.get(i);
Color c2 = colors.get(i + 1);
g.setPaint(new GradientPaint(p1, c1, p2, c2));
ImageIcon imageHolder = new ImageIcon(img);
JLabel lab = new JLabel(imageHolder);
JFrame f = new JFrame("Testing");
This is not a perfect solution as the points can overllap and look kinda funny.Also it does not work that well if you want to vary the color between two points. What I would like to do would be to pass a function an array of colors and an array of points. The program would then draw this and vary the color along the path baised on the colors.
I'm basicly trying to draw a rainbow colored path.
That's a good attempt drawing smaller line segments with a GradientPaint. If this is your attempt at using the Paint interface, then I think you missed the mark.
It sounds like you'll have to create classes that implement the Paint and PaintContext interfaces.
Given my understanding of how Java2D's gradients require user-space shapes to define where on the screen the gradient starts and stops, your Paint implementation will likely require a copy of the path you're trying to draw so that it can fill a Raster object with colors that follow your path.
The downside of Java's Paint interface is that you'd have to create new instances for different paths, and performance will suffer quickly as you add more geometry.
The goal of your PaintContext class would be to receive a rectangle defining a grid of pixels, create a Raster object to hold the pixels, then map that copy of your geometry over the pixel grid, and color the pixels as desired.
It seems that Java just creates boundaries and fills them with your pixels, so handling lines thicker than 1 pixel would be complicated.
Here are some links I came across, they're difficult to find, and I hope they help.
Your use of this web site or any of its content or software indicates your agreement to be bound by these Terms of Participation.
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their respective owners.