7 Replies - 143 Views - Last Post: 05 February 2012 - 04:07 PM Rate Topic: -----

Topic Sponsor:

#1 dwh1996  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 04-February 12

Problem With Putting Image In JPanel

Posted 05 February 2012 - 08:46 AM

I am trying to make a JPanel with an image that takes up the whole thing so that I can just throw it into a JFrame and have it as a background. I am having difficulty figuring it out, I have looked around the internet and couldn't find an answer that worked.
Here is what I have right now:
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.*;

public class ImageBackground extends JPanel {

    ImageBackground() {

        BufferedImage img = null;
        try {
            img = ImageIO.read(new File("Africa_Scramble_New.jpg"));
        } catch (IOException e) {
        }
    }

    public void paintComponent(Graphics g) {

        g.drawImage(img, 0, 0, this);
    }
}



Is This A Good Question/Topic? 0
  • +

Replies To: Problem With Putting Image In JPanel

#2 tlhIn`toq  Icon User is offline

  • WillMyCodeWork = !FailedWhenYouTriedIt;
  • member icon

Reputation: 3290
  • View blog
  • Posts: 6,898
  • Joined: 02-June 10

Re: Problem With Putting Image In JPanel

Posted 05 February 2012 - 09:15 AM

"Having trouble" is just really, really vague and will make it hard for experts to help you.

Can you be more precise about what you are having trouble with? Errors? Scaling the image?
Was This Post Helpful? 0
  • +
  • -

#3 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1413
  • View blog
  • Posts: 6,037
  • Joined: 20-September 08

Re: Problem With Putting Image In JPanel

Posted 05 February 2012 - 09:21 AM

setPreferredSize(new Dimension(img.getWidth(), img.getHeight()));


should do it for you. For best results, add the panel to a component with a BorderLayout set on it.


Quote

} catch (IOException e) {
}


Never do that btw, it's just asking for getting errors with no findable cause. e.printStackTrace() almost every time unless no logging framework in place

This post has been edited by g00se: 05 February 2012 - 09:22 AM

Was This Post Helpful? 1
  • +
  • -

#4 dwh1996  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 04-February 12

Re: Problem With Putting Image In JPanel

Posted 05 February 2012 - 01:37 PM

I'm getting an error in line 22 with drawing the image. The word img is red and underlined.
Was This Post Helpful? 0
  • +
  • -

#5 tlhIn`toq  Icon User is offline

  • WillMyCodeWork = !FailedWhenYouTriedIt;
  • member icon

Reputation: 3290
  • View blog
  • Posts: 6,898
  • Joined: 02-June 10

Re: Problem With Putting Image In JPanel

Posted 05 February 2012 - 02:34 PM

LMAO

Does your IDE not tell you what the error is?
Maybe if you hover the mouse over it?

What program are you developing in?
Was This Post Helpful? 0
  • +
  • -

#6 dwh1996  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 04-February 12

Re: Problem With Putting Image In JPanel

Posted 05 February 2012 - 03:53 PM

I am using netbeans and it does have an error. It says that it can't find the variable img.
Was This Post Helpful? 0
  • +
  • -

#7 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1413
  • View blog
  • Posts: 6,037
  • Joined: 20-September 08

Re: Problem With Putting Image In JPanel

Posted 05 February 2012 - 04:06 PM

http://technojeeves....free/118-errors
Was This Post Helpful? 0
  • +
  • -

#8 tlhIn`toq  Icon User is offline

  • WillMyCodeWork = !FailedWhenYouTriedIt;
  • member icon

Reputation: 3290
  • View blog
  • Posts: 6,898
  • Joined: 02-June 10

Re: Problem With Putting Image In JPanel

Posted 05 February 2012 - 04:07 PM

Well, yah.. Where did you define it? INSIDE ImageBackground()

As soon as you get outside of ImageBackground it doesn't exist. That includes paintComponent()

Its just like the example below:

void One()
{
   int x = 5;
}

void Two()
{

   int Y = x + 2; // FAIL 
}


x only exists within the scope of One()... Two() has no idea what x is.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1