passing values and displaying images in a GUI

Reply

Join Date: Oct 2006
Posts: 730
Reputation: hbk619 is an unknown quantity at this point 
Solved Threads: 7
hbk619's Avatar
hbk619 hbk619 is offline Offline
Master Poster

passing values and displaying images in a GUI

 
0
  #1
May 13th, 2007
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, 11 views)
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 437
Reputation: Fungus1487 is on a distinguished road 
Solved Threads: 50
Fungus1487's Avatar
Fungus1487 Fungus1487 is offline Offline
Posting Pro in Training

Re: passing values and displaying images in a GUI

 
0
  #2
May 14th, 2007
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.

  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

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

hope this helps if ive understood your question right
When Autumn Falls [ http://www.whenautumnfalls.co.uk ] &&
Designdotworks [ http://www.designdotworks.co.uk ] Web / Graphic / Software Design
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 437
Reputation: Fungus1487 is on a distinguished road 
Solved Threads: 50
Fungus1487's Avatar
Fungus1487 Fungus1487 is offline Offline
Posting Pro in Training

Re: passing values and displaying images in a GUI

 
0
  #3
May 14th, 2007
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 ?
When Autumn Falls [ http://www.whenautumnfalls.co.uk ] &&
Designdotworks [ http://www.designdotworks.co.uk ] Web / Graphic / Software Design
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 730
Reputation: hbk619 is an unknown quantity at this point 
Solved Threads: 7
hbk619's Avatar
hbk619 hbk619 is offline Offline
Master Poster

Re: passing values and displaying images in a GUI

 
0
  #4
May 16th, 2007
Originally Posted by Fungus1487 View Post
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 >
Last edited by hbk619; May 16th, 2007 at 4:56 pm.
Attached Files
File Type: java game.java (7.4 KB, 4 views)
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC