Card game (again, or still). This one's got a GUI. Problem is, the draw and stick method need the total value that is made in start. So when call draw() or stick() from the actionPerformed method it needs a value or it won't compile (stick(int) in game1 cannot be applied to () ) (which makes sense)

how do i get round that?

And what sort of code do i use to draw the images when start or draw is clicked? I've tried ToolKit and getImage sort of stuff but they all need a graphics variable to draw it, which is in the paintComponent method i believe.. or something like that.

Code attached as it's long. Does anyone know what i'm asking or how to help? :)

Attachments
import javax.swing.*;
import java.awt.*;
import java.net.*;
import java.awt.event.*;


public class game extends JFrame implements ActionListener
{
 JButton twist;
 JButton stick;
 JButton start;
 JTextArea intro;

  public game()
  {

      super("Well well");
      setSize(300,300);
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      setVisible(true);

      Container contentArea = getContentPane();
      contentArea.setBackground(Color.white);
     
      BorderLayout layman = new BorderLayout();
      contentArea.setLayout(layman);

      twist = new JButton("Twist");
      stick = new JButton("Stick");
      start = new JButton("Start");


      twist.addActionListener(this);
    stick.addActionListener(this);
    start.addActionListener(this);

      JPanel str = new JPanel();
      str.add(start);
      contentArea.add(str, BorderLayout.NORTH);

      JPanel buts = new JPanel();
      buts.add(twist);
      buts.add(stick);

      contentArea.add(buts, BorderLayout.SOUTH);

      intro = new JTextArea("Label time", 3,20);

      contentArea.add(intro, BorderLayout.CENTER);


      setContentPane(contentArea);


   }

   public void actionPerformed(ActionEvent event)
   {

     if(event.getSource()==start) game1.start();
     if(event.getSource()==twist) game1.draw();
     if(event.getSource()==stick) game1.stick();
   
   }

   public static void main (String[] args) 

    {
  game eg = new game();
    }

}





class game1
{
static String path;

   public static void main (String[] args)
   {
       start();

   }
       
   public static void start()
   {

/*variables for cards*/
      int total;

//figure suit for card one and two
      char type[]={'S','D','H','C'};
      int t=(int) Math.ceil(Math.random()*3);
      int u=(int) Math.ceil(Math.random()*3);

      char suit=type[t];
      char suit2=type[u];

//figure card value (numberical).
      int set[]={1,2,3,4,5,6,7,8,9,10,11,12,13};

      int i=(int) Math.ceil(Math.random()*12);
      int j=(int) Math.ceil(Math.random()*12);

      int card=set[i];
      int card1=set[j];

      total = card+card1; //add two cards

//switch to find file path of cards

      switch(suit)
      {
          case 'S' : spade(i); break;
          case 'D' : diamond(i); break;
          case 'H' : heart(i); break;
          case 'C' : club(i); break;
      }

      switch(suit2)
      {
          case 'S' : spade(j); break;
          case 'D' : diamond(j); break;
          case 'H' : heart(j); break;
          case 'C' : club(j); break;
       }

//images of cards using the file path to be displayed... what do i use??


       eval(total); // go to eval to see what total is compared to other

   }



   public static void eval(int total)
   {

       if (total==21)
       {
          //set label to you win
       }

       else if(total > 21)
       {
          //set text you lose
       }

       else if (total < 21)
       {
          //set text twist or stick
       }

   }




  public static void draw(int total) //for when the player clicks twist pick a new card and show image
   {

/*letters of suit, plus random to choose which then variables to store suit*/ 
      char type[]={'S','D','H','C'};
      int t=(int) Math.ceil(Math.random()*3);

      char suit=type[t];

/*values of cards and random to choose one, then variables to store card value*/
      int set[]={1,2,3,4,5,6,7,8,9,10,11,12,13};

      int i=(int) Math.ceil(Math.random()*12);

      int card=set[i];

/*switch block to decide which image to show as in main*/

        switch(suit)
        {
            case 'S' : spade(i); break;
            case 'D' : diamond(i); break;
            case 'H' : heart(i); break;
            case 'C' : club(i); break;
        }


/*total variable from above now equals total + card three*/

     total+=card; 

//show total and image of card??

     eval(total); //go to eval to test total and see what text to show
   }



   public static void stick(int total) //when stick is clicked
   {	
      int other;

      other = (int) Math.ceil(Math.random()*22); //declare other as random number

/*if total is bigger than the other in the main function then say you won etc*/

      if(total > other)
      { 
         //set text you won! etc
      }


/*if other in main is bigger than total say you lose etc*/

      else if (other > total)
      {
	//set text you lose!
      }	
	
   }


//decks

   public static String spade(int p)
   {
      String set[]={"sa.gif","s2.gif","s3.gif","s4.gif","s5.gif","s6.gif","s7.gif","s8.gif","s9.gif","s10.gif","sj.gif","sq.gif","sk.gif"};
      path=set[p];
	
      return (path);
   }

