im trying to draw an arc – just a simple looking arc from point (x1,y1) to point (x2,y2)
how do i do that?
i been using the so complex and not freindly to user method called drawArc on Graphics class. no luck yet tho.
thats what i tried:
void drawArc(Graphics2D g, int x1, int y1, int x2, int y2) {
AffineTransform prev = g.getTransform();
double dx = x2 - x1, dy = y2 - y1;
double angle = Math.atan2(dy, dx);
int len = (int) Math.sqrt(dx*dx + dy*dy);
AffineTransform at = AffineTransform.getTranslateInstance(x1, y1);
at.rotate(angle);
g.transform(at);
g.drawArc(len/2, len/2, len ,len/2, 0, 60);
g.setTransform(prev);
}
thanks ahead.
graphics.drawLine(x1,y1,x2,y2)is the simplest possible arc that you can draw with these information.Probably it is not what you want. If you want something more … curvy you need to define somehow how curvy it is, in what direction. The
drawArcmethod requires you to calculate an ellipse that touches both points. The arc is the segment of the circle between those points. There is an infinite number of possible ellipses. (ThedrawLineexample assumes an infinite ellipse.) But this requires more information (what ellipse to chose) and some calculation.If you want to draw curves between two points and control points (what you probably want) you need to look into QuadCurve2D or CubicCurve2D and
drawShape. You can find sample code here.