Vapor Trails in Graphics2D

Is it possible to apply a gradiant to a line or poligon’s Stroke?

Basicaly, I have a chain of lines ( myGraphics.DrawLine( … ) ) that points out of the back of my projectile, and I want to gradient it between Color X and transparrent.

I dont want to use particals for this (in most instances) right now, as it just has to be very simple, and I have not written a partical engine yet :slight_smile:

For screenshots of what I have at the moment, Please see my Journal for this project.

[quote]Is it possible to apply a gradiant to a line or poligon’s Stroke?
[/quote]
yes, try the GradientPaint class. Here is a simple demo.

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Line2D;

import javax.swing.JFrame;


public class GradientTest extends JFrame {
	static final Color TRANSPARENT = new Color(0, 0, 0, 0);
	Line2D line = new Line2D.Float(50, 30, 200, 300);
	Color colorX = new Color(255, 100, 100);
	
	public GradientTest() {
		setPreferredSize(new Dimension(400, 300));
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		pack();
		setVisible(true);
	}
	
	public void paint(Graphics g) {
		Graphics2D g2 = (Graphics2D)g;
		GradientPaint gradient = new GradientPaint(line.getP1(), colorX, line.getP2(), TRANSPARENT, false);
		g2.setPaint(gradient);
		g2.draw(line);
		g2.dispose();
	}
	
	public static void main(String[] args) {
		new GradientTest();
	}

}

cool, thanks, I’ll try that.

I’ve been having a little troubble understanding how all the abstract classes in the Graphics2D are 'spozed to work together. I guess I’ll figure it out in time.

Hi, just to update, I got it working fine now.

It was a terrible mistake - I’d got a stuff.getX() insted of a stuff.getY() hidden away in my code. once i found that, the gradients draw perfectly!

Thanks :slight_smile: