Any one knows how to draw a Tree using a Path with Java swing (method paint(), image icon, ...)? I must draw a Tree from an existing Path.
This is my Tree (= "Arbre") class code for the recursive tree and the Test class, but I have to draw it.
class Arbre:
package org.mql.java.arbre;
import java.io.File;
import java.util.StringTokenizer;
import java.util.Vector;
public class Arbre {
private String name;
private Vector<Arbre> childs;
int niveau = 0;
public Arbre() {
}
public Arbre(String path, Vector<Arbre> childs) {
super();
this.name = path;
this.childs = childs;
}
public String getPath() {
return name;
}
public void setPath(String path) {
this.name = path;
}
public Vector<Arbre> getChilds() {
return childs;
}
public void setChilds(Vector<Arbre> childs) {
this.childs = childs;
}
public String toString() {
return "\nTree [name=" + name + ", childs=" + childs + "]";
}
public void afficher(String path){
afficherWrapper(path, path.split("\\\\").length);
}
public void afficherWrapper(String path , int skip) {
File f = new File(path);
if (f.isDirectory()) {
int niveau = f.getAbsolutePath().split("\\\\").length;
String output = "";
for(int i=0 ; i < niveau-skip ; i++) output += " " ;
output += f.getName() + ">" ;
System.out.println(output);
File[] files = f.listFiles();
for (int i = 0; i < files.length; i++) {
if (files[i].isFile()) {
int niveauFile = files[i].getAbsolutePath().split("\\\\").length;
String outputF = "";
for(int j=0 ; j < niveauFile-skip ; j++) outputF += " " ;
System.out.println(outputF+files[i].getName());
}
if (files[i].isDirectory()) {
this.afficherWrapper(files[i].getPath() ,skip);
}
}
} else {
System.out.println("veuillez entrez le chemin d'un répertoire");
}
}
public void load(String path) {
File f = new File(path);
if (f.isDirectory()) {
this.setPath(f.getAbsolutePath());
this.setChilds(this.getChildren(f));
}
}
public Vector<Arbre> getChildren(File f) {
Vector<Arbre> ts = new Vector<>();
if (f.isDirectory()) {
File[] files = f.listFiles();
for (int i = 0; i < files.length; i++) {
ts.add(new Arbre(files[i].getAbsolutePath(), this.getChildren(files[i])));
}
}
if (f.isFile()) {
}
return ts;
}
public int getChildrenNumber(Arbre t)
{
int total=0;
for (int i = 0; i < t.getChilds().size(); i++) {
total=total+this.getChildrenNumber(t.getChilds().get(i));
}
total=total+t.getChilds().size();
return total;
}
/* public void tester(String path) {
File f = new File(path);
if (f.isDirectory()) {
File[] files = f.listFiles();
for (int i = 0; i < files.length; i++) {
File file = new File(files[i].getAbsolutePath());
System.out.println(file.getName());
this.tester(files[i].getAbsolutePath());
}
}
}
*/
public String getTitle(String path)
{
int i=path.lastIndexOf("\\");
String s=path.substring(i+1, path.length());
return s;
}
}
class Test:
package org.mql.java.arbre;
public class Test {
public Test() {
exp02();
}
private void exp02() {
Arbre Ar = new Arbre();
//Ar.load("E:\\eclipsetmp\\eclipse\\tree");
Ar.afficher("E:\\eclipsetmp\\eclipse\\tree");
}
public void exp01(){
Arbre Ar = new Arbre();
Ar.load("E:\\eclipsetmp\\eclipse\\tree");
System.out.println(Ar);
}
public static void main(String[] args) {
new Test();
}
}