Deck of Cards Applet how to shuffle and display

  • (2 Pages)
  • +
  • 1
  • 2

27 Replies - 1682 Views - Last Post: 17 January 2013 - 10:25 PM Rate Topic: -----

#16 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10385
  • View blog
  • Posts: 38,434
  • Joined: 27-December 08

Re: Deck of Cards Applet how to shuffle and display

Posted 17 January 2013 - 12:57 AM

Quote

So now I can use the Collection.shuffle ( images) and randomly remove elements?

You can use either. You don't have to use both approaches.
Was This Post Helpful? 0
  • +
  • -

#17 bwong23  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 29
  • Joined: 14-January 13

Re: Deck of Cards Applet how to shuffle and display

Posted 17 January 2013 - 01:03 AM

Okay, so my code is down below, it compiles and I can use the appletviewer to make the program run. However, when I run the program, the applet viewer pops up, but with nothing on it, just a blank white screen? And is there a way to, after shuffling, uses the public void paint ( Graphics g ) and do ten g.drawImages to display the cards?

import java.awt.Graphics;
import java.awt.Image;
import java.applet.Applet;
import java.util.*;

public class DeckofCards1 extends Applet {

    public void init ( ) {
        String[] suits = {"c", "s", "h", "d"};
        String[] values = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "j", "q", "k"};

        ArrayList<Image> images = new ArrayList<Image>( );
        for(String suit : suits){
                for(String value : values){
                        Image img = getImage ( getDocumentBase ( ), "images/" + suit + value + ".gif" );
	      images.add(img);
                         }
        }
        Collections.shuffle ( images );
        System.out.println ( "images " );
        }
      
}


This post has been edited by bwong23: 17 January 2013 - 01:05 AM

Was This Post Helpful? 0
  • +
  • -

#18 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10385
  • View blog
  • Posts: 38,434
  • Joined: 27-December 08

Re: Deck of Cards Applet how to shuffle and display

Posted 17 January 2013 - 01:18 AM

You've basically just described the approach yourself. Rather than asking me to hold your hand, write code and give it a try.
Was This Post Helpful? 0
  • +
  • -

#19 bwong23  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 29
  • Joined: 14-January 13

Re: Deck of Cards Applet how to shuffle and display

Posted 17 January 2013 - 01:34 AM

I'm stuck on how to get the first 10 cards, pass them to a paint method, or if I even can, and what to put for

Collections.shuffle ( images );//how to get first ten cards

public void paint ( Graphics g ) { //can i pass the first ten images to this?


g.drawImage (whatGoesHere, 10, 10, this);


Was This Post Helpful? 0
  • +
  • -

#20 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: Deck of Cards Applet how to shuffle and display

Posted 17 January 2013 - 07:13 AM

If your Card are in an ArrayList<Card> no need to shuffle just write a method that return a hand:

   ArrayList<Card> al;
   Random ran = new Random();

   Card[] getHand(int nb) {
       Card[] hand = new Card[nb];
       for(int i = 0; i < nb; ++i) {
          int index = ran.nextInt(al.size());
          hand[i] = al.remove(index);
       }
       return hand;
   }


Was This Post Helpful? 0
  • +
  • -

#21 bwong23  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 29
  • Joined: 14-January 13

Re: Deck of Cards Applet how to shuffle and display

Posted 17 January 2013 - 04:41 PM

pbl, would this go in the in the same method or different method as the one that generates the cards?
Was This Post Helpful? 0
  • +
  • -

#22 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10385
  • View blog
  • Posts: 38,434
  • Joined: 27-December 08

Re: Deck of Cards Applet how to shuffle and display

Posted 17 January 2013 - 06:41 PM

You can't define methods in other methods.
Was This Post Helpful? 1
  • +
  • -

#23 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: Deck of Cards Applet how to shuffle and display

Posted 17 January 2013 - 07:38 PM

I guess that macosxnerd101 answered
Your question didn't really make sense
Was This Post Helpful? 0
  • +
  • -

#24 bwong23  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 29
  • Joined: 14-January 13

Re: Deck of Cards Applet how to shuffle and display

Posted 17 January 2013 - 10:06 PM

View Postpbl, on 17 January 2013 - 07:13 AM, said:

If your Card are in an ArrayList<Card> no need to shuffle just write a method that return a hand:

   ArrayList<Card> al;
   Random ran = new Random();

   Card[] getHand(int nb) {
       Card[] hand = new Card[nb];
       for(int i = 0; i < nb; ++i) {
          int index = ran.nextInt(al.size());
          hand[i] = al.remove(index);
       }
       return hand;
   }



Sorry, I meant to say where would this go in the entire code?
Was This Post Helpful? -1
  • +
  • -

#25 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10385
  • View blog
  • Posts: 38,434
  • Joined: 27-December 08

Re: Deck of Cards Applet how to shuffle and display

Posted 17 January 2013 - 10:08 PM

I'd say at this point, this more than applies. Please start putting in some effort.

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

#26 bwong23  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 29
  • Joined: 14-January 13

Re: Deck of Cards Applet how to shuffle and display

Posted 17 January 2013 - 10:17 PM

Haha fair enough. If I don't turn this assignment in by tomorrow I will have to pay a $500 extension fee and thought professional programmers like you guys would have no problem figuring this out. I just need to get the course done and am going to retake this class in college, but I understand. Thanks for the help you have provided however.
Was This Post Helpful? 0
  • +
  • -

#27 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10385
  • View blog
  • Posts: 38,434
  • Joined: 27-December 08

Re: Deck of Cards Applet how to shuffle and display

Posted 17 January 2013 - 10:19 PM

It's not about us having a problem with the assignment. We're just not going to hold your hand and tell you the blow by blow of where to paste code. Programming is about thinking and problem solving. You're really not doing much of either.
Was This Post Helpful? 0
  • +
  • -

#28 bwong23  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 29
  • Joined: 14-January 13

Re: Deck of Cards Applet how to shuffle and display

Posted 17 January 2013 - 10:25 PM

Totally understandable. Partially because this online class is far from comprehensive and it was developed in 1999 so a lot of stuff you use is new to me. But thanks again for the help you have very generously provided me.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2