Draw images (arrows) along a path
I'm trying to draw a set arrows along a path like on a map, I have the begin and ending coordinates.
I tried to draw a stroke with rectanglur shape, then fill in the shape, but I couldn't get it work.
The idea was inspired from www.jhlabs.com/java/java2d/strokes/
The arrows need to evenly spaced and pointing the rigth direction. I can draw one arrow in the direction, but I don't know I do about drawing them along a line. The arrow is an image.
I appreciate any suggestions.
Message was edited by:
williw
[554 byte] By [
williwa] at [2007-11-27 4:07:54]

# 1
import java.awt.*;
import java.awt.geom.*;
import java.awt.image.BufferedImage;
import javax.swing.*;
public class ArrowLines extends JPanel {
BufferedImage image;
double[] coords = {
30.0, 125.0, 99.0, 300.0,
75.0, 50.0, 325.0, 275.0,
25.0, 325.0, 300.0, 315.0
};
public ArrowLines() {
GeneralPath arrow = getArrow();
createImage(arrow);
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BICUBIC);
int iw = image.getWidth();
int ih = image.getHeight();
for(int j = 0; j < coords.length; j+=4) {
double dy = coords[j+3] - coords[j+1];
double dx = coords[j+2] - coords[j];
double theta = Math.atan2(dy, dx);
double length = Point2D.distance(coords[j],coords[j+1],
coords[j+2], coords[j+3]);
int n = (int)(length/iw);
double delta = (length - n*iw)/(n+1);
g2.setPaint(Color.lightGray);
g2.draw(new Line2D.Double(coords[j],coords[j+1],
coords[j+2], coords[j+3]));
double pos = delta;
for(int k = 0; k < n; k++) {
double x = coords[j]+ pos*Math.cos(theta);
double y = coords[j+1] + pos*Math.sin(theta) - ih/2.0;
AffineTransform at = AffineTransform.getTranslateInstance(x,y);
at.rotate(theta, 0, ih/2.0);
g2.drawRenderedImage(image, at);
pos += iw + delta;
}
}
}
private GeneralPath getArrow() {
GeneralPath path = new GeneralPath();
path.moveTo(0,0);
path.lineTo(75,0);
double theta = Math.toRadians(15);
int BARB = 20;
float x = (float)(75 - BARB*Math.cos(theta));
float y = (float)(0 - BARB*Math.sin(theta));
path.moveTo(x, y);
path.lineTo(75,0);
x = (float)(75 - BARB*Math.cos(-theta));
y = (float)(0 - BARB*Math.sin(-theta));
path.lineTo(x, y);
return path;
}
private void createImage(GeneralPath path) {
Rectangle2D r = path.getBounds2D();
double y = r.getHeight()/2;
int w = (int)r.getWidth();
int h = (int)r.getHeight() + 2;
int type = BufferedImage.TYPE_INT_ARGB_PRE;
image = new BufferedImage(w, h, type);
Graphics2D g2 = image.createGraphics();
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2.setPaint(Color.red);
g2.translate(0,y);
g2.draw(path);
g2.dispose();
}
public static void main(String[] args) {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(new ArrowLines());
f.setSize(400,400);
f.setLocation(200,200);
f.setVisible(true);
}
}