AboutArtemus Harper Expertise I have a BS in computer science and am working towards a Masters degree.
Experience I have experience in Core Java, good background in Java swing/gui, some experience with JNI, Java reflection, and Java java.nio.*
knowledge of Java bytecode and annotations.
Basics in c++ and c#
Question Hi..
i have problem to draw a arc in java..
i want draw a arch from pixel to pixel but
not use method drawArc() in grapics method..
i want draw arc by using 3 koordinat
drawArcApp(int x1, int y1, int x2, int y2, int x3, int y3, color)
{
//x1,y1 is first coordinat
//x2,y2 is middle coordinat
//x3,y3 last coordinat
}
so..would you help me???
Answer If you have the coordinates in this format, you can use Path2D.Float (or GeneralPath if you aren't using Java 1.6 which has the same methods).
Simply:
import java.awt.geom.*;
import java.awt.*;
...
public void drawArc(Grahics2D g, int x1, int y1, int x2, int y2, int x3, int y3, Paint color)
{
Path2D.Float path = new Path2D.Float();
path.moveTo(x1,y1);
path.quadTo(x2,y2,x3,y3);
Paint previousPaint = g.getPaint();
g.setPaint(color);
g.draw(path);
g.setPaint(previousPaint);
}
Note that Paint is a super class of Color, so you can pass Color.BLUE as the paint parameter.
You can look at the source code for Path2D for inspiration if you like.
To draw "pixel-to-pixel" you would have to work on the raster layer. I highly do not recommend this, and instead recommend that you simply draw a line or polyline between each point on the arc (even if the line is only 2 pixels).
If you want to do this manually you will need to do some math.
you will need to determine the radius of the arc, which is the distance from the center to some point on the arc (which will change if this is not a circular arc).
Then you iterate from the start angle to the end angle (0 to 2 PI is a full circle).
For each iteration, you will take sin(angle) to determine the x value and cos(angle) to determine the y value. Then multiply that by the distance.
E.g.
int x = (int)(Math.sin(angle) * distance);
int y = (int)(Math.cos(angle) * distance);