Shape, setClip()

How can I set a clip for something like a line.
This is what I do and need:

biG.setStroke(new BasicStroke(20,BasicStroke.CAP_ROUND,BasicStroke.JOIN_MITER));
biG.drawLine(x1,y1,x2,y2);
biG.setClip(???);
biG.drawImage(ast,x1,y1,this);

I need to draw the Image exactly on top of the line, because its a translucent one
which should add a nice texture to the background color.

How can I do this?
I tried with polygons but these only help me when the line is in a certain k*90° angle.

I don’t know how to do that for a line, but you can certainly do it for a general Shape object.

Shape myLine = ...;
g2.fill(myLine); // draw the basic line
Shape oldClip = g2.getClip();
g2.setClip(myLine);
g2.drawImage(...); // texture on top of the line
g2.setClip(oldClip);

In your case, you would need to construct a Shape that looks like the line you want, which could be quite tricky. I would probably create a GeneralPath object (which implements the Shape interface). You could maybe start with a Rectangle2D or RoundRectangle2D (necessarily aligned to the x- or y-axis), create a GeneralPath object from that, and then rotate it (through an AffineTransform) to give the line you’re after.

Alternatively, there might be a Java function that creates a Shape from a simple line definition (plus a BasicStroke object) but you’d have to hunt in the JDK documentation for it.

Hope that helps.
Simon

I had the hope there is already a java function for lines.
The only thing I found is Line2D.Double which is indeed an extension of Shape.
But using this for my clip the content of it seems to be empty so no part of my image is drawn to the screen.
Thanks for the tip :slight_smile:
I tried to rotate some rectangle in a way and it works now somehow, but I wish there was an easier method
like adding the BasicStroke to the Line2D shape, too.