Skip to main content

How to vary the color along a stoke

3 replies [Last post]
Joined: 2006-05-05

Hi all,
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?

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Joined: 2003-07-02

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.



Joined: 2006-05-05

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.

import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics2D;
import java.awt.geom.Path2D;
import java.awt.geom.Point2D;
import java.awt.image.BufferedImage;
import java.util.Vector;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;

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();

//Add Points
points.add(new Point2D.Double(100,100));
points.add(new Point2D.Double(200,500));
points.add(new Point2D.Double(500,500));
points.add(new Point2D.Double(100,400));
points.add(new Point2D.Double(90,200));

//Set Colors

//Set Stroke
g.setStroke(new BasicStroke(10, BasicStroke.CAP_ROUND,

Path2D.Double path;
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);

path.moveTo(p1.x, p1.y);
path.lineTo(p2.x, p2.y);

g.setPaint(new GradientPaint(p1, c1, p2, c2));

ImageIcon imageHolder = new ImageIcon(img);
JLabel lab = new JLabel(imageHolder);
JFrame f = new JFrame("Testing");
f.getContentPane().setLayout(new BorderLayout());
f.getContentPane().add(lab, BorderLayout.CENTER);


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.

Joined: 2004-07-24

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.