• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Show A Card. Any Card.

 
Frederick Douglass
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm using JavaFX to construct a card game. But for now I would be delighted simply to SEE a card. ANY card.
My problem is that although I've set up a Scene with a Hbox and Imageview, I cannot actually see any card even though I've set up the size and position.
The only thing showing in the resulting window is its Title. My code is below including a for loop to read in the cards from a file of png images.
It then sets up a Group called root for the Scene , then an ImageView and finally a Hbox. However the window finally shown is blank. What am I missing ? Thanks.

 
John Damien Smith
Ranch Hand
Posts: 296
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For the following code:

The location is probably incorrect and there is probably no png image file at that location.
You can determine this for sure, but querying image.isError();

Usually, for ease of portability, it is best to either:
1. Host the images on a website and retrieve them using http.
2. Place the images on the class path (e.g. as a resource inside the distribution jar for the application) and load the images from there.

An example of (1) http loading from a website:

An example of (2) loading from a classpath resource.
For this to work, download the bear image from the url in the earlier example and place it in the same directory as the ImageDisplayViaResource.java file (e.g. the source "imagedisplay" package because that is where the the ImageDisplayViaResource.java file will be located).
Also ensure that whatever build tool you are using is setup to copy the png image to the target output directory or package of the build (e.g. you should find the bear image in either the same output directory as ImageDisplayViaResource.class or under the "imagedisplay" path inside the jar file built for your application - you can check this using jar tvf <yourjarfile>.jar)
 
Frederick Douglass
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your reply which broadly divides into two parts : where I got it wrong myself AND your suggestions as to what I SHOULD do.
So let's talk about ME

.
"The location is probably incorrect and there is probably no png image file at that location."


Actually, there is. I did change the actual filename shown here for privacy purposes.
The program WAS abending at that point until I inserted the "file:" clause. Notice I also inserted :



Below I show output from that line, at least enough to show SOME of the lines and their cards.
I presume "@30db361", for instance, relates to exactly where in memory this card/image is stored.

The card file in question is 1.4M. Maybe that is too large for an individual Java program but why is it not complaining
and is there a parameter I can reset to assign it that much space ?

As I see it, the simplest way to handle a deck of cards is to read them into the program's main memory,
which is why I'm trying to do it this way. If the way YOU are describing how to do it is indeed 'the Java way'
of doing it then imo that is Java's problem ( effectively insisting on doing it the hard way ).


0 and javafx.scene.image.Image@30db361
1 and javafx.scene.image.Image@261a5bab
2 and javafx.scene.image.Image@435a4ceb
3 and javafx.scene.image.Image@360ee259
4 and javafx.scene.image.Image@58f6d942
5 and javafx.scene.image.Image@2c4506f4
42 and javafx.scene.image.Image@7e93ee5
43 and javafx.scene.image.Image@69d0b8a8
44 and javafx.scene.image.Image@4686933e
45 and javafx.scene.image.Image@264d3d6c
46 and javafx.scene.image.Image@689d3156
47 and javafx.scene.image.Image@464c115
48 and javafx.scene.image.Image@7609bdc4
49 and javafx.scene.image.Image@39e04834
50 and javafx.scene.image.Image@38f6d9df
51 and javafx.scene.image.Image@1ab8dbbd
 
Dave Tolls
Ranch Hand
Posts: 2091
15
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Frederick Douglass wrote:
The card file in question is 1.4M. Maybe that is too large for an individual Java program but why is it not complaining
and is there a parameter I can reset to assign it that much space ?


If the size of the file were the problem then you would (as you guess) see an OutOfMemoryError.
So it's not that that's causing the issue.

Frederick Douglass wrote:
As I see it, the simplest way to handle a deck of cards is to read them into the program's main memory,
which is why I'm trying to do it this way. If the way YOU are describing how to do it is indeed 'the Java way'
of doing it then imo that is Java's problem ( effectively insisting on doing it the hard way ).


John is not suggesting anything other than changing how you load each file, not whether or not you should be loading them all.
Since they're not that big (even uncompressed, which is the size they'll occupy in memory) then loading them all won't be an issue.

Using the code given above by John can you get one of the images to appear?
 
John Damien Smith
Ranch Hand
Posts: 296
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Output like the following is an object reference printed by the default object toString method:

The default toString method is:

So, when you call toString on your images, all it is doing is just reporting the identity of the Image object instance.
However, just because you have an Image object, does not mean that you have a valid image.
The Image object could have been created but there may have been an error loading the image.

