How to randomly assign one of four images to a field









up vote
-1
down vote

favorite












So I am doing a project for my JAVA class finals. During the assignment, there is a question ask me to randomly assign one of four images (the flower images I have) to the image field in my Flower constructor. But I did not seem to understand this requirement. Can somebody help me with this? I will greatly appreciate. Here is my code. Also, my teacher has given us a hint that we should use an "if" statement for this.



import java.awt.Point;
import javax.swing.ImageIcon;

public class Flower
private ImageIcon image;
private Point pos;

public Flower(int x, int y)
pos = new Point(x,y);












share|improve this question



















  • 2




    And the images to a List and use Collections.shuffle to randomise the List, then just grab the first one, because I'm lazy
    – MadProgrammer
    Nov 11 at 22:32










  • Thank you @MadProgrammer, since im really new to programming, how can i add all four of them into an ArrayList?
    – Marcus Nguyen
    Nov 11 at 22:43











  • Perhaps this: // OPTIONALLY accept a ImageIcon type image within // the Flower constructor. public Flower(int x, int y, ImageIcon... randomImageIcon) if (randomImageIcon.length > 0) this.image = randomImageIcon[0]; this.pos = new Point(x,y);
    – DevilsHnd
    Nov 11 at 22:43











  • hey, @DevilsHnd thank you for your help. But how do I actually make the randomImageIcon read the image files? would I have to declare Imageicon image = new image("_image path_") first?
    – Marcus Nguyen
    Nov 11 at 22:47










  • You would do this before creating an instance of Flower: ImageIcon images = new ImageIcon("your1st.png"), new ImageIcon("your2nd.png"), new ImageIcon("your3rd.png"), new ImageIcon("your4th.png"); and then Random rand = new Random(); Flower flower = new Flower(0, 10, images[rand.nextInt(4)]);.
    – DevilsHnd
    Nov 11 at 22:58














up vote
-1
down vote

favorite












So I am doing a project for my JAVA class finals. During the assignment, there is a question ask me to randomly assign one of four images (the flower images I have) to the image field in my Flower constructor. But I did not seem to understand this requirement. Can somebody help me with this? I will greatly appreciate. Here is my code. Also, my teacher has given us a hint that we should use an "if" statement for this.



import java.awt.Point;
import javax.swing.ImageIcon;

public class Flower
private ImageIcon image;
private Point pos;

public Flower(int x, int y)
pos = new Point(x,y);












share|improve this question



















  • 2




    And the images to a List and use Collections.shuffle to randomise the List, then just grab the first one, because I'm lazy
    – MadProgrammer
    Nov 11 at 22:32










  • Thank you @MadProgrammer, since im really new to programming, how can i add all four of them into an ArrayList?
    – Marcus Nguyen
    Nov 11 at 22:43











  • Perhaps this: // OPTIONALLY accept a ImageIcon type image within // the Flower constructor. public Flower(int x, int y, ImageIcon... randomImageIcon) if (randomImageIcon.length > 0) this.image = randomImageIcon[0]; this.pos = new Point(x,y);
    – DevilsHnd
    Nov 11 at 22:43











  • hey, @DevilsHnd thank you for your help. But how do I actually make the randomImageIcon read the image files? would I have to declare Imageicon image = new image("_image path_") first?
    – Marcus Nguyen
    Nov 11 at 22:47










  • You would do this before creating an instance of Flower: ImageIcon images = new ImageIcon("your1st.png"), new ImageIcon("your2nd.png"), new ImageIcon("your3rd.png"), new ImageIcon("your4th.png"); and then Random rand = new Random(); Flower flower = new Flower(0, 10, images[rand.nextInt(4)]);.
    – DevilsHnd
    Nov 11 at 22:58












up vote
-1
down vote

favorite









up vote
-1
down vote

favorite











So I am doing a project for my JAVA class finals. During the assignment, there is a question ask me to randomly assign one of four images (the flower images I have) to the image field in my Flower constructor. But I did not seem to understand this requirement. Can somebody help me with this? I will greatly appreciate. Here is my code. Also, my teacher has given us a hint that we should use an "if" statement for this.



