Cards Applet

Page 1 of 1

4 Replies - 344 Views - Last Post: 12 September 2012 - 08:36 AM Rate Topic: -----

#1 hellothere58  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 15-August 12

Cards Applet

Posted 11 September 2012 - 09:32 PM

I am trying to complete this assignment, but my code won't work. Please help, thanks!

use an applet to display images of playing cards. The applet should load a deck of 52 playing card images from the "images" folder that you downloaded. The applet should shuffle the deck (use a random number generator) and display the first 10 cards of the shuffled deck. Display the cards in two rows of five cards each.

here is my code:
import java.applet.Applet;
import java.applet.AudioClip;
import java.net.URL;
import java.awt.*;
import javax.swing.*;

public class ShuffleCards extends JApplet
{
	Image card1, card2, card3, card4, card5, card6, card7, card8, card9, card10, randCard, flippedCard;
	int cardChoice; 

	public void init()
	{
		ImageIcon flipped = new ImageIcon( "Users/chrischambers/Pictures/cards_gif/b2pv.gif" );
		flippedCard = flipped.getImage();
		for (int count = 0; count < 10; count++)
		{
			cardChoice = newRandom();
			randCard = cardSet( cardChoice );
			switch (count)
			{
				case 0:
					card1 = randCard;
					break;
				case 1:
					card2 = randCard;
					break;
				case 2:
					card3 = randCard;
					break;
				case 3:
					card4 = randCard;
					break;
				case 4:
					card5 = randCard;
					break;
				case 5:
					card6 = randCard;
					break;
				case 6:
					card7 = randCard;
					break;
				case 7:
					card8 = randCard;
					break;
				case 8:
					card9 = randCard;
					break;
				case 9:
					card10 = randCard;
					break;
				default:
					break;
			}
		}
	}

	public void paint( Graphics c )
	{
		Color cardTable = new Color(0, 127, 0);

		setBackground( cardTable );

		Font style = new Font( "Blackmoor LET", Font.BOLD, 36 );
		c.setFont( style );
		c.setColor( Color.red );
		c.drawString( "Prepare to be amazed!", 20, 50 );

		double wait = 1000000000.00;

		delay(wait);

		c.clearRect( 20, 50, 250, 100 );
		setBackground( cardTable );

		c.drawString( "See this deck of cards?", 20, 50 );
		for (int count = 0; count < 52; count++);
		{
			int x = 50;
			int y = 100;
			c.drawImage(flippedCard, x, y, this);
			x++;
			y++;
		}

		delay(wait);

		c.clearRect( 20, 50, 250, 200 );
		setBackground( cardTable );

		c.drawString( "I, a simple applet, will now shuffle these cards and show you the top 10 from the deck!!!", 20, 50 );

		delay(wait);

		c.clearRect( 20, 50, 300, 200 );
		setBackground( cardTable );

		c.drawImage(card1, 10, 10, this);
		delay(wait/2);
		c.drawImage(card2, 85, 10, this);
		delay(wait/2);
		c.drawImage(card3, 160, 10, this);
		delay(wait/2);
		c.drawImage(card4, 235, 10, this);
		delay(wait/2);
		c.drawImage(card5, 310, 10, this);
		delay(wait/2);
		c.drawImage(card6, 10, 110, this);
		delay(wait/2);
		c.drawImage(card7, 85, 110, this);
		delay(wait/2);
		c.drawImage(card8, 160, 110, this);
		delay(wait/2);
		c.drawImage(card9, 235, 110, this);
		delay(wait/2);
		c.drawImage(card10, 310, 110, this);
		delay(wait/2);

		c.drawString( "TA-DA!!!!", 20, 130);
		URL tadaURL=new URL("http://bluejay.cty.jhu.edu/file.php/569/Course_Downloads/Sounds/tada.wav" );
		//URL tadaURL = getClass().getResource( "http://bluejay.cty.jhu.edu/file.php/569/Course_Downloads/Sounds/tada.wav" );
		AudioClip fanfare = Applet.newAudioClip( tadaURL );
		fanfare.play();
	}

	public static int newRandom()
	{
		boolean inRange = false;
		double decimal = Math.random();
		double random = 100 * decimal;
		int number = (int) random;

		do
		{
			if (number < 52)
				inRange = true;
			else
			{
				decimal = Math.random();
				random = 100 * decimal;
				number = (int) random;
			}
		}
		while (inRange == false);

		return number;
	}

	public static Image cardSet (int card)
	{
		Image newCard;
		ImageIcon cardImage;
	
		switch (card)
		{
			case 0:
				cardImage = new ImageIcon( "Users/chrischambers/Pictures/cards_gif/c1.gif" );
				newCard = cardImage.getImage();
				break;
			case 1:
				cardImage = new ImageIcon( "Users/chrischambers/Pictures/cards_gif/c2.gif" );
				newCard = cardImage.getImage();
				break;
			case 2:
				cardImage = new ImageIcon( "Users/chrischambers/Pictures/cards_gif/c3.gif" );
				newCard = cardImage.getImage();
				break;
			case 3:
				cardImage = new ImageIcon( "Users/chrischambers/Pictures/cards_gif/c4.gif" );
				newCard = cardImage.getImage();
				break;
			case 4:
				cardImage = new ImageIcon( "Users/chrischambers/Pictures/cards_gif/c5.gif" );
				newCard = cardImage.getImage();
				break;
			case 5:
				cardImage = new ImageIcon( "Users/chrischambers/Pictures/cards_gif/c6.gif" );
				newCard = cardImage.getImage();
				break;
			case 6:
				cardImage = new ImageIcon( "Users/chrischambers/Pictures/cards_gif/c7.gif" );
				newCard = cardImage.getImage();
				break;
			case 7:
				cardImage = new ImageIcon( "Users/chrischambers/Pictures/cards_gif/c8.gif" );
				newCard = cardImage.getImage();
				break;
			case 8:
				cardImage = new ImageIcon( "Users/chrischambers/Pictures/cards_gif/c9.gif" );
				newCard = cardImage.getImage();
				break;
			case 9:
				cardImage = new ImageIcon( "Users/chrischambers/Pictures/cards_gif/c10.gif" );
				newCard = cardImage.getImage();
				break;
			case 10:
				cardImage = new ImageIcon( "Users/chrischambers/Pictures/cards_gif/cj.gif" );
				newCard = cardImage.getImage();
				break;
			case 11:
				cardImage = new ImageIcon( "Users/chrischambers/Pictures/cards_gif/cq.gif" );
				newCard = cardImage.getImage();
				break;
			case 12:
				cardImage = new ImageIcon( "Users/chrischambers/Pictures/cards_gif/ck.gif" );
				newCard = cardImage.getImage();
				break;
			case 13:
				cardImage = new ImageIcon( "Users/chrischambers/Pictures/cards_gif/d1.gif" );
				newCard = cardImage.getImage();
				break;
			case 14:
				cardImage = new ImageIcon( "Users/chrischambers/Pictures/cards_gif/d2.gif" );
				newCard = cardImage.getImage();
				break;
			case 15:
				cardImage = new ImageIcon( "Users/chrischambers/Pictures/cards_gif/d3.gif" );
				newCard = cardImage.getImage();
				break;
			case 16:
				cardImage = new ImageIcon( "Users/chrischambers/Pictures/cards_gif/d4.gif" );
				newCard = cardImage.getImage();
				break;
			case 17:
				cardImage = new ImageIcon( "Users/chrischambers/Pictures/cards_gif/d5.gif" );
				newCard = cardImage.getImage();
				break;
			case 18:	
				cardImage = new ImageIcon( "Users/chrischambers/Pictures/cards_gif/d6.gif" );
				newCard = cardImage.getImage();
				break;
			case 19:
				cardImage = new ImageIcon( "Users/chrischambers/Pictures/cards_gif/d7.gif" );
				newCard = cardImage.getImage();
				break;
			case 20:
				cardImage = new ImageIcon( "Users/chrischambers/Pictures/cards_gif/d8.gif" );
				newCard = cardImage.getImage();
				break;
			case 21:
				cardImage = new ImageIcon( "Users/chrischambers/Pictures/cards_gif/d9.gif" );
				newCard = cardImage.getImage();
				break;
			case 22:
				cardImage = new ImageIcon( "Users/chrischambers/Pictures/cards_gif/d10.gif" );
				newCard = cardImage.getImage();
				break;	
			case 23:
				cardImage = new ImageIcon( "Users/chrischambers/Pictures/cards_gif/dj.gif" );
				newCard = cardImage.getImage();
				break;
			case 24:
				cardImage = new ImageIcon( "Users/chrischambers/Pictures/cards_gif/dq.gif" );
				newCard = cardImage.getImage();
				break;
			case 25:
				cardImage = new ImageIcon( "Users/chrischambers/Pictures/cards_gif/dk.gif" );
				newCard = cardImage.getImage();
				break;
			case 26:
				cardImage = new ImageIcon( "Users/chrischambers/Pictures/cards_gif/s1.gif" );
				newCard = cardImage.getImage();
				break;
			case 27:
				cardImage = new ImageIcon( "Users/chrischambers/Pictures/cards_gif/s2.gif" );
				newCard = cardImage.getImage();
				break;
			case 28:
				cardImage = new ImageIcon( "Users/chrischambers/Pictures/cards_gif/s3.gif" );
				newCard = cardImage.getImage();
				break;
			case 29:
				cardImage = new ImageIcon( "Users/chrischambers/Pictures/cards_gif/s4.gif" );
				newCard = cardImage.getImage();
				break;
			case 30:
				cardImage = new ImageIcon( "Users/chrischambers/Pictures/cards_gif/s5.gif" );
				newCard = cardImage.getImage();
				break;
			case 31:
				cardImage = new ImageIcon( "Users/chrischambers/Pictures/cards_gif/s6.gif" );
				newCard = cardImage.getImage();
				break;
			case 32:
				cardImage = new ImageIcon( "Users/chrischambers/Pictures/cards_gif/s7.gif" );
				newCard = cardImage.getImage();
				break;
			case 33:
				cardImage = new ImageIcon( "Users/chrischambers/Pictures/cards_gif/s8.gif" );
				newCard = cardImage.getImage();
				break;
			case 34:
				cardImage = new ImageIcon( "Users/chrischambers/Pictures/cards_gif/s9.gif" );
				newCard = cardImage.getImage();
				break;
			case 35:
				cardImage = new ImageIcon( "Users/chrischambers/Pictures/cards_gif/s10.gif" );
				newCard = cardImage.getImage();
				break;
			case 36:
				cardImage = new ImageIcon( "Users/chrischambers/Pictures/cards_gif/sj.gif" );
				newCard = cardImage.getImage();
				break;
			case 37:	
				cardImage = new ImageIcon( "Users/chrischambers/Pictures/cards_gif/sq.gif" );
				newCard = cardImage.getImage();
				break;
			case 38:
				cardImage = new ImageIcon( "Users/chrischambers/Pictures/cards_gif/sk.gif" );
				newCard = cardImage.getImage();
				break;
			case 39:
				cardImage = new ImageIcon( "Users/chrischambers/Pictures/cards_gif/h1.gif" );
				newCard = cardImage.getImage();
				break;
			case 40:
				cardImage = new ImageIcon( "Users/chrischambers/Pictures/cards_gif/h2.gif" );
				newCard = cardImage.getImage();
				break;
			case 41:
				cardImage = new ImageIcon( "Users/chrischambers/Pictures/cards_gif/h3.gif" );
				newCard = cardImage.getImage();
				break;
			case 42:
				cardImage = new ImageIcon( "Users/chrischambers/Pictures/cards_gif/h4.gif" );
				newCard = cardImage.getImage();
				break;
			case 43:
				cardImage = new ImageIcon( "Users/chrischambers/Pictures/cards_gif/h5.gif" );
				newCard = cardImage.getImage();
				break;
			case 44:
				cardImage = new ImageIcon( "Users/chrischambers/Pictures/cards_gif/h6.gif" );
				newCard = cardImage.getImage();
				break;
			case 45:
				cardImage = new ImageIcon( "Users/chrischambers/Pictures/cards_gif/h7.gif" );
				newCard = cardImage.getImage();
				break;
			case 46:
				cardImage = new ImageIcon( "Users/chrischambers/Pictures/cards_gif/h8.gif" );
				newCard = cardImage.getImage();
				break;
			case 47:
				cardImage = new ImageIcon( "Users/chrischambers/Pictures/cards_gif/h9.gif" );
				newCard = cardImage.getImage();
				break;
			case 48:
				cardImage = new ImageIcon( "Users/chrischambers/Pictures/cards_gif/h10.gif" );
				newCard = cardImage.getImage();
				break;
			case 49:
				cardImage = new ImageIcon( "Users/chrischambers/Pictures/cards_gif/hj.gif" );
				newCard = cardImage.getImage();
				break;
			case 50:
				cardImage = new ImageIcon( "Users/chrischambers/Pictures/cards_gif/hq.gif" );
				newCard = cardImage.getImage();
				break;
			case 51:
				cardImage = new ImageIcon( "Users/chrischambers/Pictures/cards_gif/hk.gif" );
				newCard = cardImage.getImage();
				break;
			default:
				cardImage = new ImageIcon( "Users/chrischambers/Pictures/cards_gif/jr.gif" );
				newCard = cardImage.getImage();
				break;
		}
		return newCard;
	}

	public void delay(double howLong)
	{
		for( int i = 0; i <= howLong; i++ );
	}
}




Is This A Good Question/Topic? 0
  • +

Replies To: Cards Applet

#2 Cuzzie  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 72
  • View blog
  • Posts: 341
  • Joined: 16-July 10

Re: Cards Applet

Posted 11 September 2012 - 11:57 PM

Quote

but my code won't work.


Won't work in what way? Let us know what error you're getting.
Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

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

Reputation: 8347
  • View blog
  • Posts: 31,913
  • Joined: 06-March 08

Re: Cards Applet

Posted 12 September 2012 - 05:12 AM

Learn to use arrays before trying to code such a thing
That will reduce your code by at least half


public class ShuffleCards extends JApplet
{
	Image card[], randCard, flippedCard;
	int cardChoice; 

	public void init()
	{

		card = new Image[10];
		ImageIcon flipped = new ImageIcon( "Users/chrischambers/Pictures/cards_gif/b2pv.gif" );
		flippedCard = flipped.getImage();
		for (int count = 0; count < 10; count++)
		{
			cardChoice = newRandom();
			randCard = cardSet( cardChoice );
			card[i] = randCard;
		}
	}




Also, sleeping in the paint() method is completly useless. When paint() is executed, it draws offscreen in memory. It is only when paint() terminates and the drawing is finish that the hardware (the screen) is refreshed with that drawing in memory. If it wasn't the case, drawing something over something else would make the screen flickers.
Was This Post Helpful? 1
  • +
  • -

#4 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5937
  • View blog
  • Posts: 12,862
  • Joined: 16-October 07

Re: Cards Applet

Posted 12 September 2012 - 06:33 AM

I don't see that you've added anything since posting this under another login...
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

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

Reputation: 8347
  • View blog
  • Posts: 31,913
  • Joined: 06-March 08

Re: Cards Applet

Posted 12 September 2012 - 08:36 AM

Duplicated thread closed
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1