14 Replies - 2735 Views - Last Post: 07 February 2011 - 07:51 PM Rate Topic: -----

#1 sultan144  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 24-June 10

Shuffling Poker cards

Posted 07 February 2011 - 11:31 AM

AM having problems with with the button that shuffles cards as u can see i called the methods cards in the actionperformer method this should shuffle the cards, but instead the window freezez. although every time i compile the program cards do shufle but obvously i dont want this i need a button that shuffles it whenever pressed, Can someone please help, and with example that would be even better. plzz

thank you


import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

	public class deal2 extends JFrame implements ActionListener 
	{
		JLabel labels[]=new JLabel[52];
		JMenuBar menubar;
		JMenu file;
	    JMenuItem reset;
		JMenuItem exit;
		
	    public deal2()
	    {
	    	cards();
			menubar = new JMenuBar();
            setJMenuBar(menubar);
	    	file = new JMenu("File");
	    	menubar.add(file);
	    	reset = new JMenuItem("Shuffle Cards");
	    	file.add(reset);
	    	exit = new JMenuItem("Quet");
	    	file.add(exit);
	    	exit.addActionListener(this);
	    	reset.addActionListener(this);	
	    }    
    	public void cards()
		{
    	for(int i=0;i<labels.length;i++)
   		{
   			labels[i]=(new JLabel(new ImageIcon(i+".png")));
    	}
    		int [] pack = new int [52];
    		int n=52;
    		for (int i=0;i<n;i++) pack[i]=i;
    		Random r= new Random();
    	for (int i=0;i<52;i++)
   		{
   		 int k= r.nextInt(n);
   	     add(labels[pack[k]]);
         pack[k]=pack[n-1];
  	     n--;
  	    }	
	  }
    	public void actionPerformed(ActionEvent e)
    {
		if(e.getSource()==exit) System.exit(0);
		if(e.getSource()==reset) cards();  //this is the problem when i pressed the reset button, cards    //should shuffle because am calling the method cards which is the method that creates and shuffles the cards.

			}
    
    	public static void main(String args[]) 
    	{ 
		deal2 deal = new deal2();
        deal.setLayout(new GridLayout(3,2)); 
        deal.setVisible(true);
    	deal.setTitle("Poker Cards shuffling");
    	deal.setBackground(Color.YELLOW);
		deal.pack();
     }
	}
 

This post has been edited by n8wxs: 07 February 2011 - 12:01 PM
Reason for edit:: Fix code tags - closing tag missing slash: [/code]


Is This A Good Question/Topic? 0
  • +

Replies To: Shuffling Poker cards

#2 sultan144  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 24-June 10

Re: Shuffling Poker cards

Posted 07 February 2011 - 11:53 AM

This is a really simple problem but sorry about it inconvenient this was annoying me for like 2 days and am desperate in need of help
Was This Post Helpful? 0
  • +
  • -

#3 Ember  Icon User is offline

  • D.I.C Head

Reputation: 70
  • View blog
  • Posts: 160
  • Joined: 24-April 10

Re: Shuffling Poker cards

Posted 07 February 2011 - 12:19 PM

No worries bro, it is not that simple of a problem.

Here is what is going down:

When you activate the cards() method, the method will create 52 NEW labels. The old labels do not get deleted because they are still referenced to by the JFrame that added them. So when you click the shuffle cards again, it will add another 52 jlabels, and continually add each time you click them, instead of the intended purpose of erasing the old 52 JLabels and replace them with the new 52, you now have 104 card pictures (or 52*amount_clicked+52 cards)!

What you need to do is stop creating new JLabels, and just change the image on the JLabels currently referenced. This is both faster and fixes the problem.

~Reply back if you need more help.

This post has been edited by Ember: 07 February 2011 - 12:21 PM

Was This Post Helpful? 1
  • +
  • -

#4 sultan144  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 24-June 10

Re: Shuffling Poker cards

Posted 07 February 2011 - 12:27 PM

Thanks Bro really appreciated, But i dont exactly Know wat u mean "just change the image on the JLabels currently referenced" because i already called all 52 cards, does this mean i have to change few things COULD YOU SHOW ME AN EXAMPLE OR wat u exactly mean THANK YOU bro.
Was This Post Helpful? 0
  • +
  • -

#5 sultan144  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 24-June 10

Re: Shuffling Poker cards

Posted 07 February 2011 - 05:18 PM

?????
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Shuffling Poker cards

Posted 07 February 2011 - 05:27 PM

You cannot just create new JLabel and adding them every time you want to shuffle

You will have to create your JLabel array only once and add them to the panel.
Then when you shuffle, do not create new JLabel but just call the setIcon() method on the already existing labels.
Was This Post Helpful? 1
  • +
  • -

#7 sultan144  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 24-June 10

Re: Shuffling Poker cards

Posted 07 February 2011 - 05:41 PM

Thank you pbl Being waiting for a perfect respond, appreciated it a lot u help me out big time, Am new TO SWINGS so am a little unexperienced at it.
:smile2:
Was This Post Helpful? 0
  • +
  • -

#8 sultan144  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 24-June 10

Re: Shuffling Poker cards

Posted 07 February 2011 - 05:57 PM

pbl Could you give me an Example?????? please if you dont mine Thank you again

