Reputation: 375
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.
Upvotes: 2
Views: 1480
Reputation: 109815
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
Upvotes: 2
Reputation: 1267
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.
Upvotes: 0