Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: How to setColor with a for loop?

  1. #1
    Junior Member
    Join Date
    Jul 2019
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default How to setColor with a for loop?

    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...
    Last edited by Ricardo10; July 16th, 2019 at 08:48 PM. Reason: formatting

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: How to setColor with a for loop?

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.

    Also please copy the full text of any error messages and paste it here.

    way to change to a different color using the loop?
    Define an array of Color objects and index into it the same way code indexes into the array of Strings.
    The Color class has several static fields that define Color objects. For example: Color.RED
    Read the API doc for the Color class to see what fields it has.
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    Ricardo10 (July 17th, 2019)

  4. #3
    Junior Member
    Join Date
    Jul 2019
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: How to setColor with a for loop?

    Norm... An example would have been great... but after some digging around i did this:

     //setting color array
       Color[] nextColor = 
       {Color.GREEN,Color.BLUE,Color.RED,Color.GRAY,Color.CYAN,
       Color.ORANGE,Color.WHITE,Color.YELLOW};

    and then used the array like this:
       pie.setColor(nextColor[sliceNumber]);


    and it worked...

    Thanks for the reply.

Similar Threads

  1. loop once or loop multiple times
    By stanlj in forum Loops & Control Statements
    Replies: 3
    Last Post: November 7th, 2013, 02:14 PM
  2. help with when the for loop is met and i want to run the while loop again
    By m49er704 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: March 22nd, 2013, 09:03 AM
  3. For loop, the first command in the loop does not get executed the 2nd time..
    By lina_inverse in forum Loops & Control Statements
    Replies: 1
    Last Post: October 16th, 2012, 09:00 PM
  4. Converting a while loop to a for loop and a for loop to a while loop.
    By awesom in forum Loops & Control Statements
    Replies: 3
    Last Post: February 26th, 2012, 08:57 PM
  5. g.setColor or repaint is not working
    By sonicjr in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 14th, 2011, 07:19 AM