Results 1 to 4 of 4
  1. #1
    FunHog is offline Member
    Join Date
    Nov 2008
    Location
    SEPA
    Posts
    4
    Rep Power
    0

    Default <Solution Found>random images on the fly??

    Hello,

    I tried to search but the search feature won't work for me...

    So here I am. I posted comments in the middle of the code where I have my questions. The program needs to display a group of three randomly generated cards saved as png files named as such...1.png, 2.png...54.png.
    I was wondering if there is a way to avoid coding each image as a private ImageIcon that I could use the randomly generated number and maybe somehow append it to a stringBuilder to get the file name to invoke. I found a lot of stuff about Applets but I am restricted to GUI and I don't know applets yet. I can make this work with a huge list of if else statements or create a switch (which now that I think of it may not work either...dang it)

    So any help is appreciated. Thanks in advance.


    import java.awt.*;
    import javax.swing.*;


    public class RandomCards extends JFrame
    {
    //I want to prevent coding all 54 png files
    private ImageIcon card1 = new ImageIcon("resources/1.png");
    private ImageIcon card2 = new ImageIcon("resources/2.png");
    private ImageIcon card3 = new ImageIcon("resources/3.png");
    private ImageIcon card4 = new ImageIcon("resources/4.png");
    private ImageIcon card5 = new ImageIcon("resources/5.png");
    private ImageIcon card6 = new ImageIcon("resources/6.png");
    private ImageIcon card7 = new ImageIcon("resources/7.png");
    private ImageIcon card8 = new ImageIcon("resources/8.png");
    private ImageIcon card9 = new ImageIcon("resources/9.png");
    private ImageIcon card10 = new ImageIcon("resources/10.png");
    private ImageIcon card11 = new ImageIcon("resources/11.png");
    private ImageIcon card12 = new ImageIcon("resources/12.png");
    private ImageIcon card13 = new ImageIcon("resources/13.png");
    private ImageIcon card14 = new ImageIcon("resources/14.png");
    private ImageIcon card15 = new ImageIcon("resources/15.png");
    private ImageIcon card16 = new ImageIcon("resources/16.png");
    private ImageIcon card17 = new ImageIcon("resources/17.png");
    private ImageIcon card18 = new ImageIcon("resources/18.png");
    private ImageIcon card19 = new ImageIcon("resources/19.png");
    private ImageIcon card20 = new ImageIcon("resources/20.png");


    RandomCards()
    {
    setLayout(new FlowLayout(FlowLayout.LEFT, 25, 10));


    for (int i = 1; i < 4; i++)
    {
    double x = Math.floor(Math.random()*54) + 1;
    if (x == 1)
    add(new JLabel(card1));
    else if (x == 2)
    add(new JLabel(card2));
    //else if for all 54 image files???

    /*Is there a better way that I can
    invoke the new private imageIcon
    stuff above only for the random
    numbers that I generate? Or do I
    have to do this for all 54 files?
    I could do a switch but I still
    need to invoke all 54 files. I was
    trying to find some way that I could
    make the file name into a string
    and print it in the command to invoke
    only the image file that I want.
    Any help would be great. Other wise
    I will just insert all of the
    files...1.png, 2.png, 3.png......54.png
    and do an ass load of if else or a huge
    switch. Thanks*/


    }

    }


    public static void main (String [] args)
    {
    RandomCards frame = new RandomCards();
    frame.setTitle("Program 13.9, 3 Random Cards");
    frame.setSize(330, 150);
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
    frame.setVisible(true);

    }
    }
    Last edited by FunHog; 03-30-2009 at 11:20 PM. Reason: Found a solution

  2. #2
    FunHog is offline Member
    Join Date
    Nov 2008
    Location
    SEPA
    Posts
    4
    Rep Power
    0

    Default

    OK so here is a solution to this...
    Although I don't know HashMaps yet that is where I am going with this....
    At the start of the program build a map of the images, and a number as the key. For example:

    Map<Integer, String> images = new HashMap<Integer, String>();
    int numImageFiles=54;
    for(int i=0; i < numImageFiles; i++)
    {
    images.put(i, "resources/" + i + ".png");
    }

    //Then to get the image out by the random number
    double cardNumber = Math.floor(Math.random()*54) + 1;
    add(new JLabel(images.get(cardNumber)));

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Rather than a HashMap, if your index is a simple int, I'd just create a simple array of String. It appears that you'd leave the first element of the array of the Array blank since there may not be a "0" image, but that's not too hard to manage.

  4. #4
    FunHog is offline Member
    Join Date
    Nov 2008
    Location
    SEPA
    Posts
    4
    Rep Power
    0

    Default

    Fub,

    I abondoned the HashMap and went with a basic String [] and it worked great. Thanks for pointing that out. Saved me a lot of hassle since I really don't know what a Hash Map is yet. I understand it is basically a type of array but thats about it so far. Thanks again.

    -FH

Similar Threads

  1. Replies: 8
    Last Post: 04-19-2009, 06:50 PM
  2. Random Div Background Images
    By mchapple in forum New To Java
    Replies: 2
    Last Post: 01-26-2009, 02:16 AM
  3. Random
    By koolhoney in forum New To Java
    Replies: 11
    Last Post: 09-30-2008, 04:39 AM
  4. Using Random
    By razmyasdfg in forum CLDC and MIDP
    Replies: 1
    Last Post: 07-27-2008, 11:47 PM
  5. random numbers without random class`
    By carlos123 in forum New To Java
    Replies: 4
    Last Post: 01-17-2008, 11:44 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •