Galerie photo

Résolu
potentielJava Messages postés 2 Date d'inscription vendredi 2 juin 2017 Statut Membre Dernière intervention 3 juin 2017 - Modifié le 2 juin 2017 à 22:40
potentielJava Messages postés 2 Date d'inscription vendredi 2 juin 2017 Statut Membre Dernière intervention 3 juin 2017 - 3 juin 2017 à 08:14
j'aimerai pouvoir afficher tous les images dans un Jlabel voici ma méthode qui ne fonctionne pas merci

public class Photo1 extends JFrame{

    private JPanel panel = new JPanel()  ;
    File sourceimage = new File("./Images");
    String [] listImg = sourceimage.list();
    ImageIcon img = new ImageIcon(sourceimage + listImg[0]);
   
   
    JLabel lblImage ;
        public Photo1 () {
       
        for (int i = 0; i <  listImg.length; i++) {
           
            JLabel lblImage = new JLabel(img);
            panel.add(lblImage);
        }
        panel.setLayout(new GridLayout(0, 3));
       add(panel,BorderLayout.CENTER);
       
          }

1 réponse

KX Messages postés 16733 Date d'inscription samedi 31 mai 2008 Statut Modérateur Dernière intervention 31 janvier 2024 127
2 juin 2017 à 23:22
Bonjour,

Voici un exemple :

import java.awt.GridLayout;
import java.awt.MediaTracker;
import java.io.File;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class Photo1 extends JFrame {

    private static final long serialVersionUID = 1L;

    public Photo1(File path) {
        File[] files = path.isDirectory() ? path.listFiles() : new File[] { path };

        List<JLabel> labels = Arrays.stream(files).parallel() //
                .map(File::getAbsolutePath) //
                .map(ImageIcon::new) //
                .filter(icon -> icon.getImageLoadStatus() == MediaTracker.COMPLETE) //
                .map(JLabel::new) //
                .collect(Collectors.toList());

        int dim = (int) Math.ceil(Math.sqrt(labels.size()));
        setLayout(new GridLayout(dim, dim));

        labels.forEach(this::add);

        setSize(800, 600);
        setExtendedState(MAXIMIZED_BOTH);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);
    }

    public static void main(String[] args) {
        new Photo1(new File("./Images"));
    }
}
1
KX Messages postés 16733 Date d'inscription samedi 31 mai 2008 Statut Modérateur Dernière intervention 31 janvier 2024 127
3 juin 2017 à 00:21
Remarque : si on veut conserver les dimensions d'origine des images on peut surcharger le JLabel pour considérer à la volée la taille de la fenêtre et faire le redimensionnement de l'image lorsqu'elle est redessinée.

Exemple en remplaçant le
.map(JLabel::new)
de la ligne 23 par :

.map(icon -> new JLabel(icon) {
    public void paint(Graphics g) {
        draw(g, icon);
    }
})

Et en rajoutant la méthode
draw(g, icon)
à la classe Photo1 :

private void draw(Graphics g, ImageIcon icon) {
    GridLayout grid = (GridLayout) getContentPane().getLayout();
    int columnWidth = getWidth() / grid.getColumns();
    int rowHeight = getHeight() / grid.getRows();
    double iconWidth = icon.getIconWidth();
    double iconHeight = icon.getIconHeight();
    double ratio = Math.max(iconWidth / columnWidth, iconHeight / rowHeight);
    g.drawImage(icon.getImage(), 0, 0, (int) (iconWidth / ratio), (int) (iconHeight / ratio), this);
}
0
potentielJava Messages postés 2 Date d'inscription vendredi 2 juin 2017 Statut Membre Dernière intervention 3 juin 2017
Modifié le 3 juin 2017 à 08:47
Merciiiii c'est top
0
Rejoignez-nous