   public static String diamond(int p)
   {
      String set[]={"da.gif","d2.gif","d3.gif","d4.gif","d5.gif","d6.gif","d7.gif","d8.gif","d9.gif","d10.gif","dj.gif","dq.gif","dk.gif"};
      path=set[p];
	
      return (path);
   }

   public static String heart(int p)
   {
      String set[]={"ha.gif","h2.gif","h3.gif","h4.gif","h5.gif","h6.gif","h7.gif","h8.gif","h9.gif","h10.gif","hj.gif","hq.gif","hk.gif"};
      path=set[p];
	
      return (path);
   }

   public static String club(int p)
   {
      String set[]={"ca.gif","c2.gif","c3.gif","c4.gif","c5.gif","c6.gif","c7.gif","c8.gif","c9.gif","c10.gif","cj.gif","cq.gif","ck.gif"};
      path=set[p];
	
      return (path);
   }

}

ok what format are you storing the images of the cards or are you drawing them using java ?

if you are loading them in, then you have two options. load them all at runtime of your GUI this way you wont have any wait time whilst playing the game but will obv take longer to load the app [you could achieve this by loading them into an array for example]. Or you can load them on the fly as you go along this will free up the load time at beginning.

this code will work for both.

import java.awt.Image;
import java.io.File;
import javax.imageio.ImageIO;


Image[] cards = new Image[52];
try {
cards[0] = ImageIO.read(new File("card.jpg"));
} catch (Exception e) {
System.out.println(e);
}

for it to work and load all you will need to add a loop.

showing the image in the GUI is the easy part. just simply create a JLabel wherever you wish thte image to be and use the following

JLabel.setIcon(new ImageIcon(cards[0]));

hope this helps if ive understood your question right

i dont understand what you mean by needing a value for the stick() etc. why do you need to run stick at the start ? cant you just say stick(-1) and run a check in stick to see if -1 was entered and tell it to do what you want i.e. bypass the method ? but maybe you can clarify what you want it to do ?

i dont understand what you mean by needing a value for the stick() etc. why do you need to run stick at the start ? cant you just say stick(-1) and run a check in stick to see if -1 was entered and tell it to do what you want i.e. bypass the method ? but maybe you can clarify what you want it to do ?

right, from the start, open application, click start button. Two cards are drawn and displayed (start() ). the total value of these are the compared (eval() ). If over 21 game ends. If not we continue.

the user then clicks either twist or stick buttons.

twist button draws and displays another card and adds that value to the previous total (draw() ). Thus it needs the total which was made in eval().

Stick button compares the total to the "opponents" total (a random number) and displays you lose/win etc (stick() ). Hence stick() needs the total value made in eval() (as does draw()).

I've done each method so it's called via actionPerformed. (so when the user clicks).

updated code attached. Problem now (as well as moving the total variable around) is i've got two of the arrays loading the images, one in start() one in draw() (thanks for the array code btw). I tried doing a seperate class with them in, and changing the coding in the switch box but ket saying "cannot find variable"

and i can't change the text back to black >:(

Attachments
import javax.swing.*;
import java.awt.*;
import java.net.*;
import java.awt.event.*;
import java.awt.Image;
import java.io.File;
import javax.imageio.ImageIO;

class game1
{


//declaring a new JLabel for the image icon (??)
    static JLabel label = new JLabel("Image here");

   public static void start()
   {

   Image[] spades = new Image[12];

    for(int i=0; i<12; i++)
      {
      try 
         {
            spades[0] = ImageIO.read(new File("card.jpg"));
         } 
     catch (Exception e) 
         {
            System.out.println(e);
         }
      }

    Image[] clubs = new Image[12];

    for(int i=0; i<12; i++)
      {
      try 
         {
            clubs[0] = ImageIO.read(new File("card.jpg"));
         } 
     catch (Exception e) 
         {
            System.out.println(e);
         }
      }

    Image[] hearts = new Image[12];

    for(int i=0; i<12; i++)
      {
      try 
         {
            hearts[0] = ImageIO.read(new File("card.jpg"));
         } 
     catch (Exception e) 
         {
            System.out.println(e);
         }
      }    

    Image[] diamonds= new Image[12];

    for(int i=0; i<12; i++)
      {
      try 
         {
            diamonds[0] = ImageIO.read(new File("card.jpg"));
         } 
     catch (Exception e) 
         {
            System.out.println(e);
         }
      }



/*variables for cards*/
      int total;

//figure suit for card one and two
      char type[]={'S','D','H','C'};
      int t=(int) Math.ceil(Math.random()*3);
      int u=(int) Math.ceil(Math.random()*3);

      char suit=type[t];
      char suit2=type[u];

//figure card value (numerical).
      int set[]={1,2,3,4,5,6,7,8,9,10,11,12,13};

      int i=(int) Math.ceil(Math.random()*12);
      int j=(int) Math.ceil(Math.random()*12);

      int card=set[i];
      int card1=set[j];

      total = card+card1; //add two cards

//switch to show image, depending on suit, using arrays above /\ /\



      switch(suit)
      {
          case 'S' : label.setIcon(new ImageIcon(spades[i]));; break;
          case 'D' : label.setIcon(new ImageIcon(diamonds[i]));; break;
          case 'H' : label.setIcon(new ImageIcon(hearts[i])); break;
          case 'C' : label.setIcon(new ImageIcon(clubs[i]));; break;
      }

      switch(suit2)
      {
          case 'S' : label.setIcon(new ImageIcon(spades[j]));; break;
          case 'D' : label.setIcon(new ImageIcon(diamonds[j]));; break;
          case 'H' : label.setIcon(new ImageIcon(hearts[j])); break;
          case 'C' : label.setIcon(new ImageIcon(clubs[j]));; break;
       }


       eval(total); // go to eval to see what total is compared to other

   }



