Blackjack Problem

Converting Applet to JFrame without using image

Page 1 of 1

4 Replies - 1253 Views - Last Post: 12 December 2009 - 02:23 PM Rate Topic: -----

#1 inrest  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 10-December 09

Blackjack Problem

Post icon  Posted 10 December 2009 - 01:10 PM

Hello,

I am begginer in java programming. I have a code which i get from a web of Black jack game. I am practicing to play with the code. source code from web is for applet and it has image too. Now I am trying to convert it into JFrame Application without displaying image. I want to use string value instead of Image.

Here is the code


import java.awt.*;
import java.awt.event.*;
import java.applet.*;



public class Blackjack extends Applet implements ActionListener,Runnable{

	private Deck deck;	//The deck...
	
	private Hand player_hand;//player hand
	private Hand dealer_hand;//dealer hand.

	private Button bHit=new Button("Hit");
	private Button bStay=new Button("Stay");
	private Button bRestart=new Button("New Game");


	//lets do some double buffering.
	Image offIm=null;
	Graphics offGraphics=null;
	Dimension offDimension=null;

	Image[] card_images = new Image[52];
	private boolean cards_loaded = false;
	private int current_card_loading = 0;

	String message;					//print a message...

	int width;		//The width of the applet
	int height;		//The height of the applet

	int card_width = -1;
	int card_padding = -1;	//How much should we pad the cards by..?
	
	private int games=-1;	//How many games have been played?
 
	public void init() {

		
		Thread card_loader = new Thread(this);
		card_loader.start();

	}
	
	public void newGame(){
		bHit.setEnabled(true);
		bStay.setEnabled(false);

		player_hand = new Hand();	//Create new hands...
		dealer_hand = new Hand();

		//shuffle the deck.
		deck.shuffleCards();

		games++;
	}
 
	public void hit(){

		bStay.setEnabled(true);

		player_hand.addCard(deck.dealCard());
		if(player_hand.getValue() > 21){
			message = "You loose! (score:"+player_hand.getValue()+")";
			//disable the hit and stay buttons...
			bHit.setEnabled(false);
			bStay.setEnabled(false);
			return;
		}
		message = "Score: "+player_hand.getValue();	
	}

	public void stay(){

		while(dealer_hand.getValue() < 17){
			dealer_hand.addCard(deck.dealCard());
		}
		if(dealer_hand.getValue() <= 21 && player_hand.getValue() < dealer_hand.getValue())
		{
			message = "You loose! (" + player_hand.getValue()+
					" - "+dealer_hand.getValue()+")";

		}else if (player_hand.getValue() == dealer_hand.getValue())
		{
			message = "You draw! (" + player_hand.getValue()+")";

		}else {

			message = "You win! (" + player_hand.getValue()+
					" - "+dealer_hand.getValue()+")";

		}

		bHit.setEnabled(false);
		bStay.setEnabled(false);

	}


	public void actionPerformed(ActionEvent e) {
 

		if(e.getSource() == bRestart)
		{
			newGame();
		}else if (e.getSource() == bHit)
		{
			hit();
		}else if (e.getSource() == bStay)
		{
			stay();
		}
		   
		repaint();
	}
  
	public void paint(Graphics g) {
		update(g);
	}
  
	public void update(Graphics g) {
	
		//lets sord out double buffering
		if(offGraphics==null){
			offIm=createImage(getSize().width,getSize().height);
			offGraphics=offIm.getGraphics();
		}

		if(!cards_loaded){
			//display a message saying we're loading the cards...
			offGraphics.setFont(new Font("Arial",Font.PLAIN,14));
			offGraphics.setColor(new Color(171,205,239));
			offGraphics.fillRect(0,0,getSize().width,getSize().height);
			offGraphics.setColor(Color.black);
			offGraphics.drawString("Loading cards... ",5,20);
			offGraphics.drawRect(15,40, 102 ,10);
			offGraphics.drawRect(13,38, 106 ,14);
			offGraphics.setColor(new Color(171,205,239));
			offGraphics.fillRect(0,0,150,35);
			offGraphics.setColor(Color.black);
			offGraphics.fillRect(15,40, (current_card_loading)*2 ,10);
			offGraphics.drawString("Loading card: "+current_card_loading+1,15,20);


		}else{
			Image currentCard;

			while(card_width == -1)
			{
				card_width = deck.getCard(0).getImage().getWidth(this);
			}
			
			if(card_padding == -1)
			{
				card_padding = (width - (card_width * 2) - 4) / 4;
			}

			//clear the background...
			offGraphics.setColor(Color.blue);
			offGraphics.fillRect(0,0,width,height);
			offGraphics.setColor(Color.black);
			offGraphics.fillRect(1,1,width-2,height-2);
			offGraphics.setColor(Color.white);
			offGraphics.drawString("PLAYER:",card_padding,40);
			offGraphics.drawString("DEALER:",(width/2) + card_padding,40);
			offGraphics.drawString(message,5,height - 20);
			if(games > 0)
			{
				offGraphics.drawString(games + " game(s) played...",5,height - 10);
			}
			//Draw the players hand...

			for(int i=0;i<player_hand.getCardCount();i++){
				currentCard = player_hand.getCards()[i].getImage();
				offGraphics.drawImage(currentCard, card_padding, 70+(20*(i-1)), this);
			}
			//now draw the dealers hand...
			for(int i=0;i<dealer_hand.getCardCount();i++){
				currentCard = dealer_hand.getCards()[i].getImage();
				offGraphics.drawImage(currentCard, (width/2 ) + card_padding, 70+(20*(i-1)), this);
			}

		}
		//draw buffered image.
		g.drawImage(offIm,0,0,this);
 
	}


   public void run(){

		MediaTracker t=new MediaTracker(this);
		System.out.println("Applet getting cards...");

		for(current_card_loading=0; current_card_loading < 52; current_card_loading++){

		  //try{
			card_images[current_card_loading] = getImage(getCodeBase(),
												"cards/" + (current_card_loading+1) + ".gif");

			if(card_images[current_card_loading] == null){
				System.out.println("Null card... ["+current_card_loading+"]");
			}else{
				t.addImage(card_images[current_card_loading],0);
			}
		
			try{
				t.waitForID(0);
			}catch(InterruptedException e){
				System.err.println("Interrupted waiting for images..>");
			}
			//lets show our new status.
			repaint();
		}

		//create the deck object now
		deck = new Deck(this,card_images);	//Create a new deck object.

		card_width = deck.getCard(0).getImage().getWidth(this);

		bHit.addActionListener(this);
		bStay.addActionListener(this);
		bRestart.addActionListener(this);

		bHit.setEnabled(false);
		bStay.setEnabled(false);

		width  = getSize().width;
		height = getSize().height;


		this.add(bHit);
		this.add(bStay);
		this.add(bRestart); 

		player_hand = new Hand();	//Create the hands...
		dealer_hand = new Hand();

		//make sure that the buttons appear properly.
		this.validate();

		message = "";
		cards_loaded = true;
		System.out.println("End of thread...");
		repaint();
	}



}  



class Card {
  Image image;
  int value;
  
  /* Constructor */
  public Card(Image image, int value) {
	this.image = image;
	this.value = value;
  }
  
  /* Return the image associated with the card */
  public Image getImage() {
	return image;
  }
  
  /* Return the current value of the card */
  public int getValue() {
	return value;
  }
  
  /* Change the value of the card */
  public void setValue(int value) {
	this.value = value;
  }
} 
   

class Deck {
	Card[] cards = new Card[52];
	int currCard;

	public Deck(Blackjack myApplet,Image[] card_images) {
		// These are the (initial) values of all the cards 
		int[] values = {11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10,
			11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10,
			11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10,
			11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10};

		// Make all the cards, getting the images individually
		for (int i=0; i<52; i++) {
//			Image image = myApplet.getImage(myApplet.getCodeBase(), "cards/" + (i+1) + ".gif");
			cards[i] = new Card(card_images[i], values[i]);
		}

		// Shuffle the cards.
		shuffleCards();
	}

	public void shuffleCards(){

		Card temp;
		//loop through each card in the deck and swap it with some random card.
		for (int i=0; i<52; i++) {
			int rand = (int)(Math.random()*52);
			temp = cards[51-i];//take the current card...
			cards[51-i] = cards[rand];//and swap it with some random card...
			cards[rand] = temp;
		}

		//now, reset the currentCard to deal...
		currCard = 0;
	}

  
	public Card dealCard() {

		Card card = cards[currCard];
		currCard++;
		return card;
	}

	public Card getCard(int i) {
		return cards[i];
	}

} 

class Hand {
	private Card cards[] = new Card[10];
	private int count;
	private int value;

	
	public void Hand(){
		count=0;
		value=0;
	}
	
	public Card[] getCards(){

		return cards;

	}

	public int getCardCount(){
		return count;
	}

	public void addCard(Card c) {
		cards[count] = c;
		value += cards[count].getValue();

		if(c.getValue() == 11 && value > 21)
		{	//got and ace, so decrement the value of the hand by 10, if it 
			//would have been above 21...
			value-=10;
		}
		count++;
	}
	  
	public int getValue() {
		return value;
	}
  
	public boolean ace() {
		int aceIndex = 0;
		boolean result = false;

		for (int i=0; i<count; i++) {
			if (cards[i].getValue() == 11) {
				value-=10;
				return true;
			}
		}
		return false;
	}  

}

	




Looking for suggetion. Thank you

Is This A Good Question/Topic? 0
  • +

Replies To: Blackjack Problem

#2 erik.price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 485
  • View blog
  • Posts: 2,690
  • Joined: 18-December 08

Re: Blackjack Problem

Posted 10 December 2009 - 02:08 PM

We WILL NOT do your homework for you.


[rules][/rules]
Was This Post Helpful? 0
  • +
  • -

#3 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: Blackjack Problem

Posted 10 December 2009 - 08:14 PM

View Postinrest, on 10 Dec, 2009 - 12:10 PM, said:

Hello,

I am begginer in java programming. I have a code which i get from a web of Black jack game. I am practicing to play with the code. source code from web is for applet and it has image too. Now I am trying to convert it into JFrame Application without displaying image. I want to use string value instead of Image.

If you don't want to use image why do you convert it to a JFrame Application ?
Just make it a console Application and forget all the GUI stuff
Was This Post Helpful? 0
  • +
  • -

#4 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2553
  • View blog
  • Posts: 10,649
  • Joined: 20-September 08

Re: Blackjack Problem

Posted 12 December 2009 - 10:32 AM

An applet is a Panel subclass, to you can add it directly to a Frame instance. Call init() explicitly
Was This Post Helpful? 0
  • +
  • -

#5 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: Blackjack Problem

Posted 12 December 2009 - 02:23 PM

View Postg00se, on 12 Dec, 2009 - 09:32 AM, said:

An applet is a Panel subclass, to you can add it directly to a Frame instance. Call init() explicitly

like in the constructor that you will add

class MyApplet extends JApplet {

	public MyApplet() {	   // new constructor 
		init();
	}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1