import java.awt.Point;
import javax.swing.ImageIcon;

public class Flower
private ImageIcon image;
private Point pos;

public Flower(int x, int y)
pos = new Point(x,y);












share|improve this question















So I am doing a project for my JAVA class finals. During the assignment, there is a question ask me to randomly assign one of four images (the flower images I have) to the image field in my Flower constructor. But I did not seem to understand this requirement. Can somebody help me with this? I will greatly appreciate. Here is my code. Also, my teacher has given us a hint that we should use an "if" statement for this.



import java.awt.Point;
import javax.swing.ImageIcon;

public class Flower
private ImageIcon image;
private Point pos;

public Flower(int x, int y)
pos = new Point(x,y);









java






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 22:10









Hovercraft Full Of Eels

260k20211317




260k20211317










asked Nov 11 at 21:59









Marcus Nguyen

11




11







  • 2




    And the images to a List and use Collections.shuffle to randomise the List, then just grab the first one, because I'm lazy
    – MadProgrammer
    Nov 11 at 22:32










  • Thank you @MadProgrammer, since im really new to programming, how can i add all four of them into an ArrayList?
    – Marcus Nguyen
    Nov 11 at 22:43











  • Perhaps this: // OPTIONALLY accept a ImageIcon type image within // the Flower constructor. public Flower(int x, int y, ImageIcon... randomImageIcon) if (randomImageIcon.length > 0) this.image = randomImageIcon[0]; this.pos = new Point(x,y);
    – DevilsHnd
    Nov 11 at 22:43











  • hey, @DevilsHnd thank you for your help. But how do I actually make the randomImageIcon read the image files? would I have to declare Imageicon image = new image("_image path_") first?
    – Marcus Nguyen
    Nov 11 at 22:47










  • You would do this before creating an instance of Flower: ImageIcon images = new ImageIcon("your1st.png"), new ImageIcon("your2nd.png"), new ImageIcon("your3rd.png"), new ImageIcon("your4th.png"); and then Random rand = new Random(); Flower flower = new Flower(0, 10, images[rand.nextInt(4)]);.
    – DevilsHnd
    Nov 11 at 22:58












  • 2




    And the images to a List and use Collections.shuffle to randomise the List, then just grab the first one, because I'm lazy
    – MadProgrammer
    Nov 11 at 22:32










  • Thank you @MadProgrammer, since im really new to programming, how can i add all four of them into an ArrayList?
    – Marcus Nguyen
    Nov 11 at 22:43











  • Perhaps this: // OPTIONALLY accept a ImageIcon type image within // the Flower constructor. public Flower(int x, int y, ImageIcon... randomImageIcon) if (randomImageIcon.length > 0) this.image = randomImageIcon[0]; this.pos = new Point(x,y);
    – DevilsHnd
    Nov 11 at 22:43











  • hey, @DevilsHnd thank you for your help. But how do I actually make the randomImageIcon read the image files? would I have to declare Imageicon image = new image("_image path_") first?
    – Marcus Nguyen
    Nov 11 at 22:47










  • You would do this before creating an instance of Flower: ImageIcon images = new ImageIcon("your1st.png"), new ImageIcon("your2nd.png"), new ImageIcon("your3rd.png"), new ImageIcon("your4th.png"); and then Random rand = new Random(); Flower flower = new Flower(0, 10, images[rand.nextInt(4)]);.
    – DevilsHnd
    Nov 11 at 22:58







2




2




And the images to a List and use Collections.shuffle to randomise the List, then just grab the first one, because I'm lazy
– MadProgrammer
Nov 11 at 22:32




And the images to a List and use Collections.shuffle to randomise the List, then just grab the first one, because I'm lazy
– MadProgrammer
Nov 11 at 22:32












Thank you @MadProgrammer, since im really new to programming, how can i add all four of them into an ArrayList?
– Marcus Nguyen
Nov 11 at 22:43





