K Richard

Greenhorn
+ Follow
since Jul 19, 2017
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by K Richard

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.
6 years ago
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?
6 years ago
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.


6 years ago

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?
6 years ago

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.
6 years ago
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.
6 years ago
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.
6 years ago
6 years ago
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.
6 years ago
Totally agree, it most certainly has made for confusing code and for confusing the hell out of not just me but countless others spanning YEARS, anyway thanks for all your help, especially your last post which did actually help me understand it further, before reading statements like what you made I just can't seem to see the wood for the trees but then after I read your comment about successive if statements as opposed to if/else if/else it just becomes obvious, kinda the same as the pool puzzles in this book that have been frustrating me, while I feel at the moment being able to solve them is just too much, I can still however fully iterate them on paper and understand exactly why and where things are happening BUT ONLY ONCE I ALREADY LOOK AT THE ANSWER, I'm not sure if this is part of the learning curve of a programmer or not and maybe someday soon ill push past this stage and be able to just instinctively see what code goes where in these puzzles for them to make sense.

Thanks again.

K.
6 years ago
Maybe it SHOULD, but it doesn't.

And this is the ONLY solution I've found in the last 2 days of searching, I'd rather not waste anymore of my time trying to break down and fully understand all the intricacies and finer syntactical details of a class that we as readers are specifically told NOT TO DO THAT, as we will understand it better the further progression is made into the book, I will be sure to revisit this part of the book once I've either:

A. Finished the book
B. Am more proficient and learned in the java language.

Maybe this was the original authors intent.

K.
6 years ago
Ok so after much searching found the answer online here:

http://forums.oreilly.com/topic/49225-chapter-6-dotcom-class-with-arraylist/
poster named Jugo. (Legend).

No need for me to post the original code as i had typed it or as the book has it printed but what i will post is the code as it should be, as i now have and have tested, works as originally intended by the authors, its a shame that beginners have to endure such errors and struggle along when all the while the all knowing all powerful authors had actually instructed the reader on page 112 that "Whenever you see the Ready Bake Code logo, you are seeing code that you have to type in as-is and take it on FAITH (i have placed emphasis, not the book), TRUST IT(again more emphasis). You'll learn how that code works later", well who would have thought that exactly 40 pages later that FAITH would be tested and fail miserably, the TRUST is officially gone, never to return, with regards to this boldly claiming book, here is the working code as it should be.

I shall just paste the offending method, all other code is correct.



While it seems obvious to me now when i see that of course without breaks how could one exit a loop, i still find rather sloppy the authors would leave out an entire else statement and have a Boolean wrong, anyway.

Hopefully that should clear this matter up somewhat quicker for any poor unfortunate souls that happen to buy this book and find themselves here.

Quick thanks to ALL who posted replies.

I'm now finally moving on and continuing my learning instead of marking time.

Thanks again.

K.
6 years ago
Your missing a + operator from this line of code:

How it should look:



How yours looks:



See the difference?                                                        
6 years ago

Knute Snortum wrote:Look carefully at this line.  What's wrong?



My lack of experience is showing as i have no idea whats wrong with that line of code as a stand alone piece of code or with respect to the greater whole, but in my defense that is part of the gameHelper class which the book explicitly states not to worry about trying to understand yet, and while i wish i did understand it and while i also wish i could see as you do what is wrong with that line of code, i just simply cant, it is at this stage beyond me, i can certainly play around with it and remove the not operator which then runs the code again as intended but as before assigns a0 as the random location to every objects elements, i can also change the ampersand to two of them instead of one but this doesn't really change the code, and as my understanding would have only changes what gets checked should something return false.

I also have no idea where the number 200 has been pulled from and what significance it has.

So unfortunately while i'd love to be able to answer you, I'm still none the wiser.

Thanks.

K.
6 years ago
Ill change it back but that then just puts me back into the infinite loop, as for "x" being a terrible variable under these circumstances, i am doing as per instructed by the book, i.e. copy paste and don't ask questions, i have attached the offending code in two images for anyone to have a look and help, for the time being i shall change the operator back to less than (<), as is in the book.

Thanks.

K.
6 years ago