AN example of what exactly you mean by creating "JLabel array only once and add them to the panel." because am not adding it to a panel Could you specify in more detail please of what you mean am new to java so please take it to consideration ??????

Because i cant forward the setIcon to the labels as i have already define them does that mean i have to re-write the code...?????

Thank you Again
Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Shuffling Poker cards

Posted 07 February 2011 - 06:10 PM

I used an arrayList that is easier to shuffle by randomly removing the cards
I read the .png only once

Et voila
import java.util.*;
import java.awt.*;
import javax.swing.*;

public class Shuffle extends JFrame {

	ImageIcon[] ii;
	JLabel[] label;
	Random ran;
	
	
	Shuffle() {
		// create only once the ImageIcon
		ii = new ImageIcon[52];
		for(int i = 0; i < 52; ++i)
			ii[i] = new ImageIcon(i + ".png");
		
		// create the 5 poker hand
		label = new JLabel[5];
		// put them into a Panel
		JPanel p = new JPanel(new GridLayout(1,5));
		for(int i = 0; i < 5; ++i) {
			label[i] = new JLabel();
			p.add(label[i]);
		}
		add(p);
		// the random generator
		ran = new Random();
			
	}
	
	// method to shuffle the cards in and put 5 in the label
	private void doShuffle() {
		// create  an ArrayList to store all the cards
		ArrayList<ImageIcon> al = new ArrayList<ImageIcon>();
		for(int i = 0; i < ii.length; ++i)
			al.add(ii[i]);
		// retreive randomly 5 cards
		for(int i = 0; i < label.length; i++) {
			// get a random index in the arrayList
			int index = ran.nextInt(al.size());
			// remove the ImageIcon from the ArrayList to put it into the 5 labels
			label[i].setIcon(al.remove(index));
		}
	}
}


Was This Post Helpful? 1
  • +
  • -

#10 sultan144  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 24-June 10

Re: Shuffling Poker cards

Posted 07 February 2011 - 06:14 PM

Can someone give me an example with the problem Am having... Can shuffle this cards even though i have defines all the method i need ???????????

THANK YOU

pbl YOU A DIAMOND MATE, Thank you soooo much...... I cant tell you How appreciated i am with the favorite, Thank you A LOOOOOOOOOOOOOOOOOOOOOOOOOOOOOT. i being on this for the last 2 days am new to JAVA only started Swings 2 weeks Ago AGAIN a million thanks to you.
Was This Post Helpful? 0
  • +
  • -

#11 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Shuffling Poker cards

Posted 07 February 2011 - 06:24 PM

What else do you need ?

You have to remove your label creation out of your card method to do it only once

label = new JLabel[52];
for(int i = 0; i < 52; ++i) {
  label[i] = new JLabel();
  add(label[i]);
}


in your cards() method use an array of 52 if you do not want to use an ArrayList
   int pack[] = new int[52];
   for(int i = 0; i < 52; ++i)
     pack[i] = i;

   for(int i = 0; i < 52; ++i) {
     // select as you do randomly
     k = ...

     // then set the Icon
     label[i].setIcon(new ImageIcon(k + ".png");
   }


Was This Post Helpful? 1
  • +
  • -

#12 sultan144  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 24-June 10

Re: Shuffling Poker cards

Posted 07 February 2011 - 07:20 PM

I really apreciated your help pdl i wanna ask you one more question if you dont mine thank u so much.
Was This Post Helpful? 0
  • +
  • -

#13 sultan144  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 24-June 10

Re: Shuffling Poker cards

Posted 07 February 2011 - 07:33 PM

would it be fine if i called the action performer like this
  
private void dealer() {
		   // create  an ArrayList to store all the cards
		    ArrayList<ImageIcon> al = new ArrayList<ImageIcon>();
		    for(int i = 0; i < ii.length; ++i)
	       al.add(ii[i]);
          // retreive randomly 5 cards
		    for(int i = 0; i < label.length; i++) {
		    // get a random index in the arrayList
		     int index = ran.nextInt(al.size());
		     // remove the ImageIcon from the ArrayList to put it into the 5 labels
		      label[i].setIcon(al.remove(index));
		    	        }
		    	    }

	public void actionPerformed(ActionEvent e) {
		if(e.getSource()==exit) System.exit(0);
		if(e.getSource()==shuffle)
                deal2(); //would this work because currently am having few problems. with this
}

 
.

Thats My last problem At the moment as you have helped me with creating the JLabel array only once and adding them to the panel. ??????

Am sorry if Am asking too many unnecessary questions.
Was This Post Helpful? 0
  • +
  • -

#14 sultan144  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 24-June 10

Re: Shuffling Poker cards

Posted 07 February 2011 - 07:45 PM

Could you show me How i can add a button that shuffles the cards???
Thank you again
Was This Post Helpful? 0
  • +
  • -

#15 sultan144  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 24-June 10

Re: Shuffling Poker cards

Posted 07 February 2011 - 07:51 PM

I HAVE DONE IT THANKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKK YOU SOOOO MUCH
AND I UNDERSTAND IT 100111& THANK YOU SOOOO MUCH.... am loving programming Now thank to the Help you give me. THANKSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS....

best wishes

Plus i learned more programming than i ever though i would in a day.... appreciated it to much
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1