Thank you @MadProgrammer, since im really new to programming, how can i add all four of them into an ArrayList?
– Marcus Nguyen
Nov 11 at 22:43













Perhaps this: // OPTIONALLY accept a ImageIcon type image within // the Flower constructor. public Flower(int x, int y, ImageIcon... randomImageIcon) if (randomImageIcon.length > 0) this.image = randomImageIcon[0]; this.pos = new Point(x,y);
– DevilsHnd
Nov 11 at 22:43





Perhaps this: // OPTIONALLY accept a ImageIcon type image within // the Flower constructor. public Flower(int x, int y, ImageIcon... randomImageIcon) if (randomImageIcon.length > 0) this.image = randomImageIcon[0]; this.pos = new Point(x,y);
– DevilsHnd
Nov 11 at 22:43













hey, @DevilsHnd thank you for your help. But how do I actually make the randomImageIcon read the image files? would I have to declare Imageicon image = new image("_image path_") first?
– Marcus Nguyen
Nov 11 at 22:47




hey, @DevilsHnd thank you for your help. But how do I actually make the randomImageIcon read the image files? would I have to declare Imageicon image = new image("_image path_") first?
– Marcus Nguyen
Nov 11 at 22:47












You would do this before creating an instance of Flower: ImageIcon images = new ImageIcon("your1st.png"), new ImageIcon("your2nd.png"), new ImageIcon("your3rd.png"), new ImageIcon("your4th.png"); and then Random rand = new Random(); Flower flower = new Flower(0, 10, images[rand.nextInt(4)]);.
– DevilsHnd
Nov 11 at 22:58




You would do this before creating an instance of Flower: ImageIcon images = new ImageIcon("your1st.png"), new ImageIcon("your2nd.png"), new ImageIcon("your3rd.png"), new ImageIcon("your4th.png"); and then Random rand = new Random(); Flower flower = new Flower(0, 10, images[rand.nextInt(4)]);.
– DevilsHnd
Nov 11 at 22:58












2 Answers
2






active

oldest

votes

















up vote
0
down vote













Generating a random number from 0 to 4 will do the trick for you.
Suppose the 4 images you have is in the form of array of type ImageIcon.
If the name of the array is list_flowers.
your constructor can be-



public Flower(int x, int y) 
Random rand = new Random();
pos = new Point(x,y);
image = list_flowers[rand.nextInt(4)];






share|improve this answer




















  • Hey thank you so much Anju, however, my images are png file types... so I don't think doing that will help with my problem :(
    – Marcus Nguyen
    Nov 11 at 22:30






  • 1




    public Flower(int x, int y) { Random rand = new Random(); The Random instance should be declared once, outside the method. Otherwise if the code calls that method from within a loop, the 'random' flower is likely to be exactly the same. Note also that using a random number for an array index is how I or you might do it, but the OP mentions the teacher hinted at using if statements. The only way I can figure to incorporate conditional statements is to produce a pair (for 4 images) of boolean values.
    – Andrew Thompson
    Nov 11 at 22:32






  • 1




    "my images are png file types" This answer will work for any image type. (The ImageIcon will work for any image that Java supports in the core API.)
    – Andrew Thompson
    Nov 11 at 22:33










  • @AndrewThompson thank you for willing to help me! but by using the trick that Anju just stated, our class has not even studied about that yet. Thus, that's why the teacher wants us to do it with the if statement
    – Marcus Nguyen
    Nov 11 at 22:38










  • "our class has not even studied about that yet" GUIs are an advanced subject. Before starting them, a programmer should be familiar with the basics of the language & simple computing concepts like loops and conditional statements, as well as basic data structures such as arrays and collections. So this teacher - throwing the class into making a GUI based app. - is .. not very competent.
    – Andrew Thompson
    Nov 11 at 22:50

















up vote
0
down vote













First things first, you need to change Flower so you can pass it the image to be used.



public class Flower 
private ImageIcon image;
private Point pos;

public Flower(ImageIcon image, int x, int y)
pos = new Point(x,y);





Personal thing, but I prefer it this way, as the result of initialising the class can be reasoned about.



One solution would be to make use of the available functionality in the Java API. Because I'm lazy, this would mean making use of Collections.shuffle to "randomise" a list of objects.



It might go something like....



 List<ImageIcon> images = new ArrayList<>(4);
images.add(new ImageIcon(...)); // Flower 1
images.add(new ImageIcon(...)); // Flower 2
images.add(new ImageIcon(...)); // Flower 3
images.add(new ImageIcon(...)); // Flower 4

for (int index = 0; index < numberOfFlowersToCreate; index++)
int xPos = ...; // Calculate x position
int yPos = ...; // Calculate y position

Collections.shuffle(images);
ImageIcon image = images.get(0);
Flower flower = new Flower(image, xPos, yPos);

// Do something with the instance of Flower






share|improve this answer




















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53253664%2fhow-to-randomly-assign-one-of-four-images-to-a-field%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    Generating a random number from 0 to 4 will do the trick for you.
    Suppose the 4 images you have is in the form of array of type ImageIcon.
    If the name of the array is list_flowers.
    your constructor can be-



    public Flower(int x, int y) 
    Random rand = new Random();
    pos = new Point(x,y);
    image = list_flowers[rand.nextInt(4)];






    share|improve this answer




















    • Hey thank you so much Anju, however, my images are png file types... so I don't think doing that will help with my problem :(
      – Marcus Nguyen
      Nov 11 at 22:30






    • 1




      public Flower(int x, int y) { Random rand = new Random(); The Random instance should be declared once, outside the method. Otherwise if the code calls that method from within a loop, the 'random' flower is likely to be exactly the same. Note also that using a random number for an array index is how I or you might do it, but the OP mentions the teacher hinted at using if statements. The only way I can figure to incorporate conditional statements is to produce a pair (for 4 images) of boolean values.
      – Andrew Thompson
      Nov 11 at 22:32






    • 1




      "my images are png file types" This answer will work for any image type. (The ImageIcon will work for any image that Java supports in the core API.)
      – Andrew Thompson
      Nov 11 at 22:33










    • @AndrewThompson thank you for willing to help me! but by using the trick that Anju just stated, our class has not even studied about that yet. Thus, that's why the teacher wants us to do it with the if statement
      – Marcus Nguyen
      Nov 11 at 22:38










    • "our class has not even studied about that yet" GUIs are an advanced subject. Before starting them, a programmer should be familiar with the basics of the language & simple computing concepts like loops and conditional statements, as well as basic data structures such as arrays and collections. So this teacher - throwing the class into making a GUI based app. - is .. not very competent.
      – Andrew Thompson
      Nov 11 at 22:50














    up vote
    0
    down vote













    Generating a random number from 0 to 4 will do the trick for you.
    Suppose the 4 images you have is in the form of array of type ImageIcon.
    If the name of the array is list_flowers.
    your constructor can be-



    public Flower(int x, int y) 
    Random rand = new Random();
    pos = new Point(x,y);
    image = list_flowers[rand.nextInt(4)];






    share|improve this answer




















    • Hey thank you so much Anju, however, my images are png file types... so I don't think doing that will help with my problem :(
      – Marcus Nguyen
      Nov 11 at 22:30






    • 1




      public Flower(int x, int y) { Random rand = new Random(); The Random instance should be declared once, outside the method. Otherwise if the code calls that method from within a loop, the 'random' flower is likely to be exactly the same. Note also that using a random number for an array index is how I or you might do it, but the OP mentions the teacher hinted at using if statements. The only way I can figure to incorporate conditional statements is to produce a pair (for 4 images) of boolean values.
      – Andrew Thompson
      Nov 11 at 22:32






    • 1




      "my images are png file types" This answer will work for any image type. (The ImageIcon will work for any image that Java supports in the core API.)
      – Andrew Thompson
      Nov 11 at 22:33










    • @AndrewThompson thank you for willing to help me! but by using the trick that Anju just stated, our class has not even studied about that yet. Thus, that's why the teacher wants us to do it with the if statement
      – Marcus Nguyen
      Nov 11 at 22:38










    • "our class has not even studied about that yet" GUIs are an advanced subject. Before starting them, a programmer should be familiar with the basics of the language & simple computing concepts like loops and conditional statements, as well as basic data structures such as arrays and collections. So this teacher - throwing the class into making a GUI based app. - is .. not very competent.
      – Andrew Thompson
      Nov 11 at 22:50












    up vote
    0
    down vote










    up vote
    0
    down vote









    Generating a random number from 0 to 4 will do the trick for you.
    Suppose the 4 images you have is in the form of array of type ImageIcon.
    If the name of the array is list_flowers.
    your constructor can be-



    public Flower(int x, int y) 
    Random rand = new Random();
    pos = new Point(x,y);
    image = list_flowers[rand.nextInt(4)];






    share|improve this answer












    Generating a random number from 0 to 4 will do the trick for you.
    Suppose the 4 images you have is in the form of array of type ImageIcon.
    If the name of the array is list_flowers.
    your constructor can be-



    public Flower(int x, int y) 
    Random rand = new Random();
    pos = new Point(x,y);
    image = list_flowers[rand.nextInt(4)];







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 11 at 22:27









    Anju Geetha Nair

    112




    112











    • Hey thank you so much Anju, however, my images are png file types... so I don't think doing that will help with my problem :(
      – Marcus Nguyen
      Nov 11 at 22:30






    • 1




      public Flower(int x, int y) { Random rand = new Random(); The Random instance should be declared once, outside the method. Otherwise if the code calls that method from within a loop, the 'random' flower is likely to be exactly the same. Note also that using a random number for an array index is how I or you might do it, but the OP mentions the teacher hinted at using if statements. The only way I can figure to incorporate conditional statements is to produce a pair (for 4 images) of boolean values.
      – Andrew Thompson
      Nov 11 at 22:32






    • 1




      "my images are png file types" This answer will work for any image type. (The ImageIcon will work for any image that Java supports in the core API.)
      – Andrew Thompson
      Nov 11 at 22:33










    • @AndrewThompson thank you for willing to help me! but by using the trick that Anju just stated, our class has not even studied about that yet. Thus, that's why the teacher wants us to do it with the if statement
      – Marcus Nguyen
      Nov 11 at 22:38










    • "our class has not even studied about that yet" GUIs are an advanced subject. Before starting them, a programmer should be familiar with the basics of the language & simple computing concepts like loops and conditional statements, as well as basic data structures such as arrays and collections. So this teacher - throwing the class into making a GUI based app. - is .. not very competent.
      – Andrew Thompson
      Nov 11 at 22:50
















    • Hey thank you so much Anju, however, my images are png file types... so I don't think doing that will help with my problem :(
      – Marcus Nguyen
      Nov 11 at 22:30






    • 1




      public Flower(int x, int y) { Random rand = new Random(); The Random instance should be declared once, outside the method. Otherwise if the code calls that method from within a loop, the 'random' flower is likely to be exactly the same. Note also that using a random number for an array index is how I or you might do it, but the OP mentions the teacher hinted at using if statements. The only way I can figure to incorporate conditional statements is to produce a pair (for 4 images) of boolean values.
      – Andrew Thompson
      Nov 11 at 22:32






    • 1




      "my images are png file types" This answer will work for any image type. (The ImageIcon will work for any image that Java supports in the core API.)
      – Andrew Thompson
      Nov 11 at 22:33










    • @AndrewThompson thank you for willing to help me! but by using the trick that Anju just stated, our class has not even studied about that yet. Thus, that's why the teacher wants us to do it with the if statement
      – Marcus Nguyen
      Nov 11 at 22:38










    • "our class has not even studied about that yet" GUIs are an advanced subject. Before starting them, a programmer should be familiar with the basics of the language & simple computing concepts like loops and conditional statements, as well as basic data structures such as arrays and collections. So this teacher - throwing the class into making a GUI based app. - is .. not very competent.
      – Andrew Thompson
      Nov 11 at 22:50















    Hey thank you so much Anju, however, my images are png file types... so I don't think doing that will help with my problem :(
    – Marcus Nguyen
    Nov 11 at 22:30




    Hey thank you so much Anju, however, my images are png file types... so I don't think doing that will help with my problem :(
    – Marcus Nguyen
    Nov 11 at 22:30




    1




    1




    public Flower(int x, int y) { Random rand = new Random(); The Random instance should be declared once, outside the method. Otherwise if the code calls that method from within a loop, the 'random' flower is likely to be exactly the same. Note also that using a random number for an array index is how I or you might do it, but the OP mentions the teacher hinted at using if statements. The only way I can figure to incorporate conditional statements is to produce a pair (for 4 images) of boolean values.
    – Andrew Thompson
    Nov 11 at 22:32




    public Flower(int x, int y) { Random rand = new Random(); The Random instance should be declared once, outside the method. Otherwise if the code calls that method from within a loop, the 'random' flower is likely to be exactly the same. Note also that using a random number for an array index is how I or you might do it, but the OP mentions the teacher hinted at using if statements. The only way I can figure to incorporate conditional statements is to produce a pair (for 4 images) of boolean values.
    – Andrew Thompson
    Nov 11 at 22:32




    1




    1




    "my images are png file types" This answer will work for any image type. (The ImageIcon will work for any image that Java supports in the core API.)
    – Andrew Thompson
    Nov 11 at 22:33




    "my images are png file types" This answer will work for any image type. (The ImageIcon will work for any image that Java supports in the core API.)
    – Andrew Thompson
    Nov 11 at 22:33












    @AndrewThompson thank you for willing to help me! but by using the trick that Anju just stated, our class has not even studied about that yet. Thus, that's why the teacher wants us to do it with the if statement
    – Marcus Nguyen
    Nov 11 at 22:38




    @AndrewThompson thank you for willing to help me! but by using the trick that Anju just stated, our class has not even studied about that yet. Thus, that's why the teacher wants us to do it with the if statement
    – Marcus Nguyen
    Nov 11 at 22:38












    "our class has not even studied about that yet" GUIs are an advanced subject. Before starting them, a programmer should be familiar with the basics of the language & simple computing concepts like loops and conditional statements, as well as basic data structures such as arrays and collections. So this teacher - throwing the class into making a GUI based app. - is .. not very competent.
    – Andrew Thompson
    Nov 11 at 22:50




    "our class has not even studied about that yet" GUIs are an advanced subject. Before starting them, a programmer should be familiar with the basics of the language & simple computing concepts like loops and conditional statements, as well as basic data structures such as arrays and collections. So this teacher - throwing the class into making a GUI based app. - is .. not very competent.
    – Andrew Thompson
    Nov 11 at 22:50












    up vote
    0
    down vote













    First things first, you need to change Flower so you can pass it the image to be used.



    public class Flower 
    private ImageIcon image;
    private Point pos;

    public Flower(ImageIcon image, int x, int y)
    pos = new Point(x,y);





    Personal thing, but I prefer it this way, as the result of initialising the class can be reasoned about.



    One solution would be to make use of the available functionality in the Java API. Because I'm lazy, this would mean making use of Collections.shuffle to "randomise" a list of objects.



    It might go something like....



     List<ImageIcon> images = new ArrayList<>(4);
    images.add(new ImageIcon(...)); // Flower 1
    images.add(new ImageIcon(...)); // Flower 2
    images.add(new ImageIcon(...)); // Flower 3
    images.add(new ImageIcon(...)); // Flower 4

    for (int index = 0; index < numberOfFlowersToCreate; index++)
    int xPos = ...; // Calculate x position
    int yPos = ...; // Calculate y position

    Collections.shuffle(images);
    ImageIcon image = images.get(0);
    Flower flower = new Flower(image, xPos, yPos);

    // Do something with the instance of Flower






    share|improve this answer
























      up vote
      0
      down vote













      First things first, you need to change Flower so you can pass it the image to be used.



      public class Flower 
      private ImageIcon image;
      private Point pos;

      public Flower(ImageIcon image, int x, int y)
      pos = new Point(x,y);





      Personal thing, but I prefer it this way, as the result of initialising the class can be reasoned about.



      One solution would be to make use of the available functionality in the Java API. Because I'm lazy, this would mean making use of Collections.shuffle to "randomise" a list of objects.



      It might go something like....



       List<ImageIcon> images = new ArrayList<>(4);
      images.add(new ImageIcon(...)); // Flower 1
      images.add(new ImageIcon(...)); // Flower 2
      images.add(new ImageIcon(...)); // Flower 3
      images.add(new ImageIcon(...)); // Flower 4

      for (int index = 0; index < numberOfFlowersToCreate; index++)
      int xPos = ...; // Calculate x position
      int yPos = ...; // Calculate y position

      Collections.shuffle(images);
      ImageIcon image = images.get(0);
      Flower flower = new Flower(image, xPos, yPos);

      // Do something with the instance of Flower






      share|improve this answer






















        up vote
        0
        down vote










        up vote
        0
        down vote









        First things first, you need to change Flower so you can pass it the image to be used.



        public class Flower 
        private ImageIcon image;
        private Point pos;

        public Flower(ImageIcon image, int x, int y)
        pos = new Point(x,y);





        Personal thing, but I prefer it this way, as the result of initialising the class can be reasoned about.



        One solution would be to make use of the available functionality in the Java API. Because I'm lazy, this would mean making use of Collections.shuffle to "randomise" a list of objects.



        It might go something like....



         List<ImageIcon> images = new ArrayList<>(4);
        images.add(new ImageIcon(...)); // Flower 1
        images.add(new ImageIcon(...)); // Flower 2
        images.add(new ImageIcon(...)); // Flower 3
        images.add(new ImageIcon(...)); // Flower 4

        for (int index = 0; index < numberOfFlowersToCreate; index++)
        int xPos = ...; // Calculate x position
        int yPos = ...; // Calculate y position

        Collections.shuffle(images);
        ImageIcon image = images.get(0);
        Flower flower = new Flower(image, xPos, yPos);

        // Do something with the instance of Flower






        share|improve this answer












        First things first, you need to change Flower so you can pass it the image to be used.



        public class Flower 
        private ImageIcon image;
        private Point pos;

        public Flower(ImageIcon image, int x, int y)
        pos = new Point(x,y);





        Personal thing, but I prefer it this way, as the result of initialising the class can be reasoned about.



        One solution would be to make use of the available functionality in the Java API. Because I'm lazy, this would mean making use of Collections.shuffle to "randomise" a list of objects.



        It might go something like....



         List<ImageIcon> images = new ArrayList<>(4);
        images.add(new ImageIcon(...)); // Flower 1
        images.add(new ImageIcon(...)); // Flower 2
        images.add(new ImageIcon(...)); // Flower 3
        images.add(new ImageIcon(...)); // Flower 4

        for (int index = 0; index < numberOfFlowersToCreate; index++)
        int xPos = ...; // Calculate x position
        int yPos = ...; // Calculate y position

        Collections.shuffle(images);
        ImageIcon image = images.get(0);
        Flower flower = new Flower(image, xPos, yPos);

        // Do something with the instance of Flower







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 11 at 22:55









        MadProgrammer

        298k17152264




        298k17152264



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53253664%2fhow-to-randomly-assign-one-of-four-images-to-a-field%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            How to read a connectionString WITH PROVIDER in .NET Core?

            Node.js Script on GitHub Pages or Amazon S3

            Museum of Modern and Contemporary Art of Trento and Rovereto