1

I use NetBeans and I wanted to show an image on a jPanel (basically to make it scroll). I wrote this code

    Graphics g=jPanelScrolling.getGraphics();
    File fileBackground = new File("background.jpg");
    Image background;
    try{
        background=ImageIO.read(fileBackground);
        final int WIDTH=background.getWidth(rootPane);            
        final int HEIGHT=background.getHeight(rootPane);
        g.drawImage(background, WIDTH, HEIGHT, rootPane);
    }
    catch(IOException e){
        background=null;
        jPanelScrolling.setBackground(Color.red);  //to test if the image has been succesfully uploaded
    }

but when I execute it, it shows me only the void jPanel

How can I make it work?

Giorgio Arena
  • 81
  • 2
  • 3
  • 2
    Please make sure, that the image is itself accessible. Please have a look at this [answer](http://stackoverflow.com/a/9866659/1057230), regarding how to add images to a Java Project. Hope it will help :-) Moreover, don't create `Graphics` object by yourself, use the one provided by `Swing` itself, if you override `paintComponent` method of any `JComponent`. Make sure you override `getPreferredSize()` too for the said `JComponent` whose `paintComponent` is being overridden, since some Layout Managers tend to give 0, 0 sizes instead :( – nIcE cOw Feb 23 '14 at 16:25

2 Answers2

1

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) throws IOException {
        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);
    }
}
Rakesh KR
  • 6,357
  • 5
  • 40
  • 55
1

Want to display Image, try something like this:

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.*;

public class ImagePanel extends JPanel{
    private BufferedImage bi;

    public ImagePanel() {  

      try {
            bi = ImageIO.read(new File("Your Image Path"));
        } catch (IOException ex) {
            Logger.getLogger(ImagePanel.class.getName()).log(Level.SEVERE, null, ex);
        }


        final JPanel panel = new JPanel(){
            @Override
            protected void paintComponent(Graphics g){
                Graphics g2 = g.create();
                g2.drawImage(bi, 0, 0, getWidth(), getHeight(), null);
                g2.dispose();
            }

            @Override
            public Dimension getPreferredSize(){
                return new Dimension(bi.getWidth()/2, bi.getHeight()/2);
                //return new Dimension(200, 200);
            }
        };

        add(panel);
    }  

        public static void main(String args[]){
            SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                ImagePanel imgPanel=new ImagePanel();
                JOptionPane.showMessageDialog(
                        null, imgPanel, "Image Panel", JOptionPane.PLAIN_MESSAGE);
            }
        });
        }
}

Output

Image

ravibagul91
  • 20,072
  • 5
  • 36
  • 59