-2

i am trying for about two hours to show a 800*600 "Background Picture" on my Jpanel, it simply wont show up, whats wrong over here?

And by the way: i want to change my Background sometimes while the application is running, shouldnt i use another method for that than the main? If so, how could that Method look like? I still get Errors like "cant make static reference..."

public class Start {

    public static void main(String[] args) {
        JFrame frame = new JFrame("Abstieg");
        JPanel panel = new JPanel();
        frame.add(panel);
        frame.setSize(800, 600);
        frame.setResizable(false);
        frame.setLocationRelativeTo ( null );
        frame.setUndecorated(true);


        ImageIcon background = new ImageIcon("Title.png");
        JLabel label = new JLabel();
        label.setBounds(0, 0, 0, 0);
        label.setIcon(background);
        panel.setLayout(null);
        panel.add(label);

        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
user3808217
  • 135
  • 3
  • 12
  • "cant make static reference..." errors occours if you call a non static function from a static function. -> simply add "static" to the new method header (like "public static void changeBackground(Jpanel panel, ImageIcon img){...}") – Fabian N. Jul 05 '14 at 18:20
  • you set the width and height of the image to 0 and you wonder why it doesn't show up? – Harry Blargle Jul 05 '14 at 18:33
  • Hopefully this [answer](http://stackoverflow.com/a/9866659/1057230), might be able to help in showing an image on the `JPanel/JComponent`. For changing the image at runtime, might be this [answer](http://stackoverflow.com/a/9631116/1057230), be able to help. – nIcE cOw Jul 06 '14 at 01:57

2 Answers2

0

Missing resources such as images are a common problem. Are you sure "Title.png" (case sensitive) is also available in your classpath?

Place this method in your class

   private Image requestImage() {
        Image image = null;

        try {
            image = ImageIO.read(new URL("http://www.johnlennon.com/wp-content/themes/jl/images/home-gallery/2.jpg"));
        } catch (IOException e) {
            e.printStackTrace();
        }

        return image;
    }

and call

label.setIcon(requestImage());

If this works then your Title.png is not being found. (Might be good practice to make all your image file resources all lower case.)

Also, setting and image on a JLabel is OK if you want the image to be a part of your scene. However for background images this is a preferred approach:

JPanel panel = new JPanel() {
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawImage(image, 0, 0, null);
    }
};
javajon
  • 1,660
  • 16
  • 18
0

Try:

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.*;

public class ImageInFrame {
    public static void main(String[] args){
      try{
        String path = "Image1.jpg";
        File file = new File(path);
        BufferedImage image = ImageIO.read(file);
        JLabel label = new JLabel(new ImageIcon(image));
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.getContentPane().add(label);
        f.pack();
        f.setLocation(200,200);
        f.setVisible(true); 
     catch(Exception e){
       e.printStackTrace();
     }

    }
}
Rakesh KR
  • 6,357
  • 5
  • 40
  • 55