1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
| /*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Rihab
*/
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.*;
import javax.imageio.ImageIO;
import javax.swing.event.*;
import javax.swing.tree.*;
public class Arbres extends JFrame implements TreeSelectionListener {
private JTree arbre;
private DefaultMutableTreeNode racine;
private DefaultMutableTreeNode images;
private DefaultMutableTreeNode autre;
private Vue vue = new Vue();
private String répertoire = "C:/Photos/";
private void ajouterFichiers() {
File fichiers = new File(répertoire);
ArrayList<String> liste = new ArrayList<String>();
for (String nom : fichiers.list()) liste.add(nom);
Enumeration recherche = images.children();
while (recherche.hasMoreElements()) {
DefaultMutableTreeNode noeud = (DefaultMutableTreeNode) recherche.nextElement();
for (String nom : liste) {
String extension = nom.split("\\.")[1];
if (extension.equalsIgnoreCase(noeud.toString()))
noeud.add(new DefaultMutableTreeNode(nom, false));
}
}
for (DefaultMutableTreeNode noeud = images.getFirstLeaf(); noeud !=null; noeud = noeud.getNextLeaf())
liste.remove(noeud.toString());
for (String nom : liste) autre.add(new DefaultMutableTreeNode(nom, false));
}
private void construireArbre() {
racine = new DefaultMutableTreeNode("Fichiers", true);
images = new DefaultMutableTreeNode("Images", true);
DefaultMutableTreeNode jpeg = new DefaultMutableTreeNode("JPG", true);
DefaultMutableTreeNode gif = new DefaultMutableTreeNode("GIF", true);
DefaultMutableTreeNode png = new DefaultMutableTreeNode("PNG", true);
autre = new DefaultMutableTreeNode("Autre", true);
racine.add(images);
racine.add(autre);
images.add(jpeg);
images.add(gif);
images.add(png);
arbre = new JTree(racine, true);
arbre.setPreferredSize(new Dimension(180, 1000));
arbre.addTreeSelectionListener(this);
ajouterFichiers();
}
private class Vue extends JComponent
{
private BufferedImage photo;
private double ratio;
@Override
protected void paintComponent(Graphics g) {
if (photo!=null) g.drawImage(photo, 0, 0, getWidth(), (int)(getWidth()/ratio), null);
}
public void setPhoto(File fichier) {
try {
photo = ImageIO.read(fichier);
ratio = (double)photo.getWidth() / photo.getHeight();
repaint();
}
catch (IOException ex) { setTitle("Impossible de lire le fichier");}
}
}
public void valueChanged(TreeSelectionEvent e) {
if (arbre.getSelectionPath()!=null) {
String nom = arbre.getSelectionPath().getLastPathComponent().toString();
vue.setPhoto(new File("C:/Photos/"+nom));
}
}
public Arbres() {
super("Images");
construireArbre();
add(new JScrollPane(arbre), BorderLayout.WEST);
add(vue);
setSize(540, 300);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) { new Arbres(); }
} |
Partager