AllExperts > Experts 
Search      

Java

Volunteer
Answers to thousands of questions
 Home · More Questions · Answer Library  · Encyclopedia ·
More Java Answers
Question Library

Ask a question about Java
Volunteer
Experts of the Month
Expert Login

Awards

About Us
Tell friends
Link to Us
Disclaimer

 
 
 
 
About Artemus 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#
 
   

You are here:  Experts > Computing/Technology > Focus on Java > Java > draw arc

Topic: Java



Expert: Artemus Harper
Date: 5/30/2008
Subject: draw arc

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);


Add to this Answer    Ask a Question



  Rate this Answer
   Was this answer helpful?
Not at allDefinitely              
   12345  

     
About Us | Advertise on This Site | User Agreement | Privacy Policy | Help
Copyright  © 2008 About, Inc. About and About.com are registered trademarks of About, Inc. The About logo is a trademark of About, Inc. All rights reserved.