import javax.swing.*;
import java.awt.*;
import java.util.Random;
public class cah_Ch12_No9 extends JFrame
{
public cah_Ch12_No9()
{
//Create a random object
Random r = new Random();
//Create a string array of objects
String [] cards = {"1.png", "2.png", "3.png", "4.png", "5.png", "6.png", "7.png", "8.png", "9.png", "10.png",
"11.png", "12.png", "13.png", "14.png", "15.png", "16.png", "17.png", "18.png", "19.png", "20.png",
"21.png", "22.png", "23.png", "24.png", "25.png", "26.png", "27.png", "28.png", "29.png", "30.png",
"31.png", "32.png", "33.png", "34.png", "35.png", "36.png", "37.png", "38.png", "39.png", "40.png",
"41.png", "42.png", "43.png", "44.png", "45.png", "46.png", "47.png", "48.png", "49.png", "50.png",
"51.png", "52.png"};
//Layout setup
setLayout(new FlowLayout(FlowLayout.CENTER, 10, 10));
//Add the first panel.
JPanel panel1 = new JPanel();
//Add the first panel to the frame
add(panel1);
//choose random objects from the array and add it to panel 1 as a label.
for (int i = 0; i < cards.length; i++)
{
panel1.add(new JLabel(r.nextInt(cards.length)));
}
Diplay a random object from a string array
Page 1 of 14 Replies - 3244 Views - Last Post: 22 April 2010 - 05:45 AM
#1
Diplay a random object from a string array
Posted 21 April 2010 - 11:21 AM
I need to display 3 random cards as labels in panels. (Each panel containing one random card)I'm still working on the first panel right now and I'm stuck. Here's what I have. Any help is appreciated.
Replies To: Diplay a random object from a string array
#2
Re: Diplay a random object from a string array
Posted 21 April 2010 - 11:44 AM
Might I make a suggestion for your card array?
for(int i = 0; i < 52; i++)
{
cards[i] = (i+1) + ".png";
}
This post has been edited by zim1985: 21 April 2010 - 07:29 PM
#3
Re: Diplay a random object from a string array
Posted 21 April 2010 - 11:59 AM
You might find life a lot easier if you use an ArrayList instead of a static Array for your cards. Reason is, is that if you have 52 cards you want to be able to remove that card from your deck because it's now on your panel. and ArrayList has a nice function called remove that does two things one it returns what ever card is in that position and then deletes it from your array. i'll give you an example.
this is from one of my own programs. it makes 3 decks of cards. I have a ArrayList<String> deck that starts off empty. then it enters those two for loops. first loop is how many ranks i have (2 - Ace) then and second loop is for suit (heart, club, diamond, spade) so my string something would read
"C,A,1.png" for the first time through. and than it would add that string 3 times into deck. (there by giving me 3 decks of cards. I can add more or less just by adding or removing that one command deck.add(something);
easy enough. All this does is makes a temporary array. removes a random card from my deck and puts it right away into my temp array. once it's finished getting all my cards i just return tempArray because it now has 5 cards in it.
or you could use
public void newDeck()
{
int c=1;
for (int a=0;a<rank.length;a++) //0-13
{
for (int b=0;b<suits.length;b++) //0-4
{
String something = suits[b]+"," + rank[a]+","+c+".png";
deck.add(something);
deck.add(something);
deck.add(something);
c++;
}
}
}
this is from one of my own programs. it makes 3 decks of cards. I have a ArrayList<String> deck that starts off empty. then it enters those two for loops. first loop is how many ranks i have (2 - Ace) then and second loop is for suit (heart, club, diamond, spade) so my string something would read
"C,A,1.png" for the first time through. and than it would add that string 3 times into deck. (there by giving me 3 decks of cards. I can add more or less just by adding or removing that one command deck.add(something);
public ArrayList<String> getFiveCards() //returns a ArrayList with 5 cards in it.
{
ArrayList<String> tempArray = new ArrayList();
Random randInt = new Random();
for (int a=0;a<5;a++) //loop 5 times for 5 cards
{
int b = randInt.nextInt(deck.size()); //makes it easier to read my random integer
tempArray.add(deck.remove(B)/>); // see how i add to tempArray what i removed from deck
}
return tempArray;
}
easy enough. All this does is makes a temporary array. removes a random card from my deck and puts it right away into my temp array. once it's finished getting all my cards i just return tempArray because it now has 5 cards in it.
Quote
Might I make a suggestion for your card array?
for(int i = 0; i < 52; i++)
{
cards[num] = (i+1) + ".png";
}
for(int i = 0; i < 52; i++)
{
cards[num] = (i+1) + ".png";
}
or you could use
for (int i=1; i<=52; i++)
{
cards[num] = i+".png";
}
This post has been edited by immeraufdemhund: 21 April 2010 - 12:00 PM
#4
Re: Diplay a random object from a string array
Posted 21 April 2010 - 07:29 PM
Or once you "remove" a card and put it in the panel, set that slot on the array to -1 to indicate the absence of a card. Then you just have provide an if statement and some code that will deal with the selected slot being -1. You would probably find it's -1 and then just pick another one and check again.
#5
Re: Diplay a random object from a string array
Posted 22 April 2010 - 05:45 AM
in my code you wont need to give a -1 value because the spot in that array is missing. say my array is
//my array has -> {1,2,3,4,5,6,7,8,9,10}
System.out.println(myArray.get(0)); //it would print 1
//myArray = {1,2,3,4,5,6,7,8,9,10}
System.out.println(myArray.set(2,-1)); //it would print 3
//myArray = {1,2,-1,4,5,6,7,8,9,10}
System.out.println(myArray.remove(2)); //it would print -1
//myArray = {1,2,4,5,6,7,8,9,10}
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote





|