Results 1 to 18 of 18
  1. #1
    sharmapiyush is offline Member
    Join Date
    Apr 2014
    Posts
    74
    Rep Power
    0

    Default how to set background image to jpanel

    how to set background image to jpanel

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,409
    Rep Power
    25

    Default Re: how to set background image to jpanel

    One way is to override the paintComponent() method and draw the background there.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    sharmapiyush is offline Member
    Join Date
    Apr 2014
    Posts
    74
    Rep Power
    0

    Default Re: how to set background image to jpanel

    how to overload paint component in a jpanel class.
    can you give an example?

  4. #4
    jashburn is offline Senior Member
    Join Date
    Feb 2014
    Posts
    219
    Rep Power
    1

    Default Re: how to set background image to jpanel

    A simple search in the Internet using the search term "jpanel background image" will give you lots of material to work with. E.g.,

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,609
    Rep Power
    5

    Default Re: how to set background image to jpanel

    Did you search the forum before posting this question? That topic has been covered numerous times here.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  6. #6
    sharmapiyush is offline Member
    Join Date
    Apr 2014
    Posts
    74
    Rep Power
    0

    Default Re: how to set background image to jpanel

    i have overrided paintcomponent() but it does not sets my image on the background.

    Java Code:
    public void paintComponent (Graphics g){
        Image a=Toolkit.getDefaultToolkit().createImage("img.jpg");
        g.drawImage(a, 0, 0, this);
        super.paint(g);
    }

  7. #7
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,409
    Rep Power
    25

    Default Re: how to set background image to jpanel

    You shouldn't call the super.paint() method.
    You should load the image outside of the paintComponent() method.
    If you don't understand my response, don't ignore it, ask a question.

  8. #8
    sharmapiyush is offline Member
    Join Date
    Apr 2014
    Posts
    74
    Rep Power
    0

    Default Re: how to set background image to jpanel

    its still not working i have updated my code as

    Java Code:
    Image a=Toolkit.getDefaultToolkit().createImage("img.jpg");
    
    public void paintComponent (Graphics g){    
        a.getScaledInstance(706, 550, 0);
        super.paintComponents(g);
        g.drawImage(a, 0, 0, this);
        
    }

  9. #9
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,409
    Rep Power
    25

    Default Re: how to set background image to jpanel

    Did you add the @Override annotation before the definition of the method so the compiler can check if the method is doing an override?
    Is the method being called? Add a println() statement to print a message when the code is executed.

    This method of reading an image is NOT reliable. Try this to see what errors (none) it gives:
    Image a=Toolkit.getDefaultToolkit().createImage("imgXXXX x.jpg");
    Use the ImageIO read() method,
    Last edited by Norm; 04-09-2014 at 10:24 PM.
    If you don't understand my response, don't ignore it, ask a question.

  10. #10
    sharmapiyush is offline Member
    Join Date
    Apr 2014
    Posts
    74
    Rep Power
    0

    Default Re: how to set background image to jpanel

    when i am adding @Override annotation it says

    Java Code:
     method does not override or implement a method from a supertype
    and shows an error.

  11. #11
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,409
    Rep Power
    25

    Default Re: how to set background image to jpanel

    Look at the API doc for the class that the method is in to see what methods it has. You can NOT override a method that is not in the class.
    If you don't understand my response, don't ignore it, ask a question.

  12. #12
    sharmapiyush is offline Member
    Join Date
    Apr 2014
    Posts
    74
    Rep Power
    0

    Default Re: how to set background image to jpanel

    there is nothing displayed.. everytime it says " cannot perform show javadoc here"

  13. #13
    sharmapiyush is offline Member
    Join Date
    Apr 2014
    Posts
    74
    Rep Power
    0

    Default Re: how to set background image to jpanel

    how to do the same if javadoc is not present for paintcomponent().

  14. #14
    sharmapiyush is offline Member
    Join Date
    Apr 2014
    Posts
    74
    Rep Power
    0

    Default Re: how to set background image to jpanel

    code has running..i have tried using println command .it prints the value when program runs but still image is not visible.

  15. #15
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,409
    Rep Power
    25

    Default Re: how to set background image to jpanel

    This method of reading an image is NOT reliable. Try this to see what errors (none) it gives:
    Image a=Toolkit.getDefaultToolkit().createImage("imgXXXX x.jpg");
    Use the ImageIO read() method,
    If you don't understand my response, don't ignore it, ask a question.

  16. #16
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,609
    Rep Power
    5

    Default Re: how to set background image to jpanel

    I think you meant "super.paintComponent(g)." This still may not solve your problem.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  17. #17
    sharmapiyush is offline Member
    Join Date
    Apr 2014
    Posts
    74
    Rep Power
    0

    Default Re: how to set background image to jpanel

    paintComponent() method executing perfectly as i have checked it with a println command...but image did not displayed on panel.

  18. #18
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,409
    Rep Power
    25

    Default Re: how to set background image to jpanel

    Is the image being read from the file? That method doesn't give an error if the file is NOT found. See post#15
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. JPanel with background image
    By pflerman in forum AWT / Swing
    Replies: 3
    Last Post: 09-22-2011, 09:47 PM
  2. jpanel background -java
    By felito in forum New To Java
    Replies: 7
    Last Post: 02-14-2011, 07:29 PM
  3. Can JDesktopPane can have a background JPanel?
    By chyrl in forum AWT / Swing
    Replies: 13
    Last Post: 10-22-2010, 06:56 AM
  4. background color with jpanel
    By hannerz06 in forum New To Java
    Replies: 6
    Last Post: 03-31-2010, 03:25 AM
  5. how to put a background image in a JPanel
    By yanipao in forum New To Java
    Replies: 5
    Last Post: 10-21-2009, 02:27 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •