• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Can't display image on JPanel

 
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Guys,
Creating a Hangman Game, just started developing the GUI aspect of it.
JPanel covers the JFrame, I set the layout to null, and I'm using the setBounds(x, y, width, height) to position everything on the screen.

However, I have had no luck with being able to display a .png image on the panel?

My setUpGui() method:

setHookStatus() method:

Nothing prints on the screen. I'm using Eclipse and the image is located in the images folder right next to my source code.
I have also tried extending my class to JPanel, and overriding the paintComponent method and call the drawImage(image, x, y, panel), but this hasn't worked at all either. any thoughts?


screen.png
[Thumbnail for screen.png]
Home Screen for the application
screen-2.png
[Thumbnail for screen-2.png]
If I add method userHookLabel.setOpaque(true); I get this result:
 
Bartender
Posts: 732
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1. You really should use a LayoutManager!
2. Have you checked the result of the call
to see that it is successful?

 
Ben Jass
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not using the layout managers because it's not able to align the components the way I imagined for this project. How would I display the result of the call for an image icon object?
 
Bartender
Posts: 1844
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A layout manager would be better; GridBagLayout is my favorite and would work well in your case here, but it takes a bit of patience to understand all of its nuances. But that is beside the point of your question.

To check the result, just call System.out.println(userHook) and see what you get. Or add a breakpoint in eclipse and check the value of the variable. The big question is, "Is it null?"
 
Ben Jass
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'll look into GridBagLayout, thanks.

The result was it printed the name of the location shown: images/HangMan Hook.png
It's not null.
 
Fred Kleinschmidt
Bartender
Posts: 732
10
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you sure there is anything in the ImageIcon? What is the result of
 
Ben Jass
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The results that were printed were: -1 and 1.

So I checked the methods that could be called on an ImageIcon object and nothing indicates a setSize() method.

The actual file dimensions:
Width: 110
Height: 136

So I tried extending from JPanel, overriding the paintComponent() method:



My paintComponent() method never ran?
 
Fred Kleinschmidt
Bartender
Posts: 732
10
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That result means that your file (images/HangMan Hook.png) does not contain a valid image.
 
Ben Jass
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
By invalid image, do you mean there is something wrong with the actual file itself? Or it is not able to locate the file? I made the file in paint.
 
Ben Jass
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Everyone for the help, I was able to get it to print using


But one last question, does anyone know why my paintComponent Method is never called? I've tried repaint() and revalidate()
 
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Considering we've only ever seen it by itself, without any context at all, there could be any number of reasons why it's not being called. It's pretty clear that it's a method of some object which isn't part of your GUI -- or perhaps it doesn't override any method of JPanel -- anyway you see what I mean, it would be better to look at how the method is used rather than guessing.
 
Fred Kleinschmidt
Bartender
Posts: 732
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You haven't shown us enough code to be able to answer this, especially the declaration of the class that contains your paintComponent method. But to hazard a guess, perhaps your class extends JFrame (which does not have a paintCoimponent method), or your paintComponent method has a different signature than its superclass' method.
 
Rancher
Posts: 1093
29
Netbeans IDE Oracle MySQL Database Tomcat Server C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ben,

It probably did run, but nothing is really being done in it that is of any use. Generally speaking make a call to super.paintComponent(g) so it will erase your screen, then have everything written back to it afterward. What you are showing, as has already been stated, is not enough to know what is going on. IMO: you will have much better luck using a BufferedImage and drawing that onto the context of the JPanel using drawImage as described in the API--g.drawImage where g is the graphics context of the JPanel. You can get a BufferedImage using ImageIO.

Les

Here is an example:


Ben Jass wrote:The results that were printed were: -1 and 1.

So I checked the methods that could be called on an ImageIcon object and nothing indicates a setSize() method.

The actual file dimensions:
Width: 110
Height: 136

So I tried extending from JPanel, overriding the paintComponent() method:



My paintComponent() method never ran?

 
Fred Kleinschmidt
Bartender
Posts: 732
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As Les Morgan's example shows, your first statement in paintComponent() should be

But then I don't think you even need to override the paintComponent() method at all. You set the Icon to be the icon in the userHookLabel child, so the label will be automatically rendered when your component is rendered.
 
Les Morgan
Rancher
Posts: 1093
29
Netbeans IDE Oracle MySQL Database Tomcat Server C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ben,

When you get a -1, and I am not sure about the 1, it means the load has not completed yet. So you are basically trying to render a null to the graphics context. In my experience once that happens, you will not have a completed image for the life of the run. So even though you try to render, and your paintComponent works, there isn't any image to render--nothing happens.

Les

Ben Jass wrote:The results that were printed were: -1 and 1.

So I checked the methods that could be called on an ImageIcon object and nothing indicates a setSize() method.

The actual file dimensions:
Width: 110
Height: 136

So I tried extending from JPanel, overriding the paintComponent() method:



My paintComponent() method never ran?

 
Les Morgan
Rancher
Posts: 1093
29
Netbeans IDE Oracle MySQL Database Tomcat Server C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yep, if you use an ImageIcon and a JLabel, then you have nothing to do with the paintComponent of the JPanel, and since you are using a "null" layout manager, you don't even have to worry about resizing your image to fit fully onto the JLabel.

Fred Kleinschmidt wrote:As Les Morgan's example shows, your first statement in paintComponent() should be

But then I don't think you even need to override the paintComponent() method at all. You set the Icon to be the icon in the userHookLabel child, so the label will be automatically rendered when your component is rendered.

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic