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 7 of 7

Thread: Re: Arraylist help

  1. #1
    Junior Member cj_in_seattle's Avatar
    Join Date
    Feb 2012
    Posts
    7
    My Mood
    Tired
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Arraylist help

    No sense in starting a new thread when I can just use an old one, right?
    Hi there. Beginner programmer here. Here is my question.
    I've created an array full of circles (ovals) that go down in size with different colors. What I'm stuck on is having to rotate the colors. I'm supposed to take the center color and move it down one in the array and repeat with each, with the outermost ring color changing to the center. I'm stuck... Here is the code for the circles...
    public ArrayList createAPileOfDisks() {
    		// Get the array number of disks from the user
    		Input in = new Input();
    		int n;
    		do {
    			n = in.readIntDialog("Enter the number of disks "
    					+ "(between 1 and " + MAXIMUM_NUMBER_OF_DISKS + ")");
    			if (n <= 0 || n > MAXIMUM_NUMBER_OF_DISKS) {
    				// display an error message
    				JOptionPane.showMessageDialog(null, "Invalid input",
    						"Input Error", JOptionPane.WARNING_MESSAGE);
    			}
    		} while (n <= 0 || n > MAXIMUM_NUMBER_OF_DISKS);
     
    		// Create the list of disks
    		ArrayList<Oval> list = new ArrayList<Oval>(n);
    		int chgSize = HEIGHT;
    		for (int i = 1; i <= n; i++) {
     
    			// Find the center of the window
    			int centerX = (int) (WIDTH / 2) - (chgSize / 2);
    			int centerY = (int) (HEIGHT / 2) - (chgSize / 2);
     
    			// Draw an Oval
    			Oval o = new Oval(centerX, centerY, chgSize, chgSize,
    					randomColors(), true);
    			list.add(o);
    			chgSize -= WIDTH / n;
    		}
    		return list;
    	}
     
    public Color randomColors() {
    		Color c = new Color((int) (Math.random() * 256),
    				((int) (Math.random() * 256)), ((int) (Math.random() * 256)));
    		return c;
    	}
    How can I approach this? I tried reading what was written before, but I wasn't able to figure it out...
    Last edited by helloworld922; February 22nd, 2012 at 01:30 AM. Reason: please use code tags


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Arraylist help

    No sense in starting a new thread when I can just use an old one, right
    Yes, you should start a new thread. Please don't hijack one that is almost two years old. I have moved this to a new thread of its own for you

  3. #3
    Junior Member cj_in_seattle's Avatar
    Join Date
    Feb 2012
    Posts
    7
    My Mood
    Tired
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Arraylist help

    Ok. Sorry about that. New to all this forum stuff. Can anyone help?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Arraylist help

    'm supposed to take the center color and move it down one in the array
    Is this a problem changing the contents of an array?
    For array content movement problems you need to work out the details of the movement by using a piece of paper and a pencil to see what the order of assignments are to make sure none of the values in the array are overwritten and lost.

  5. #5
    Junior Member cj_in_seattle's Avatar
    Join Date
    Feb 2012
    Posts
    7
    My Mood
    Tired
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Arraylist help

    Quote Originally Posted by Norm View Post
    Is this a problem changing the contents of an array?
    For array content movement problems you need to work out the details of the movement by using a piece of paper and a pencil to see what the order of assignments are to make sure none of the values in the array are overwritten and lost.
    Yeah, I believe so. I knew the logic of what I wanted to do, but didn't understand the syntax. Here is the solution.

    public ArrayList<Oval> rotateColorsInPileOfDisks(ArrayList<Oval> graphicsList) {

    Color bottomDisk = graphicsList.get(0).getColor();
    for (int i = 0; i < graphicsList.size()-1 /*createAPileOfDisks().size()*/; i++) {
    Color color1 = graphicsList.get(i+1).getColor();
    graphicsList.get(i).setColor(color1);
    }
    int lastIndex = graphicsList.size() - 1;
    graphicsList.get(lastIndex).setColor(bottomDisk);
    return graphicsList;
    }

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Arraylist help

    You've solved it?

  7. #7
    Junior Member cj_in_seattle's Avatar
    Join Date
    Feb 2012
    Posts
    7
    My Mood
    Tired
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Pretty much. I got help from a fellow student. He helped to break it down and explain it. Now I need to my sierpinski triangle, but just switch the colors from blue to green, then o red and back to blue. Should be pretty straight-forward.

Similar Threads

  1. How to use an ArrayList and what is its advantage over array?
    By JavaPF in forum Java SE API Tutorials
    Replies: 4
    Last Post: December 21st, 2011, 04:44 AM
  2. private Map<String, ArrayList> xlist = new HashMap<String, ArrayList>();
    By Scotty in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 21st, 2011, 08:37 AM
  3. Ordering ArrayList by 3 conditions as you add to ArrayList
    By aussiemcgr in forum Collections and Generics
    Replies: 4
    Last Post: July 13th, 2010, 02:08 PM
  4. [SOLVED] Extracting an How to ArrayList from an ArrayList and convert to int??
    By igniteflow in forum Collections and Generics
    Replies: 2
    Last Post: August 16th, 2009, 01:11 PM
  5. How to use an ArrayList and what is its advantage over array?
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 1
    Last Post: May 17th, 2009, 01:12 PM