You are here:

Java/divide circle into parts and fill with diff color

Advertisement


Question
Respected Sir/Mam,

I hv to create reports in java via coding and i have to of draw graphical reports (circle) like this approx as shown in following image:

http://www.appperfect.com/images/tutorials/code-testing-reports.png

Can you please give me some idea hw to create a circle (like that) and divide it into different parts and fill thoes parts with different color.

Thanks,
Ravi

Answer
You can use the fillArc method in Graphics to do this. Your code would look something like:

Color[] colors = ...; //Fill in for the colors you want to use
double[] percentages = ...; //Fill in with the percentage of the circle.
double startPercentage = 0.0d;
for(int i = 0; i < percentage.length; i++) {
  g.setColor(colors[i]);
  g.fillArc(x,y, width, height, startPercentage * (Math.PI * 2), percentages[i] * (Math.PI * 2)); //change x,y,width,height to the rectangle area you want to put the pie chart.
  startPercentage += percentages[i];
}

Java

All Answers


Answers by Expert:


Ask Experts

Volunteer


Artemus Harper

Expertise

I have a Masters in computer science. I can answer questions on core J2SE, swing and graphics. Please no questions about JSP or J2ME.

Experience

I have experience in Core Java, good background in Java swing/gui, some experience with JNI, Java reflection. Some experience in bio-informatics. Basics in c++ and c#

Organizations
Washington State University

Education/Credentials
MS in Computer Science from Washington State University and a BS in Mathematics and Computer Science from Central Washington University.

©2016 About.com. All rights reserved.