• 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
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Image does not appear?

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I was trying to load an image and print it out on the screen, but for some reason it does not seem to appear. The path is correct (the file is in a res folder), but even with an image that is elsewhere on my computer with the correct file path it still does not work.

Game.java



Screen.java



Background.java



Thanks!

Also, another question I have, is how would I define the drawImage function in the background class, so that in the screen.paintComponent method I just have to call background.paint(g) and it prints it out on the screen?
Something like this:



and then I just have to call player.paint(g) in screen.paintComponent method

but instead with an image like so:



but that does not work because "The method drawImage(Image, int, int, ImageObserver) in the type Graphics is not applicable for the arguments (Image, int, int, Background)"
 
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is no need to create a custom component to display an image. Just add the ImageIcon to a JLabel and add the label to the frame.

You have several problems with your painting code:

1) Don't override or invoke the update() method. This is only done in AWT and not in Swing.
2) Your "PaintComponent" method name is wrong. Methods don't start with an upper case character. Use @Override before the method name and the compiler will catch the error. Ie try the following to see what the compiler error is, then fix the error:



3) You are using the drawImage() method invorrectly. The second/third parameters should be the location of the image, not the size.
 
The human mind is a dangerous plaything. This tiny ad is pretty safe:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic