Results 1 to 14 of 14
  1. #1
    Rose88 is offline Member
    Join Date
    Apr 2009
    Posts
    19
    Rep Power
    0

    Exclamation Loading an image into JDesktopPane as background

    Hey,

    I want to put an image as a background of my project.Could somebody show me a code which loads a background image into a JDesktopPane.

    Thanks,

  2. #2
    mrmatt1111's Avatar
    mrmatt1111 is offline Senior Member
    Join Date
    Aug 2009
    Location
    San Jose, CA, USA
    Posts
    320
    Rep Power
    6

    Default

    You can override the paintComponent of the JDesktopPane (i think) if not you can attach a JPanel to the JDesktopPane instead:

    Java Code:
    private Image backImage = null; //member variable
    ...
    //in the constructor
    backImage = new ImageIcon(this.getClass().getResource("backImage.jpg")).getImage();

    Java Code:
    public void paintComponent( Graphics g )
    {
       if(backImage == null)
          super.paintComponent(g);
       else
       {
          Graphics2D g2d = (Graphics2D)g;
    
          //scale the image to fit the size of the Panel
          double mw = backImage.getWidth(null);
          double mh = backImage.getHeight(null);
    
          double sw = getWidth() / mw;
          double sh = getHeight() / mh;
    
          g2d.scale(sw, sh);
          g2d.drawImage(backImage, 0, 0, this);
       }
    }

  3. #3
    Rose88 is offline Member
    Join Date
    Apr 2009
    Posts
    19
    Rep Power
    0

    Default

    it didn't work!!..wt did u mean about "attach a JPanel to the JDesktopPane instead"??

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Rose88 View Post
    it didn't work!!
    Please tell us more here. What didn't work? Did you get a compilation error? Throw an exception? Not see an image?

    I think that your best bet here is to post some compilable code. Don't post your entire program but instead just create a simple program that has a JDesktopPane that uses an image that we all can use (using an image off the internet would probably work best), that has no extraneous code, but that demonstrates your problem, a Short, Self Contained, Correct (Compilable), Example or "SSCCE". If you can post this, I can almost guarantee that you'll be able to get "helpful" help soon.

    Best of luck.

  5. #5
    Rose88 is offline Member
    Join Date
    Apr 2009
    Posts
    19
    Rep Power
    0

    Default

    wt didn't work is that the image didn't show!

    the code is the same that was wrote up there!

  6. #6
    mrmatt1111's Avatar
    mrmatt1111 is offline Senior Member
    Join Date
    Aug 2009
    Location
    San Jose, CA, USA
    Posts
    320
    Rep Power
    6

    Default

    It works fine for me:

    Java Code:
    public class myDesktopPane extends javax.swing.JDesktopPane
    {
       private Image backImage = null; //member variable
    
       public myDesktopPane()
       {      
          try
          {
             backImage = new javax.swing.ImageIcon(this.getClass().getResource("backImage.jpg")).getImage();
          }
          catch(Exception e)
          {
             System.out.println("Could not find file in folder: " + this.getClass().getResource("."));
          }
       }
    
       public void paintComponent( Graphics g )
       {
          if(backImage == null)
             super.paintComponent(g);
          else
          {
             Graphics2D g2d = (Graphics2D)g;
    
             //scale the image to fit the size of the Panel
             double mw = backImage.getWidth(null);
             double mh = backImage.getHeight(null);
    
             double sw = getWidth() / mw;
             double sh = getHeight() / mh;
    
             g2d.scale(sw, sh);
             g2d.drawImage(backImage, 0, 0, this);
          }
       }
     }
    My Hobby Project: LegacyClone

  7. #7
    Rose88 is offline Member
    Join Date
    Apr 2009
    Posts
    19
    Rep Power
    0

    Default

    and how I call it from any method?..cuz I have the graphics! I don't know how to use graphics!!

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Rose88 View Post
    and how I call it from any method?..cuz I have the graphics!
    Could you state this question a bit clearer? I really can't tell what you're asking here.

    I don't know how to use graphics!!
    This is easily remedied by going through the tutorials and practicing by creating small graphic-rich programs.

  9. #9
    Rose88 is offline Member
    Join Date
    Apr 2009
    Posts
    19
    Rep Power
    0

    Default

    this class how can I call it from the main?!
    Java Code:
    MyDesktopPane j=new MyDesktopPane();
    		j.paintComponent(null);
    is this right?...and instead of null wt should I write?

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    You don't call paintComponent directly, and in fact almost never do so. Instead it is called by the JVM when necessary such as when it is drawing your GUI. You can suggest to the JVM when it should call this by calling repaint() on the JPanel. For instance if you were doing an animation, you'd call repaint each time you changed the position of something that you were drawing. You will learn all this and more if you go through the tutorials which I suggest you do without delay.

  11. #11
    mrmatt1111's Avatar
    mrmatt1111 is offline Senior Member
    Join Date
    Aug 2009
    Location
    San Jose, CA, USA
    Posts
    320
    Rep Power
    6

    Default

    Wow, that is like saying "hey, i have a cement mixer outside... how to do i build a house." I would suggest you start from the basics and through the tutorials as Fubarable suggested. :)
    Last edited by mrmatt1111; 08-25-2009 at 01:19 AM.
    My Hobby Project: LegacyClone

  12. #12
    Rose88 is offline Member
    Join Date
    Apr 2009
    Posts
    19
    Rep Power
    0

    Default

    ok I will...thnx alot u really helped me ;)

  13. #13
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Hey mr matt, took a look at your legacy clone project code... pretty neat stuff. thanks for sharing the link in your signature.

  14. #14
    mrmatt1111's Avatar
    mrmatt1111 is offline Senior Member
    Join Date
    Aug 2009
    Location
    San Jose, CA, USA
    Posts
    320
    Rep Power
    6

Similar Threads

  1. Background Image of Applet
    By BangJava in forum Java Applets
    Replies: 8
    Last Post: 11-24-2010, 06:48 AM
  2. How to set the background of binary image?
    By hyz_zsu in forum Java 2D
    Replies: 1
    Last Post: 04-09-2009, 04:14 AM
  3. Background image
    By leiferouis in forum New To Java
    Replies: 9
    Last Post: 03-08-2009, 06:49 PM
  4. how i can put image in background of frame??
    By ahmed13 in forum NetBeans
    Replies: 4
    Last Post: 01-15-2009, 06:47 PM
  5. Image as background
    By Java.child in forum AWT / Swing
    Replies: 2
    Last Post: 10-03-2008, 12:37 AM

Posting Permissions

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