• 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:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Display a JPEG

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I found this code in(Chapter 12) head first java book.
I understood the explanation given for this in the book, but explanation for getImage() in line 2 and "this" argument in the line 3 are not given in that book.
I suppose Image is the superclass of ImageIcon class and getImage() is the method of ImageIcon class .
is "this" in line 3 a reference to the object of class extending JPanel ?
Somebody please explain this code .
Thanks
 
Greenhorn
Posts: 28
1
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Shiv,

Have a look at the below which would give a more detailed explanation of the getImage() function:
http://docs.oracle.com/javase/7/docs/api/javax/swing/ImageIcon.html#getImage%28%29
http://docs.oracle.com/javase/7/docs/api/java/awt/Image.html

'this' refers to the current object which is being executed.

In the code sample, first statement gets you the image and stores that in the image variable. This file should exists in the classpath.
2nd statement draws that on the panes with 3,4 being representing the x,y co-ordinates where the image's left corner should go.

Hope this helps.
 
Rancher
Posts: 3051
22
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I hope you didn't find that code in a book.

You should NOT read the image file in the paintComponent() method. Swing invokes the paintComponent() method whenever it determines the components needs to be repainted, so it does not make sense to read the image every time.

Also, you should invoke super.paintComponent() as the first statement in the method to make sure the background of the component is cleared before the image is painted.
 
Marshal
Posts: 64642
225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also it should have protected access, not public.
 
Shiv Tattva
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you all for replying
 
Campbell Ritchie
Marshal
Posts: 64642
225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
Greenhorn
Posts: 23
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I been bored a little so deciced to provide you simple app with actually display an image. Take a look at code and feel free to tweak.



 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have been playing around with with Java image manipulation. I can see that when using a buffered image this seems to be the way to do it:


I was wondering why, in the code I have written here, this is necessary? This as opposed to just I see on one of the answers here, maybe it has something to do with Java possibly not loading the image correctly (

private BufferedImage argb = null; // sometimes java is not displaying image colors properly if image its not argb colorspace

). But this is not enough of an explanation for me.

Here is my code:
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!