Skip to main content

[JAVA2D] quadratic arrow-headed curve

4 replies [Last post]
Anonymous

Hi to every one,

what i want to achieve is to draw a quadratic arrow-headed curve between 2 arbitrary points P1,P2.
I already know how to draw a curve using the quadTo method of GeneralPath class.
i don't know how to add an arrow to either P1 or P2.

Thanks in advance

Thanasis

===========================================================================
To unsubscribe, send email to listserv@java.sun.com and include in the body
of the message "signoff JAVA2D-INTEREST". For general help, send email to
listserv@java.sun.com and include in the body of the message "help".
[att1.html]

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Alexey Ushakov

For the P2 you could use something like this:

GeneralPath path = new GeneralPath();
float p1x = 10, p1y = 10; // P1
float p2x = 100, p2y = 10; // P2
float cx = 55, cy = 50; // Control point of the curve
float arrSize = 5; // Size of the arrow segments

float adjSize = (float)(arrSize/Math.sqrt(2));
float ex = p2x - cx;
float ey = p2y - cy;
float abs_e = (float)Math.sqrt(ex*ex + ey*ey);
ex /= abs_e;
ey /= abs_e;

// Creating quad arrow
path.moveTo(p1x, p1y);
path.quadTo(cx, cy, p2x, p2y);
path.lineTo(p2x + (ey-ex)*adjSize, p2y - (ex + ey)*adjSize);
path.moveTo(p2x, p2y);
path.lineTo(p2x - (ey + ex)*adjSize, p2y + (ex - ey)*adjSize);

Best Regards,
Alexey

On Fri, Oct 28, 2005 at 08:10:51PM +0300, Thanasis (Hotmail) wrote:
> Hi to every one,
>
> what i want to achieve is to draw a quadratic arrow-headed curve between 2
> arbitrary points P1,P2.
> I already know how to draw a curve using the quadTo method of GeneralPath
> class.
> i don't know how to add an arrow to either P1 or P2.
>
> Thanks in advance
>
> Thanasis
> ===========================================================================
> To unsubscribe, send email to listserv@java.sun.com and include in the
> body of the message "signoff JAVA2D-INTEREST". For general help, send
> email to listserv@java.sun.com and include in the body of the message
> "help".

===========================================================================
To unsubscribe, send email to listserv@java.sun.com and include in the body
of the message "signoff JAVA2D-INTEREST". For general help, send email to
listserv@java.sun.com and include in the body of the message "help".

Thanasis (Hotmail)

Hi to all,

i call the method drawArrow that i describe below.When i pass as argument
a Graphics object i want my program to draw a GREEN arrow.
However when i pass as argument a Graphics2D object i want my program to
draw a RED arrow.
The problem is that i am always getting a GREEN arrow.Why does this
happen?What
===========================================================
public void drawArrow(Graphics g, int x1, int y1, int x2, int y2){

if (g.getClass().getName().equals("Graphics2D")) { // graphics2d object

Graphics2D g2=(Graphics2D)g;

g2.draw(new Line2D.Double(x1,y1,x2,y2));

GeneralPath arrow = new GeneralPath();
arrow.moveTo(xValues[0],yValues[0]);
arrow.lineTo(xValues[1],yValues[1]);
arrow.lineTo(xValues[2],yValues[2]);
arrow.lineTo(xValues[0],yValues[0]);

g2.setColor(Color.red);

g2.fill(arrow);

}
else // graphics object
{
g.drawLine(x1,y1,x2,y2);
g.setColor(Color.green);
g.fillPolygon(xValues,yValues,3);
}

}

===========================================================================
To unsubscribe, send email to listserv@java.sun.com and include in the body
of the message "signoff JAVA2D-INTEREST". For general help, send email to
listserv@java.sun.com and include in the body of the message "help".

Chris Campbell

Well, Class.getName() will include the full package name, so it won't
just return Graphics2D, it will return java.awt.Graphics2D. You're
probably better off just using "if (g instanceof Graphics2D)".

But then I'm not really sure why you need to structure the code the
way you do below. The code in your else clause is much more
straightforward, so I'm not sure why you can't just use that code
regardless of whether it's a Graphics or Graphics2D.

Chris

On Nov 22, 2005, at 3:02 PM, Thanasis (Hotmail) wrote:
> Hi to all,
>
> i call the method drawArrow that i describe below.When i pass as
> argument
> a Graphics object i want my program to draw a GREEN arrow.
> However when i pass as argument a Graphics2D object i want my
> program to
> draw a RED arrow.
> The problem is that i am always getting a GREEN arrow.Why does this
> happen?What
> ===========================================================
> public void drawArrow(Graphics g, int x1, int y1, int x2, int y2){
>
>
> if (g.getClass().getName().equals("Graphics2D")) { // graphics2d
> object
>
> Graphics2D g2=(Graphics2D)g;
>
> g2.draw(new Line2D.Double(x1,y1,x2,y2));
>
> GeneralPath arrow = new GeneralPath();
> arrow.moveTo(xValues[0],yValues[0]);
> arrow.lineTo(xValues[1],yValues[1]);
> arrow.lineTo(xValues[2],yValues[2]);
> arrow.lineTo(xValues[0],yValues[0]);
>
> g2.setColor(Color.red);
>
> g2.fill(arrow);
>
> }
> else // graphics object
> {
> g.drawLine(x1,y1,x2,y2);
> g.setColor(Color.green);
> g.fillPolygon(xValues,yValues,3);
> }
>
> }
>
> ======================================================================
> =====
> To unsubscribe, send email to listserv@java.sun.com and include in
> the body
> of the message "signoff JAVA2D-INTEREST". For general help, send
> email to
> listserv@java.sun.com and include in the body of the message "help".

===========================================================================
To unsubscribe, send email to listserv@java.sun.com and include in the body
of the message "signoff JAVA2D-INTEREST". For general help, send email to
listserv@java.sun.com and include in the body of the message "help".

David Kavanagh

The arrow is really up to you. Java2D lets you define how the line
terminates (with Stroke), but to draw an arrow head, you'll have to do
your own drawing (either shape outline and fill, or just the lines).

David

Thus Spoke Thanasis (Hotmail):

> Hi to every one,
>
> what i want to achieve is to draw a quadratic arrow-headed curve
> between 2 arbitrary points P1,P2.
> I already know how to draw a curve using the quadTo method of
> GeneralPath class.
> i don't know how to add an arrow to either P1 or P2.
>
> Thanks in advance
>
> Thanasis
> ===========================================================================
> To unsubscribe, send email to listserv@java.sun.com and include in the
> body of the message "signoff JAVA2D-INTEREST". For general help, send
> email to listserv@java.sun.com and include in the body of the message
> "help".

===========================================================================
To unsubscribe, send email to listserv@java.sun.com and include in the body
of the message "signoff JAVA2D-INTEREST". For general help, send email to
listserv@java.sun.com and include in the body of the message "help".
[att1.html]