943,940 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 3061
  • Java RSS
May 13th, 2007
0

passing values and displaying images in a GUI

Expand Post »
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?
Attached Files
File Type: java game.java (5.5 KB, 37 views)
Similar Threads
Reputation Points: 273
Solved Threads: 8
Master Poster
hbk619 is offline Offline
733 posts
since Oct 2006
May 14th, 2007
0

Re: passing values and displaying images in a GUI

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.

Java Syntax (Toggle Plain Text)
  1. import java.awt.Image;
  2. import java.io.File;
  3. import javax.imageio.ImageIO;
  4.  
  5.  
  6. Image[] cards = new Image[52];
  7. try {
  8. cards[0] = ImageIO.read(new File("card.jpg"));
  9. } catch (Exception e) {
  10. System.out.println(e);
  11. }

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

Java Syntax (Toggle Plain Text)
  1. JLabel.setIcon(new ImageIcon(cards[0]));

hope this helps if ive understood your question right
Reputation Points: 66
Solved Threads: 56
Posting Pro in Training
Fungus1487 is offline Offline
459 posts
since Apr 2007
May 14th, 2007
0

Re: passing values and displaying images in a GUI

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 ?
Reputation Points: 66
Solved Threads: 56
Posting Pro in Training
Fungus1487 is offline Offline
459 posts
since Apr 2007
May 16th, 2007
0

Re: passing values and displaying images in a GUI

Click to Expand / Collapse  Quote originally posted by Fungus1487 ...
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 >
Attached Files
File Type: java game.java (7.4 KB, 30 views)
Last edited by hbk619; May 16th, 2007 at 4:56 pm.
Reputation Points: 273
Solved Threads: 8
Master Poster
hbk619 is offline Offline
733 posts
since Oct 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: I'm having problems too.
Next Thread in Java Forum Timeline: help with a Java program that represents names





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC