Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

I keep adding images and only the path is displayed need help please  RSS feed

 
Ross Gerard
Ranch Hand
Posts: 53
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello my fellow ranchers! I downloaded a couple images which is great for practice only. I keep adding the images but the only thing showing is the pathway please help.







 
Ross Gerard
Ranch Hand
Posts: 53
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I made a folder in files called images and stored the images there
 
Carey Brown
Bartender
Posts: 2993
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

This will return the word "reading" followed by the String returned by image1.toString(). The image1.toString() method happens to return the file path. If you're expecting to see an actual image then this won't do it. You'll need to delve further into AWT or Swing in order to display the icon image.
 
Ross Gerard
Ranch Hand
Posts: 53
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am still stuck on this problems. Like a boxer wanting to train I would love to be shown the move before I can execute it.
 
Dave Tolls
Rancher
Posts: 2914
36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where are you trying to put these images?
 
Ross Gerard
Ranch Hand
Posts: 53
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I put the images in a file folder called images . Three jpg labeled reading, interacting and talking. I want the images in my student class. The reason I decided to put the images in the student class is because there is a method there that randomly generates a string. Depending on what string is generated I need to display the image appropriate to the string being generated. I have just shown my class student where the method is that I want to add the images above. However, I can´t get the images to load and I looked in the files and the images show up fine there
 
Carey Brown
Bartender
Posts: 2993
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ross Gerard wrote:I put the images in a file folder called images . Three jpg labeled reading, interacting and talking. I want the images in my student class. The reason I decided to put the images in the student class is because there is a method there that randomly generates a string. Depending on what string is generated I need to display the image appropriate to the string being generated. I have just shown my class student where the method is that I want to add the images above. However, I can´t get the images to load and I looked in the files and the images show up fine there

Are you using Java's AWT GUI frame work? Swing? Or something else? Plain vanilla Java provides a text based interface. If you want to display graphics you'll need to utilize one of Java's GUI support frameworks.
 
Ross Gerard
Ranch Hand
Posts: 53
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am using swing I thought. Here is everything where is the best place to put the images?






















 
Ross Gerard
Ranch Hand
Posts: 53
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No matter how I try to put it in I always get the pathway
 
Dave Tolls
Rancher
Posts: 2914
36
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need a component on your panel that will display the images.

The simplest would probably be a JLabel, whose constructor can take an ImageIcon, or use the setIcon method.
So you can change your WhatIsUp method to return the relevant ImageIcon instead of a String, and assign it to a JLabel on your panel.
 
Carey Brown
Bartender
Posts: 2993
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need a constructor that take an Icon just like the constructor in JButton. Then you need to call that constructor when you create your button providing the desired Icon, which in your case is an ImageIcon because Icon is an interface and cannot be instantiated.




Class name should always begin with an upper case letter.

You can remove "getContentPane()." and just use setLayout() and add() directly.
 
Ross Gerard
Ranch Hand
Posts: 53
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmm okay studying this
 
Ross Gerard
Ranch Hand
Posts: 53
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it possible to add the text to this as well with the appropriate image that is generated?
 
Ross Gerard
Ranch Hand
Posts: 53
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By the way it works great you are the best. I was really struggling with understanding how to pass the images in but you have clarified it nicely. Thank you
 
Paul Clapham
Sheriff
Posts: 22487
43
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seems to me that you should take some time to go through the Swing tutorial. This is a great forum, sure, but learning a language by asking questions on a forum isn't really a practical approach. So go through the tutorial and play around with its examples, and if you have problems then the forum is here for you.
 
Campbell Ritchie
Marshal
Posts: 55707
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ross Gerard wrote:I am using swing . . .
It is not called swing
Ouch! Don't do arithmetic on anything coming from Math.random(). Don't use floats for arithmetic. Don't use floats for anything (unless something in an API demands a float). Don't use those repeated if blocks, least of all without else. Use an array and use the “random” int as the array index. You can find a better way to get random numbers in this old thread.
 
Ross Gerard
Ranch Hand
Posts: 53
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the link Sheriff.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!