   public static void eval(int total)
   {

       if (total==21)
       {
          //set label to you win
       }

       else if(total > 21)
       {
          //set text you lose
       }

       else if (total < 21)
       {
          //set text twist or stick
       }

   }




  public static void draw(int total) //for when the player clicks twist pick a new card and show image
   {
   Image[] spades = new Image[12];

    for(int i=0; i<12; i++)
      {
      try 
         {
            spades[0] = ImageIO.read(new File("card.jpg"));
         } 
     catch (Exception e) 
         {
            System.out.println(e);
         }
      }

    Image[] clubs = new Image[12];

    for(int i=0; i<12; i++)
      {
      try 
         {
            clubs[0] = ImageIO.read(new File("card.jpg"));
         } 
     catch (Exception e) 
         {
            System.out.println(e);
         }
      }

    Image[] hearts = new Image[12];

    for(int i=0; i<12; i++)
      {
      try 
         {
            hearts[0] = ImageIO.read(new File("card.jpg"));
         } 
     catch (Exception e) 
         {
            System.out.println(e);
         }
      }    

    Image[] diamonds= new Image[12];

    for(int i=0; i<12; i++)
      {
      try 
         {
            diamonds[0] = ImageIO.read(new File("card.jpg"));
         } 
     catch (Exception e) 
         {
            System.out.println(e);
         }
      }

/*letters of suit, plus random to choose which then variables to store suit*/ 
      char type[]={'S','D','H','C'};
      int t=(int) Math.ceil(Math.random()*3);

      char suit=type[t];

/*values of cards and random to choose one, then variables to store card value*/
      int set[]={1,2,3,4,5,6,7,8,9,10,11,12,13};

      int i=(int) Math.ceil(Math.random()*12);

      int card=set[i];

/*switch block to decide which image to show as in main*/

        switch(suit)
        {
          case 'S' : label.setIcon(new ImageIcon(spades[i]));; break;
          case 'D' : label.setIcon(new ImageIcon(diamonds[i]));; break;
          case 'H' : label.setIcon(new ImageIcon(hearts[i])); break;
          case 'C' : label.setIcon(new ImageIcon(clubs[i]));; break;
        }


/*total variable from above now equals total + card three*/

     total+=card; 

//show total and image of card??

     eval(total); //go to eval to test total and see what text to show
   }



   public static void stick(int total) //when stick is clicked
   {	
      int other;

      other = (int) Math.ceil(Math.random()*22); //declare other as random number

/*if total is bigger than the other in the main function then say you won etc*/

      if(total > other)
      { 
         //set text you won! etc
      }


/*if other in main is bigger than total say you lose etc*/

      else if (other > total)
      {
	//set text you lose!
      }	
	
   }


}




public class game extends JFrame implements ActionListener
{
 JButton twist;
 JButton stick;
 JButton start;
 JTextArea intro;

  public game()
  {

      super("Well well");
      setSize(300,300);
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      setVisible(true);

      Container contentArea = getContentPane();
      contentArea.setBackground(Color.white);
     
      BorderLayout layman = new BorderLayout();
     // GridBagConstraints pos = new GridBagConstraints();
      contentArea.setLayout(layman);

      twist = new JButton("Twist");
      stick = new JButton("Stick");
      start = new JButton("Start");


      twist.addActionListener(this);
    stick.addActionListener(this);
    start.addActionListener(this);

      JPanel str = new JPanel();
      str.add(start);
 //     str.setBounds(145,5, 175,25);
  //    pos.gridx = 2; pos.gridy=0;
      contentArea.add(str, BorderLayout.NORTH);

      JPanel buts = new JPanel();
      buts.add(twist);
      buts.add(stick);
   //   pos.gridx = 2; pos.gridy=6;

      contentArea.add(buts, BorderLayout.SOUTH);

    //  twist.setBounds(130,100,165,120);
    //  stick.setBounds(180,100,215,120);

      intro = new JTextArea("Label time", 3,20);
   //   intro.setBounds(130,55,180,100);
   //   pos.gridx = 1; pos.gridy=2;

      contentArea.add(intro, BorderLayout.CENTER);


      setContentPane(contentArea);


   }

   public void actionPerformed(ActionEvent event)
   {

     if(event.getSource()==start) game1.start();
     if(event.getSource()==twist) game1.draw(total);
     if(event.getSource()==stick) game1.stick(total);
   
   }

   public static void main (String[] args) 

    {
  game eg = new game();
    }

}
This article has been dead for over six months. Start a new discussion instead.