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 .
'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.
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.
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.