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

Thread: Arraylist help

  1. #1
    Junior Member
    Join Date
    Sep 2010
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Arraylist help

    I'm kind of new at programming, and I'm trying to create an arraylist to hold 6 or 7 colors that can be used in a for loop so that they can be assigned like color(0), color(1), so that in my loop, I can make it say setColor(color(loop)). Loop is my int from my for loop. Basically, each time it runs the loop, I want the color to be the next one in the arraylist.

    for (int loop = 0; loop <= 5; loop++){
        		myPen.setColor(color(loop));
        	}

    Is this possible?


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

    Default Re: Arraylist help

    ArrayList colors = new ArrayList();
    //add all colors to the list
    for ( int i = 0; i < colors.size(); i++ ){
        myPen.setColor(colors.get(i));
    }

  3. #3
    Junior Member
    Join Date
    Sep 2010
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Arraylist help

    Well, I've been trying since you've posted that, and haven't been able to make it work. I'm not sure if I added the colors to the arraylist correctly, and "myPen.setColor(colors.get(i));" doesn't seem to want to work.

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

    Default Re: Arraylist help

    Please post the code and errors you are receiving, both of which are a wealth of information to help solve your problem

  5. #5
    Junior Member
    Join Date
    Sep 2010
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Arraylist help

    Basically it makes a circle centered in a hexagon, but once I figure out how to do this arraylist, I woulld like to make the sides keep changing colors in a pattern for a little bit. I can figure that part out, I just need to figure this out.

    import gpdraw.*;
    import java.awt.Color;
    	import java.util.ArrayList;
     
    public class Benzene {
    	private DrawingTool myPen;
        private SketchPad myPap;
     
        public Benzene(){
        	myPap = new SketchPad(800,800);
        	myPen = new DrawingTool(myPap);
        }
     
        public void draw(){
        	Color cBlue = new Color(0,0,255);
        	Color cRed = new Color(255,0,0);
        	Color cGreen = new Color(0,255,0);
        	Color cPurple = new Color(255,0,255);
        	Color cCyan = new Color(0,255,255);
        	Color cYellow = new Color(255,255,0);
        	Color cOrange = new Color(255,132,0);
        	myPen.setColor(cOrange);
        	myPen.setWidth(4);
        	myPen.drawCircle(100);
        	myPen.up();
        	myPen.move(-70,120);
        	myPen.down();
        	myPen.move(70,120);
     
    		ArrayList colors = new ArrayList();
    		colors.add(cBlue);
    		colors.add(cRed);
    		colors.add(cGreen);
    		colors.add(cPurple);
    		colors.add(cCyan);
    		colors.add(cYellow);
    		colors.add(cOrange);
     
        	for (int i = 0; i <= colors.size(); i++){
        		myPen.setColor(colors.get(i));
    			myPen.turnRight(60);
        		myPen.forward(140);
        	}
     
     
        }
    }

    setColor(java.awt.Color) in gpdraw.DrawingTool cannot be applied to (java.lang.Object)




    I have a feeling that I added the colors incorrectly.

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

    Default Re: Arraylist help

    ArrayList is generic, so if you do not define the objects put in, you must do so when you take them out. Solution 1 is to cast the object when you get it from the list
    myPen.setColor((Color)colors.get(i));
    Solution 2 - which provides compile time checks - is to use generics
    ArrayList<Color> colors = new ArrayList<Color>();

  7. The Following User Says Thank You to copeg For This Useful Post:

    TreeChopper (September 16th, 2010)

  8. #7
    Junior Member
    Join Date
    Sep 2010
    Posts
    3
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Arraylist help

    Hi TreeChopper,

    you need to replace

    for (int i = 0; i <= colors.size(); i++){
    myPen.setColor(colors.get(i));
    with

    for (int i = 0; i <= colors.size(); i++){
    myPen.setColor((Color)(colors.get(i)));
    because get() method will return Object class object which we need to cast explicitly to get our required object.

  9. The Following User Says Thank You to ramanjaneyulu For This Useful Post:

    TreeChopper (September 16th, 2010)

  10. #8
    Junior Member
    Join Date
    Sep 2010
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Arraylist help

    Thank you very much, it works exactly how I wanted it to.

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. 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
  3. ARRAYLIST
    By xs4rdx in forum Collections and Generics
    Replies: 4
    Last Post: March 25th, 2010, 10:36 AM
  4. Arraylist or Arraylist Object?
    By igniteflow in forum Collections and Generics
    Replies: 2
    Last Post: September 11th, 2009, 02:08 AM
  5. [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