I have built a program that will select 5 random cards and display the output as text. I want to add an image to display. In my code i've declared each of the card images, but I'm not quite sure how to tie in the graphic to the output.
if i randomly generate a 5 of clubs., i output the string value "Five Clubs". If i've indicated that imageIcon cicon1 = club7.jpg............I cant then follow up and say that "Five Clubs" = cicon1 can i?

I'm just confused, I don't see how if the code is randmly generating a number i would need to have a million if then statements don't i?

  • Put images in array of images
  • Run random between 0 (zero) and array.length-1
  • Problem solved

Thanks Peter. An array was suggested by someone else as well. I'm in a beginner java course so we haven't covered arrays just yet.

I'll need to get my head around how the coding will need to be done. I'm going to use what you've told me and what was also posted previously and do some more research. I'm not sure what you mean by run random. I do appreciate your post!!

  • Put images in array of images
  • Run random between 0 (zero) and array.length-1
  • Problem solved

When peter says 'run random...' he means to generate a random number that is between 0 and the length of your array - 1. The reason for this is because if an array can store 5 numbers, then array[0], array[1], array[2], array[3], and array[4] are the storage spots. Hence the array uses storage spots 0-(length of the array minus 1). Read this to learn how to generate random numbers.

Thank you for responding. I appreciate the feedback.

ok, that page helps the whole "random" piece to make sense This page was very helpful.
but although this will help me create a random number it doesn't tie in with my need to have that random number find and select a specific .jpg file as its match. which was my major stumbling block.
if i'm understanding his logic right he's going the opposite way that i was. Right now i'm generating a random number and displaying the result in a text string but i need to also attach a graphic to match that string. he's saying that i should generate 5 random images and then attach the text string but I think that still brings me full circle as to how these two pieces would come together.

When peter says 'run random...' he means to generate a random number that is between 0 and the length of your array - 1. The reason for this is because if an array can store 5 numbers, then array[0], array[1], array[2], array[3], and array[4] are the storage spots. Hence the array uses storage spots 0-(length of the array minus 1). Read this to learn how to generate random numbers.

So are you having trouble displaying the Image, or are you having trouble figuring out which image to display?

To display the image, you'll need to show it in a JPanel or JFrame using Java Swing. To figure out which image to display, you can either have 5 Image Objects, then use a bunch of if statements, or you can use an array like peter suggested. An array is the better solution but if you don't want to learn arrays yet, then you can do something like this:

ImageIcon cardOne;
ImageIcon cardTwo;
ImageIcon cardThree;
ImageIcon cardFour;
ImageIcon cardFive;


public static void main(String[] args){
Random rand = new Random(5);
if (rand == 0) //display cardOne
if (rand == 1) //display cardTwo

..
}

Of course the above is just an example to demonstrate what I'm talking about - that code is not exactly what you need, and I did not create the ImageIcons properly. If you want to show an Image in Java Swing, you should use the ImageIcon tutorial. You can't just get an image to display on the console, so if you were expecting the image to show up next to your text output, then it isn't going to happen. But you can use Java Swing to display Images, text boxes, labels, etc. So one possibility would be to use an ImageIcon which allows you to put a picture up along with text for that picture.

I have been able to get an image to display ( i've tested the program by having a static image display to make sure that the code worked) the problem i was having was how to tell the program if the return value is 5 clubs then grab the 5club.png file.

So in your example it will randomly display the cards and what i could do is add the system output line to have the value display as a string below the card? right? This is the opposite way that i was going with it but it almost seems a bit easier...i think.

So are you having trouble displaying the Image, or are you having trouble figuring out which image to display?

To display the image, you'll need to show it in a JPanel or JFrame using Java Swing. To figure out which image to display, you can either have 5 Image Objects, then use a bunch of if statements, or you can use an array like peter suggested. An array is the better solution but if you don't want to learn arrays yet, then you can do something like this:

ImageIcon cardOne;
ImageIcon cardTwo;
ImageIcon cardThree;
ImageIcon cardFour;
ImageIcon cardFive;


public static void main(String[] args){
Random rand = new Random(5);
if (rand == 0) //display cardOne
if (rand == 1) //display cardTwo

..
}

Of course the above is just an example to demonstrate what I'm talking about - that code is not exactly what you need, and I did not create the ImageIcons properly. If you want to show an Image in Java Swing, you should use the ImageIcon tutorial. You can't just get an image to display on the console, so if you were expecting the image to show up next to your text output, then it isn't going to happen. But you can use Java Swing to display Images, text boxes, labels, etc. So one possibility would be to use an ImageIcon which allows you to put a picture up along with text for that picture.

Can you show me what code you have so far? For example, were you already using Swing or were you using something else to display the image?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.