1 Replies - 5790 Views - Last Post: 09 February 2011 - 05:26 PM Rate Topic: -----

#1 thisguy34  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 29-November 10

Dispaying deck of cards

Posted 09 February 2011 - 03:58 PM

Hello, i was wondering if anyone out there could help men, im new to java, and im getting the concept of how to make the GUI and all that, but im having trouble with this pretty basic problem. I need to display the deck of cards from the deck

but without having to drag the cards or anything, I have to make the function work from the deck class that i have created, and i just can't seem to get this going, below is the code ive come up with so far, any help would be greatly appreciated;

public class Deck
{
	private Card [] cards;

	//set up the standard (or not) 52 card deck
	Deck()
	{
		cards = new Card[52];

		//create each of the 52 cards, add them to deck
		for(int i=1; i<=13; i++)
		{
			Card a = new Card(i, "Spade");
			Card b = new Card(i, "Heart");
			Card c = new Card(i, "Club");
			Card d = new Card(i, "Diamond");

			add(a);
			add(B)/>;
			add(c);
			add(d);

		}


	}






	//Create a specialized deck!
	//Due at midnight Friday if you didn't finish
	//in lab.
	Deck(int startNum, int endNum, String [] suits)
	{
		int cap = (endNum-startNum+1)*suits.length;
		cards = new Card[cap];

		for(int i=startNum; i<=endNum; i++)
		{
			for(int j=0; j<suits.length; j++)
			{
				add( new Card(i, suits[j]) );
			}
		}
	}


	//specify how a deck should appear when interpretted as
	//a string
	public String toString()
	{
		String output = "";

		for(int i=0; i< cards.length; i++)
		{
			output += cards[i];
			output += "\n";
		}

		return output;
	}

	public void add(Card c)
	{
		for(int i=0; i<cards.length; i++)
		{
			if( cards[i] == null )
			{
				cards[i] = c;
				i= cards.length; //break out of loop
			}
		}
	}


}

import javax.swing.*;
import java.awt.*;

public class CardPanel extends JPanel
{
	Card testcard;
	Card test;

		CardPanel()
		{
			testcard = new Card(7, "Heart");
			test = new Card(7, "Spades");
		}

		public void paintComponent(Graphics g)
		{
			//draw felt background
			g.setColor(Color.ORANGE);
			g.fillRect(0,0, getWidth(), getHeight());


		}


}

import javax.swing.*;

public class Game
{
	public static void main(String [] args)
	{
		//step 1: create a frame
		JFrame frame = new JFrame("Card Game");

		//step 2: create a panel, put panel in frame
		CardPanel panel = new CardPanel();
		frame.getContentPane().add(panel);

		//step 3: set frame size, make visible
		frame.setSize(500, 500);
		frame.setVisible(true);
	}
}

import javax.swing.*;
import java.awt.*;
public class Card
{
	int number;
	String suit;
	int x;
	int y;


	ImageIcon picture;


	Card()
	{
		number = 0;
		suit = "Joker";

		picture = new ImageIcon("cards/j.gif");
	}

	Card(int num, String s)
	{
		this();
		number = num;
		suit = s;

		String imageName = figureOutPicName();
		picture = new ImageIcon(imageName);

	}
	String figureOutPicName()
	{
		String output;
		output = "cards/";
		if (number > 1 && number < 10)
		{
			output += number;
		}
		else if( number == 1)
		{
			output += "a";
		}
		else if( number == 11)
		{
			output += "j";
		}
		else if( number == 12)
		{
			output += "q";
		}
		else if( number == 13)
		{
			output += "k";
		}
		else
		{
			output += "wtf";
		}

		output += suit.substring(0,1).toLowerCase();

		output += ".gif";

		return output;

	}

	public void draw(Graphics g, Component comp)
	{
		g.drawImage(picture.getImage(), x, y, comp);
	}
	public String toString()
	{
		return number + " of " + suit + "'s";
	}

}


Is This A Good Question/Topic? 0
  • +

Replies To: Dispaying deck of cards

#2 pbl  Icon User is offline

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

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Dispaying deck of cards

Posted 09 February 2011 - 05:26 PM

That's an original way of breaking from a loop :)
i= cards.length; //break out of loop 


you can simply do
if( cards[i] == null )  
{  
   cards[i] = c; 
   break;
}


For displaying your cards, better to use JLabels into which you set as icon images of cards
I have some here if you want
http://www.pblinc.ca/poker.html
just click on cards.zip

but should also be easy to Google for others
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1