5 Replies - 361 Views - Last Post: 18 November 2012 - 10:19 AM Rate Topic: -----

#1 kgonzo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 15-November 12

Dealing Cards

Posted 15 November 2012 - 11:06 AM

I have a code that deals a deck of cards randomly. The problem I am having is getting the card to flip over so you can see the back. I don't know if I just have the wrong idea about how to do this or if it's just a simple error in my code.

 import java.awt.*;
import java.awt.event.*;
import java.util.Random;

public class DealCard extends javax.swing.JApplet implements ActionListener, MouseListener{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	Random gen = new Random();
	int card1 = 0;
	Image[] card = new Image[13];
	int num = 0;
	int card2 = 0;
	Image[] card3 = new Image[1];
	Button dealButton = new Button("Deal Card");	
	Button dealButton2 = new Button("Flip Card");

	/**
	 * 
	 */
	public void init(){
		setSize(400, 400);
		setLayout(null);
		String c = "123456789tjqk";
		String b = "1";
		int i = 0; 


		while (i < c.length()){
			Random cards = new Random();
			int r = cards.nextInt(4)+1;
			char q = c.charAt(i);
			if (r == 1)
			{
				card[i]= getImage(getCodeBase(), "h" + q + ".gif");
			}
			else if (r == 2)
			{
				card[i]= getImage(getCodeBase(), "d" + q + ".gif");
			}
			else if (r == 3)
			{
				card[i]= getImage(getCodeBase(), "s" + q + ".gif");
			}
			else if (r == 4)
			{
				card[i]= getImage(getCodeBase(), "c" + q + ".gif");
			}
			i++;
		}

		dealButton.setBounds(20, 200, 100, 20);
		add(dealButton); 
		dealButton.addActionListener(this);
		dealButton.setEnabled(true);
		dealButton.addMouseListener(this);

		setSize(400, 400);
		setLayout(null);

		while (i < b.length()){
			int bk = card3.length;
			if (bk == 1){
				card3[i]= getImage(getCodeBase(), "b1.gif");
			} i++;
		}
		dealButton2.setBounds(20,300,100,20);
		add(dealButton2);
		dealButton2.addActionListener(this);
		dealButton2.setEnabled(true);
		dealButton2.addMouseListener(this);
	}

	public void paint(Graphics g){
		super.paint(g);
		g.drawImage(card[card1], 50 , 50, 71, 96, this);
		g.drawImage(card3[card2], 50, 50, 71, 96, this);

	}
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == dealButton){
			card1 = gen.nextInt(13);
		} 
		repaint();
	}

	public void mouseClicked(MouseEvent e) {
		int x = e.getX();
		int y = e.getY();
		if (x > 50 && x < (50 + 71) && y > 50 && y < (50 +71)){
			card1 = gen.nextInt(13);
			repaint();
		}
		e.consume();
	}
	public void mouseEntered(MouseEvent arg0) {
	}
	public void mouseExited(MouseEvent arg0) {
	}
	public void mousePressed(MouseEvent arg0) {
	}
	public void mouseReleased(MouseEvent arg0) {
	}


}




Is This A Good Question/Topic? 0
  • +

Replies To: Dealing Cards

#2 pbl  Icon User is offline

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

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

Re: Dealing Cards

Posted 15 November 2012 - 11:37 AM

did you getImage() a unflipped card somewhere
I guess that for each card you will need an attributed flipped or not and draw accordingly
Was This Post Helpful? 0
  • +
  • -

#3 kgonzo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 15-November 12

Re: Dealing Cards

Posted 16 November 2012 - 08:15 AM

View Postpbl, on 15 November 2012 - 11:37 AM, said:

did you getImage() a unflipped card somewhere
I guess that for each card you will need an attributed flipped or not and draw accordingly



What exactly do you mean by that? And thanks for the help!
Was This Post Helpful? 0
  • +
  • -

#4 heinbau  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 22
  • Joined: 08-November 12

Re: Dealing Cards

Posted 16 November 2012 - 11:47 AM

I think what he's asking is if you have an image stored of a flipped card. And if you do then all you need is a method that flips the card which would replace the image with the one of a card back.

Does that make sense? I just had a root canal and the percoset is kicking my butt right now....

Let me know
Nick
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

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

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

Re: Dealing Cards

Posted 16 November 2012 - 12:42 PM

Actually you should have a class Card that contains an image, a drawing method and a static variable containing the fliffed card

class Card {
   private static Image flippedImg;

   private Image image;
   privalte boolean flipped;

   Card(String imageName, JApplet ap) {
       flipped = false;
       if(flippedImg == null)
           flippedImg = ap.getImage(getCodeBase(), "flipped.gif");  
       image = ap.getImage(getCodeBase(), imageName + ".gif");  
   }

   public void draw(Graphics g, x, y) {
      if(flipped)
        g.drawImage(fippedImg, ....
      else
        g,drawImage(image, ......


Was This Post Helpful? 1
  • +
  • -

#6 kgonzo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 15-November 12

Re: Dealing Cards

Posted 18 November 2012 - 10:19 AM

Ah! okay, Thank you so much!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1