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

Display a JPEG  RSS feed

 
Shiv Tattva
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
 
Anuj Sharma R
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.
 
Rob Camick
Ranch Hand
Posts: 2800
15
  • 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.
 
Campbell Ritchie
Marshal
Posts: 55698
163
  • 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: 55698
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
Danny Wreczycki
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.



 
Orville Redenbacher
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:
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!