Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

Headfirst Java, Chapter 12 page 365, Displaying a Jpeg?  RSS feed

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

So ive been continuing to work through my Headfirst Java book and have come across something that i just cant seem to find a suitable answer to, i have found lots of information explaining how to display jpegs in java in sooooooooooo many wild and wonderful ways but NONE that really help me with my problem specifically hence im here asking.

So the code in the book is as follows:




Now while i understand that i dont call the paintComponent method directly, i simply add the panel (class object) to the frame i have created and the paintComponent method get called by the system, my problem is that i cannot get my image to display, im unsure on whether this is a file path problem or what???

Here is my code, including the code that runs it all:



And now the class that runs the frame:




So as youll be able to see there are other pieces of code like buttons and radio button and drawpanels etc, but they all get overwritten by the latest thing added as the code is read as im sure your aware, so as you can see i create and instantiate the MyFirstImageIcon object on line 37 and then add it to my frame on line 39, now this code compiles and runs fine but does not display the image, it also however does not display the last thing to run on the frame which was a blue rectangle, all im left to look at when to frame loads in a blank white content pane, this tells me that something has happened but i just dont know what, as i stated earlier im not sure if its a file path error or something possibly outdated with the code that the book provides with respect to how i would achieve displaying an image, as also stated earlier ive seen so many weird and wonderful ways to do the same thing and so maybe the code the book has provided is now outdated, i do however like to try and stick to what the book is specifically trying to teach me that way i can follow on fine in future exercise etc and not have to try and implement things into the books exercises that ive learned from the interweb, which ultimately would more than likely cause me more confusion.

There is a small addendum to the books code that say:



"...Note: If you're using an IDE and have difficulty, try this line of code instead:





Now because i am using an IDE i therefore have tried to use that line of code, but with zero success, and so finally ive come here in the hope that someone can help.

One last thing, if i were to want to be able to type in JUST the filename, where exactly does the image need to be saved to/stored in order for the IDE to find it, not that it would seem to make much difference as ive had the image stored on the desktop and used the entire filepath from C: all the way through User/Desktop etc etc, of course ensuring that the slashes are of the forward type "/" and not the slashes that are in the filepath name if one just copies the filepath from the folders explorer bar.


Sorry for the long post,

K.

P.s. There is also of course a main class but that just simply creates and instantiates a SimpleGui object and then calls the myFirstGui method from that class, nothing worthy of note.
 
Master Rancher
Posts: 3276
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code does not compile for execution testing because of missing class.
 
K Richard
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Norm Radder
Master Rancher
Posts: 3276
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please fix the compiler errors and post all the code so the program can be compiled and executed for testing.

The Main class is not referenced in the posted code and is not needed to compile the code in the first post.
 
K Richard
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok lets try again,

We'll start with Main:



Now we move to the next class i created:



Next class i created:



And finally the latest class:



Hope that helps.
 
Norm Radder
Master Rancher
Posts: 3276
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, that's much better.  It compiles (with reference to my image) and executes and shows my image.

What  do you want it to do different?
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was also reading that book. It's outdated. people now use JavaFX rather than awt and swing (which are outdated and not developed since long).
 
K Richard
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As stated in my original post, i realize that my code compiles, my problem is that it does not show my image, but does however show me a blank white window instead of the previous blue rectangle, thus telling me that SOMETHING is happening im just not sure what, this also leads me to believe that its a file path problem as i also mentioned in my previous post, maybe that post was TL;DR sorry if that was the case, i guess if i could break it down my first question would be:

Where do i store my images that i want the program to access so that i am able to write as short a file path a possible?

How is your file path written?

Where is YOUR image stored in order that the program was able to access it and display it?

Thanks.
 
Norm Radder
Master Rancher
Posts: 3276
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Where is YOUR image stored in order that the program was able to access it


I have my images in a folder (images) that is contained in the folder where I issue the java command.
The statement I compiled:
The comment: moved reminds me that I moved that statement out of the paintComponent method to the class level.  Long time and one time jobs should not be done in a paint method.
 
K Richard
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

karol worek wrote:I was also reading that book. It's outdated. people now use JavaFX rather than awt and swing (which are outdated and not developed since long).



Thanks for the heads up, i do realize that a lot of this stuff will be outdated, even the 2nd edition that i have is teaching Java 5.0, i am however just trying to grasp the basics and follow on through this book, even if that means learning old tricks, which im sure wont do me any harm and then once im more established in the ways of Java, of course i will progress onto the more up to date techniques.

Thanks again.
 
K Richard
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Norm Radder wrote:

I have my images in a folder (images) that is contained in the folder where I issue the java command.




Ok that didnt really help much, sorry, im looking at this from the perspective of my project folders, for example i have my main JavaProjects folder (root) which of course stores all my individual projects in their own folders, so within the project folder called MyFirstGui are a further 3 folders and a .iml file the folders are:

* .idea
* out
* src

Which of these folders should i store my image in as the "out" and "src" folders also have many sub-folders?
 
Norm Radder
Master Rancher
Posts: 3276
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know where your IDE sets the current directory when it executes the java command. 
To see where the current directory is when the code executes, put a line in the code like this to print out the current directory:
That will show you where JVM will look for files.
 
K Richard
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok so i added that line of code and imported:

import java.io.File;

Run the program and this was the result:

C:\Users\Kev\Desktop\JavaPrograms\MyFirstGui\. C:\Users\Kev\Desktop\JavaPrograms\MyFirstGui

So does this mean the first place its looking in the very first folder that holds the .idea folder and out folder etc etc?

If so i tried i storing the image in that folder so that i could just specify the exact file name and no more i.e "mushroom.jpeg" and still nothing, just a blank white frame.


 
K Richard
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it because its a Jpeg?, i notice you used a gif and also on the Oracle site found here:

http://docs.oracle.com/javase/tutorial/uiswing/components/icon.html#getresource

They also use a gif in their example?
 
karol worek
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

K Richard wrote:Is it because its a Jpeg?, i notice you used a gif and also on the Oracle site found here:

http://docs.oracle.com/javase/tutorial/uiswing/components/icon.html#getresource

They also use a gif in their example?



You ask a lot of questions Richard. No offence but literally all you have to do to display jped is to copy paste any working example from internet. If you dont know the place of files in your IDE then you can set address instead of "image.jpg". For example "C:\\Users\\Adam\\Desktop\\image.jpg" (double \\ instead of \)
 
Norm Radder
Master Rancher
Posts: 3276
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It looks like this folder is the current directory when the JVM executes:
C:\Users\Kev\Desktop\JavaPrograms\MyFirstGui
Did you try putting the image in that folder?
 
K Richard
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Its solved, rather embarrassing actually, a typo on my part, which i only realized BECAUSE i asked that last question (karol), the file is a "jpg" NOT a "jpeg", a simple but fatal error, i have however learned many things during this lesson:

* How to find my directory filepath.
* Where i need to store my images if i want to access them DIRECTLY.
* Check and double check all code relating to file names and paths.

Thanks to all who helped.

K.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!