You can check the error status of the image by calling the isError method (as previously suggested).

For example the following code which references an image that does not exist exhibits exactly the same behavior that you report in your posts (image object is created, image reference prints, but an image is not displayed):

I still believe your issue is as I originally stated: "The location is probably incorrect and there is probably no png image file at that location.".
Probably you are just missing a / to make the file location reference absolute rather than relative.
e.g. Instead of:

You should probably have:

 
Frederick Douglass
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To begin at the end, your suggestion :

Probably you are just missing a / to make the file location reference absolute rather than relative.


I have now tried running both with and without that leading / and get the same ( non-) result. So let me go back to your original pair of suggestions :

1. Host the images on a website and retrieve them using http.


Can we at least agree that my card file actually exists on my system and does NOT need to be retrieved ?

That leaves :

2. Place the images on the class path (e.g. as a resource inside the distribution jar for the application) and load the images from there.


I think I see what you are doing in your code, at least broadly, but freely admit the details could trip me up.
So let me try get the details right as much as I can but obviously feel free to correct me any time I get something wrong.

First, yoiu show a listing of an entire program totalling 33 lines. I assume I must adapt this so that it is a method used by my own program.
( That includes using the same imports and package.) If I understand it correctly, what the program does is to show a single image
named "Bear-icon.png". The program sets a Scene with a stackPane, Imageview and Image and then shows that scene.

So far so good, I hope. The big remaining question is exactly how to point to my card file and its images ?
On lines 10 & 11 you have :



Now that seems to be a filename, as opposed to a file PATH. Assuming my 7 of clubs is in a file called home/name/cards/7_of_clubs.png, then if I replace that line with



the program will show that card ?

Even if that does work, does it not mean that if I show the 7 of clubs 5 times in the course of a game then it must be loaded from its file 5 times ?
I originally tried to load the images into the program's main memory :



Could that be adapted for the line just coded ?



?






 
John Damien Smith
Ranch Hand
Posts: 296
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> Now that seems to be a filename, as opposed to a file PATH.

No, not necessarily, its a resource loaded from the classpath

> Assuming my 7 of clubs is in a file called home/name/cards/7_of_clubs.png, then if I replace that line with the program will show that card ?

No, usually JavaFX programs are packaged as jar files. If the resource you are trying to load is not in the jar file at the location you specify, it will not be loaded. If you don't add a leading slash, then the classpath loader will look for a location relative to the class from which the class loader was retrieved.

> Even if that does work, does it not mean that if I show the 7 of clubs 5 times in the course of a game then it must be loaded from its file 5 times ?

No, you don't need to load the Image five times. You are storing a reference to the image, in memory, just keep it around as long as you want.

> Could that be adapted for the line just coded ? IMAGE_LOC = " images[i]";

Absolutely not - an array reference as a string is not a URL based lookup location or a classpath location reference for an image.

----

My guess is all I am doing is confusing you more than anything else.
 
Frederick Douglass
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any confusion on my part is my own problem and is obviously not your fault. I appreciate the fact that you are trying to help, despite my inability to get the message.
As I see it, there are two separate issues :
Loading images from external memory ( a file ) to a program's own memory AND actually getting these images shown in a window.

My frustration ( rightly or wrongly ) is that I SEEM to have loaded the images into main memory ( an array of Images ) BUT cannot actually show them.
'Typical' Java ? On the one hand managing to get well over 1M into main memory BUT not handling those objects in an 'obvious' way.
You are mainly talking about how exactly to access the image files. Look in detail at IMAGE_LOC, as you discuss it yourself:

"You are storing a reference to the image, in memory, just keep it around as long as you want.
"

Sounds good as far as it goes. Please explain in simple detail ( such as comparing it to keeping images in a array ).
 
Dave Tolls
Ranch Hand
Posts: 2091
15
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As John says, we don't know if you've managed to read in the images.
And Image object will exist whether it's successfully loaded or not.
Have you logged the isError() flag?
If those are true then it failed to load the image.
 
Frederick Douglass
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As far as NetBeans is concerned there is no error. What happens is that it shows a blank window and that is it. I presume therefore there are no actual error messages to access.
 
Dave Tolls
Ranch Hand
Posts: 2091
15
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
isError() is a method on Image that tells you if that image is OK.
If it returns true then it has failed to load for whatever reason.
You won't get a message.
You're supposed to check the flag.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic