java swing image slide show
this image slider is coded with java swing API.its a very basic image slider.but it use some advanced java functions to make it work better.
opening DIR
opening DIR
if (chooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
if (chooser.getSelectedFile().isDirectory()) {
filesInDirectory = chooser.getSelectedFile().listFiles();
}
} else {
System.out.println("No Selection ");
}
play
if (chooser.getSelectedFile().isDirectory()) {
filesInDirectory = chooser.getSelectedFile().listFiles();
}
} else {
System.out.println("No Selection ");
}
play
if (filesInDirectory == null) {
return;
}
for (File file : filesInDirectory) {
if (file.getName().toLowerCase().endsWith(".jpg") || file.getName().toLowerCase().endsWith(".png")) {
imagelist.add(file.getName());
} else {
System.out.println("nope!");
}
}
jLabel3.setText("" + filesInDirectory.length);
loadimg();
return;
}
for (File file : filesInDirectory) {
if (file.getName().toLowerCase().endsWith(".jpg") || file.getName().toLowerCase().endsWith(".png")) {
imagelist.add(file.getName());
} else {
System.out.println("nope!");
}
}
jLabel3.setText("" + filesInDirectory.length);
loadimg();
loadimg
public void loadimg() {
SwingWorker<Void, Void> worker = new SwingWorker<Void, Void>() {
@Override
protected Void doInBackground() throws Exception {
int i;
for (i = 0; i <= imagelist.size(); i++) {
String next = imagelist.get(i);
Thread.sleep(2000);
//System.out.println("" + next);
ImageIcon iconLogo = new ImageIcon("Images/" + next);
Image img = iconLogo.getImage().getScaledInstance(jLabel2.getWidth(), jLabel2.getHeight(), Image.SCALE_SMOOTH);
jLabel2.setIcon(new ImageIcon(img));
System.out.println("" + i);
if (i == imagelist.size() - 1) {
i = 0;
}
}
return null;
}
};
worker.execute();
}
download files : https://github.com/stark9000/java-_simple_slide_show
No comments:
Post a Comment