2

i having problem with draw image in applet. i want to display all images one by one in applet but it shows only last image of the folder my code is given below.

public class ImageInSwingTest extends JApplet
{
    String filePath="C:\\Users\\yogi\\Pictures\\pictures"; //all .png files more than 200 files
    String files;
    File folder=new File(filePath);
    File[] listOfFiles;
    Image m;

    @Override
    public void init()
    {
        listOfFiles=folder.listFiles();
        for(int i=0;i<listOfFiles.length;i++)
        {
            if(listOfFiles[i].isFile())
            {
                files=listOfFiles[i].getName();
                if(files.endsWith(".png"))
                {
                    String filepath=listOfFiles[i].getAbsolutePath();
                    System.out.println(filepath);
                    try {
                         m = ImageIO.read(new File(filepath));
                        paint(ImageInSwingTest.this.getGraphics());

                    } catch (IOException ex) {
                        Logger.getLogger(ImageInSwingTest.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            }

        }
    }
    public void paint(Graphics g)
    {
        g.drawImage(m, 0, 0, this);

    }

}

so, please can any one tell me how can i display all images one by one.? Thanks.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
yogesh patel
  • 375
  • 3
  • 9
  • 20
  • `String filePath="C:\\Users\\yogi\\Pictures\\pictures";` This will not work for your images on the server, even in a trusted applet. It will be necessary for either the server to supply a file list on demand, or for the file names to be defined as parameters in the applet element. – Andrew Thompson May 24 '12 at 09:04

2 Answers2

2

so, please can any one tell me how can i display all images one by one.? Thanks.

  • because each of loop inside for(int i=0;i<listOfFiles.length;i++) replacing JApplet's contents

  • put JPanel to the JApplet

  • put Images to the array of Icon[]

  • put Icon to the JLabel

  • use GridLayout for placing JLabel with Icons to the JPanel

  • don't paint to the JApplet directly, use JPanel or JComponent with override method paintComponent() instead of paint()

EDIT

actually i want all images to replace each other so all images are look like playing movie

  • you have to pause this process by Thread.sleep(int);

  • you can use Thread.sleep(int); inside SwingWorker or Runnable#Thread,

  • don't use Thread.sleep(int); other ways in the Swing GUI, because to block Event Dispatch Thread, and is possible that nothing will be painted or only the last image too

  • Runnable#Thread would be better and easiest, but any output from Runnable#Thread to the Swing GUI you have to wrap JLabel.setIcon(myIcon) to the invokeLater

mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • actually i want all images to replace each other so all images are look like playing movie. – yogesh patel May 23 '12 at 10:00
  • 2
    Also consider using a [*Swing Timer*](http://docs.oracle.com/javase/tutorial/uiswing/misc/timer.html). – trashgod May 23 '12 at 10:13
  • @yogeshpatel : Have a look at this example of [Slide Show](http://stackoverflow.com/questions/9630724/not-redisplaying-an-image-s/9631116#9631116) – nIcE cOw May 23 '12 at 13:37
0

The time taken to end the loop is so short you can only see the last image displayed. Try adding a pause after drawing each image.

mastaH
  • 1,234
  • 3
  • 15
  • 30