Results 1 to 4 of 4
  1. #1
    Join Date
    Nov 2011
    Posts
    28
    Rep Power
    0

    Default Loading an image and then using it as a background

    Hey all so I am attempting to load an image and use it as a background for a 2d game I've made for my Java course. Here is what I have, and it is not working. No image is being loaded/displayed

    Java Code:
    img = Toolkit.getDefaultToolkit().getImage(getClass().getResource("mario.jpg"));
    Java Code:
    public void drawBackground(Graphics graphics)
        {
        	graphics.drawImage(img, 0, 0, this);
        }
    I don't see what the issue is here, but then again I dont have much experience in writing graphics. Thanks in advance!

  2. #2
    Join Date
    Nov 2011
    Location
    Schijndel, Netherlands
    Posts
    12
    Rep Power
    0

    Default Re: Loading an image and then using it as a background

    Try repaint().

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

    Default Re: Loading an image and then using it as a background

    Quote Originally Posted by jeroenvanveghel View Post
    Try repaint().
    Irrelevant without associated advice on how to perform custom painting correctly.

    Lesson: Performing Custom Painting (The Java™ Tutorials > Creating a GUI With JFC/Swing)

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

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

    Default Re: Loading an image and then using it as a background

    You don't draw images like that, but rather than us re-write a chapter, your best bet is to find the graphics tutorials and read them before trying to do this stuff.

Similar Threads

  1. use image for background
    By tony_stark in forum AWT / Swing
    Replies: 1
    Last Post: 05-31-2011, 08:20 PM
  2. Replies: 12
    Last Post: 04-14-2011, 01:58 PM
  3. [Help] W/ background image
    By gundum584 in forum New To Java
    Replies: 9
    Last Post: 01-10-2011, 05:48 AM
  4. Loading an image into JDesktopPane as background
    By Rose88 in forum Advanced Java
    Replies: 13
    Last Post: 08-25-2009, 01:10 AM
  5. Background image
    By leiferouis in forum New To Java
    Replies: 9
    Last Post: 03-08-2009, 05:49 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
  •