Win a copy of Secure Financial Transactions with Ansible, Terraform, and OpenSCAP this week in the Cloud/Virtualization forum!
  • 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
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Rob Spoor
  • Henry Wong
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh
  • Jj Roberts

Display a JPEG

 
Greenhorn
Posts: 13
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 3232
30
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 72478
315
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also it should have protected access, not public.
 
Shiv Tattva
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you all for replying
 
Campbell Ritchie
Marshal
Posts: 72478
315
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome
 
Greenhorn
Posts: 23
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Shiv Tattva wrote:
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.
Thanks



Hello I know, I'm very late but anyone who stuck at Head first Java chapter 12 365 page.
Just skip that page and comeback when you learn about inner classes(Only few pages to turn 378).
By using Inner Class and Graphics2D you can display Image in paintComponent() Method.
Here's the code

 
Campbell Ritchie
Marshal
Posts: 72478
315
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

Pleased to see you not subtyping JFrame. That is a mistake you have avoided
Please always indent your code correctly; it isn't obvious that the DrawPanel class is an inner class. Also leave one empty line between successive methods.
I can see some style problems: Call the parameter for the main() method args. I would prefer to call the go() method initGui().
Does your code work? Afraid that solution isn't completely correct:-
  • 1: Don't make that class a non‑private inner class. I would consider making it an anonymous class inside the go() method.
  • 2: Give the paintComponent() method protected access, not public. Remember you will never call it directly.
  • 3: Always mark any methods you think you are overriding with @Override.
  • 4: Start paintComponent() with super.paintComponent(g); That will clear the image if you need to repaint it, e.g. after resizing the display.
  • 5: Your identity conversion in line 20 isn't necessary. You might not need a cast to Graphics2D either.
  • 6: Mark all fields private. It may be possible to declare the frame as a local variable in initGui().
  •  
    Saloon Keeper
    Posts: 23440
    159
    Android Eclipse IDE Tomcat Server Redhat Java Linux
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Campbell Ritchie wrote: I would prefer to call the go() method initGui().



    I usually call my "go" method doIt(). I think I inherited the practice from Smalltalk. I wouldn't call it "initGui" because it's not just initialising the GUI, it's also running it. But different strokes...
     
    I don't like that guy. The tiny ad agrees with me.
    SKIP - a book about connecting industrious people with elderly land owners
    https://coderanch.com/t/skip-book
    reply
      Bookmark Topic Watch Topic
    • New Topic