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? :)

Recommended Answers

All 3 Replies

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 >:(

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.