1

I have an paneel.java file which looks like the following code:

import java.awt.*;
import javax.swing.*;

public class Paneel extends JFrame
{
    public static void main ( String [] args )
    {
        // frame
        JFrame frame = new Paneel();
        frame.setSize ( 1000, 1000 );
        frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        frame.setTitle( "Remembory" );
        frame.setVisible( true );
    }
    
    class Gifpaneel extends JPanel{
        private ImageIcon gif, animatedGif;
        
        public Gifpaneel() {
            gif = new ImageIcon( "test.gif" );
            animatedGif = new ImageIcon( "animaties/test.gif" );
        }       
        
        public void paintComponent( Graphics g ){
            super.paintComponent( g );
            
            gif.paintIcon( this, g, 100, 100 );
            animatedGif.paintIcon ( this, g, 250, 100 );
        }
        
    }
}

I would like to show the test.gif file. How do I get this done? because when I run it in eclipse I only get the jframe with no image in it.

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Rene
  • 13,299
  • 5
  • 19
  • 28
  • 4
    You haven't added created a Gifpaneel object and added it to your JFrame. – Simon Forsberg Sep 18 '13 at 11:35
  • how do i get this done? – Rene Sep 18 '13 at 11:38
  • 2
    Please have a look at how to [add images to Eclipse Project](http://stackoverflow.com/a/9278270/1057230), and this [answer](http://stackoverflow.com/a/9866659/1057230) for more information. Hope it helps :-) – nIcE cOw Sep 18 '13 at 11:59

4 Answers4

14

Use this

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
3

you need to set a file path to the image..something like this

final ImageIcon icon = new ImageIcon("C:\\Users\\you\\Desktop\\test.gif");
javawocky
  • 899
  • 2
  • 9
  • 31
  • 1
    Although a good suggestion, that's not the big issue here. If the absolute path is not set, it will use a path relative to where it is being executed from. – Simon Forsberg Sep 18 '13 at 12:53
2
public static void main ( String [] args )
{
    // frame
    JFrame frame = new Paneel();
    frame.setSize ( 1000, 1000 );
    frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    frame.setTitle( "Remembory" );

    // Add following
    GifPaneel gifpan = new GifPaneel();
    gifpan.repaint();
    frame.add(gifpan);


    frame.setVisible( true );
}
TheKojuEffect
  • 20,103
  • 19
  • 89
  • 125
  • 1
    Please do not call `paintComponent(...)` explicitly, that is not the job of the programmer, just call `gifpan.repaint()` which will call `paintComponent(...)` implicitly :-) – nIcE cOw Sep 18 '13 at 12:43
  • 1
    Actually, in this specific case, you are not even suppose to call `repaint()` as well, since `frame.setVisible(true)` will do that implicitly :-) For the rest You're MOST WELCOME and KEEP SMILING :-) – nIcE cOw Sep 18 '13 at 12:46
-1

create package named as images on your project file and import image in that perticular package. Now, take a lable and select the icon property and select image from class path.

theAlse
  • 5,577
  • 11
  • 68
  • 110
  • FYI, Stackoverflow understands English only! – Paresh Mayani Aug 23 '14 at 06:52
  • This answer would hugely benefit from some example code. As is... this looks like a valid attempt at answering the question, but it isn't a very good attempt, so I wouldn't expect anyone to upvote it. Even so, it is an attempt, so I'm marking it as "Looks OK" in response to a flag someone raised suggesting that it should be deleted. – ArtOfWarfare Oct 23 '14 at 18:54