Con questo codice riesco a mandare in loop una sequenza di immagini.
Devo scrivere però scrivere il percorso di ogni immagine ma siccome ne ho centinaia come posso fare a dire parti della prima e vai fino alla fine? Grazie
codice:
package PACK;
import java.awt.Color;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class Project extends JFrame{
JLabel pic;
Timer tm;
int x = 0;
//Images Path In Array
String[] list = {
"C:/Users/Desktop/JAVAnew/SEQUENZAimmagini/SQ1/CAMION0018.png",
"C:/Users/Desktop/JAVAnew/SEQUENZAimmagini/SQ1/CAMION0019.png",
"C:/Users/Desktop/JAVAnew/SEQUENZAimmagini/SQ1/CAMION0020.png",
"C:/Users/Desktop/JAVAnew/SEQUENZAimmagini/SQ1/CAMION0021.png",
"C:/Users/Desktop/JAVAnew/SEQUENZAimmagini/SQ1/CAMION0022.png",
"C:/Users/Desktop/JAVAnew/SEQUENZAimmagini/SQ1/CAMION0023.png"
};
public Project(){
super("Java SlideShow");
pic = new JLabel();
pic.setBounds(40, 30, 700, 300);
//Call The Function SetImageSize
SetImageSize(5);
//set a timer
tm = new Timer(40,new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
SetImageSize(x);
x += 1;
if(x >= list.length )
x = 0;
}
});
add(pic);
tm.start();
setLayout(null);
setSize(800, 400);
getContentPane().setBackground(Color.decode("#000000"));
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
//create a function to resize the image
public void SetImageSize(int i){
ImageIcon icon = new ImageIcon(list[i]);
Image img = icon.getImage();
Image newImg = img.getScaledInstance(pic.getWidth(), pic.getHeight(), Image.SCALE_SMOOTH);
ImageIcon newImc = new ImageIcon(newImg);
pic.setIcon(newImc);
}
public static void main(String[] args){
new Project();
}
}