Hello!
So i'm learning to draw simple graphics with Java... I drew a pie chart using fillArc(.....) and splitted the pie in 8 equal pieces with an angle of
45 each...
I want each slice to have a different color... I was able to do it using a switch case but then i tried to make the code smaller using a
String array with the color names like this:
String sliceColor[] = {"GREEN","ORANGE","BLUE", etc...} 8 color names
and then i used a for loop to draw the 8 filled arcs... but the problem is that i can't change the colors in the loop for each slice using
setColor(Color.sliceColor[loop variable here]); this is where i'm stuck... any way to change to a different color using the loop?
Here's the code so far...
//-------------------------------------------- // A Pie chart drawing with 8 slices of // 45 degrees each and different colors. //-------------------------------------------- import javax.swing.JApplet; import java.awt.Graphics; import java.awt.Color; /* <applet code = "PieChart" width =1200 height = 650> </applet> */ public class PieChart extends JApplet { public static final short ARC_ANGLE = 45; //angle constant //pie slice color array - 8 colors String sliceColor[] = {"GREEN","BLACK","ORANGE","WHITE","RED", "YELLOW","BLUE","CYAN"}; public void init() { setBackground(Color.BLACK); } public void paint(Graphics pie) { short startAngle = 0; for (short sliceNumber = 0;sliceNumber < 8; sliceNumber++) { pie.setColor(Color.sliceColor[sliceNumber]); <---------------- this is what i want to make it work :confused: pie.fillArc(300,60,500,500,startAngle,ARC_ANGLE); startAngle += 45; } // end for loop } // end paint class } // end PieChart class.
Thanks...