Arbre de decision:Le JTree ne veut pas s'afficher!aidez moi

1 post / 0 new
Cooljava
Arbre de decision:Le JTree ne veut pas s'afficher!aidez moi

J'ai essayé sans cesse de pouvoir afficher le Jtree mais j'arrive pas.J'utilise l'algorithme ID3 de Quilan.
Mon projet contient 5 classes:
-main
-Table
-Window
-ID3
-Noeud

Main.java

package test1;

import javax.swing.JFrame;

public class main
{
public static void main(String[] args)
{
JFrame TPsys = new Window();
TPsys.setVisible(true);
}
}

Window.java

package test1;

import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.Graphics2D;
import java.awt.Graphics;
/*import java.awt.Image;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;*/
import javax.swing.*;

public class Window extends JFrame implements ActionListener {

private static final long serialVersionUID = 1L; //eclipse me met un warning si je le met pas
public String Apprentissage[][] = new String[100][100];
//public String Base_de_test[][]=new String [11][11];
JButton ok, build;
int i = 0;
JTextField nb_att, nb_exp;
String Name_algo;
ID3 idd;
String type[] = {"ID3", "C4.5", "CART", "CHAID"};
Graphics2D surface;
public Table T1 = new Table(0, 0, Apprentissage, "EXEMPLE/ATTRIBUT", true);
//public Table T2=new Table(0,0,Base_de_test,"EXEMPLE/ATTRIBUT",true);
public JTable tab_appr = new JTable(T1);
//public JTable tab_test=new JTable(T2);

public Window() {
super("Window");
getContentPane().setLayout(null);

JScrollPane r1 = new JScrollPane(tab_appr);
r1.setBounds(20, 300, 500, 100);
//JScrollPane r2=new JScrollPane(tab_test);
//r2.setBounds(250, 400, 200, 200);
add(r1);
//add(r2);

ok = new JButton("ok");
build = new JButton("Build");

nb_att = new JTextField("0");
nb_exp = new JTextField("0");

JComboBox Ltype = new JComboBox(type);

this.setLocation(((int) Toolkit.getDefaultToolkit().getScreenSize().getWidth() - 500) / 2,
((int) Toolkit.getDefaultToolkit().getScreenSize().getHeight() - 500) / 2);

this.setSize(700, 700);

nb_att.setBounds(10, 100, 150, 20);
nb_exp.setBounds(10, 150, 150, 20);
ok.setBounds(200, 150, 50, 20);
build.setBounds(200, 100, 80, 20);
Ltype.setBounds(10, 200, 100, 20);

getContentPane().add(ok);
getContentPane().add(build);
getContentPane().add(nb_att);
getContentPane().add(nb_exp);
getContentPane().add(Ltype);
ok.addActionListener(this);
build.addActionListener(this);

Ltype.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent a) {
JComboBox cb = (JComboBox) a.getSource();
Name_algo = (String) cb.getSelectedItem();

}
});

}

public void actionPerformed(ActionEvent e) {
// this.repaint();

if (e.getSource() == ok) {

int lin = Integer.parseInt(nb_exp.getText());
int col = Integer.parseInt(nb_att.getText());
this.T1.lin = lin;
this.T1.col = col;
//this.T2.lin=lin;
//this.T2.col=col;
for (int i = 0; i < lin; i++) {
for (int j = 0; j < col; j++) {
this.Apprentissage[i][j] = "";
//this.Base_de_test[i][j]="0";

}
}

this.T1.fireTableStructureChanged();
//this.T2.fireTableStructureChanged();
//this.printAll(this.surface);

} else if (e.getSource() == build) {
// if (Name_algo.equals("ID3")) {
ID3 idd = new ID3(this);
idd.setVisible(true);

// }
}
}

public int get_nb_att() {
String text = new String();
text = nb_att.getText();
Integer nbb = new Integer(text);
int nb = nbb.intValue();

return (nb);

}

public int get_nb_exp() {
String text = new String();
text = nb_exp.getText();
Integer nbb = new Integer(text);
int nb = nbb.intValue();

return (nb);

}
}

Table.java

package test1;

import javax.swing.table.AbstractTableModel;

public class Table extends AbstractTableModel {

/**
*
*/
private static final long serialVersionUID = 1L;
//Object D[][]={{6,9,1},{2,3,4},{5,6,7}};
public int col;
public int lin;
String[][] T;
String S;
public boolean modif;

public Table(int lin, int col, String[][] T, String S, boolean modif) {
super();
this.lin = lin;
this.col = col;
this.T = T;
this.S = S;
this.modif = modif;

}

public int getColumnCount() {

return col + 1;
}

public int getRowCount() {

return lin;
}

public Object getValueAt(int rowIndex, int columnIndex) {
//fireTableDataChanged();

//return D[rowIndex][columnIndex];
if (columnIndex > 0) {
return this.T[rowIndex][columnIndex - 1];
}
if (S.equals("Apprentissage")) {
return "Exp" + rowIndex;
}
return "Exp" + rowIndex;
}

public void setValueAt(Object val, int row, int col) {
if (col > 0) {
(this.T[row][col - 1]) = val.toString();
}

fireTableDataChanged();
}

public boolean isCellEditable(int row, int col) {
return modif;

}

public String getColumnName(int columnIndex) {
if (columnIndex > 0) {
return "Att" + (columnIndex - 1);
}
return S;
}
}

ID3.java

package test1;
//import java.io.BufferedReader;
//import java.io.File;
//import java.io.FileInputStream;
//import java.io.InputStreamReader;
//import java.util.StringTokenizer;
import java.util.Vector;

import javax.swing.JFrame;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import javax.swing.JScrollPane;

public class ID3 extends JFrame {

private static ID3 instance = null;
private Window win;
private static final long serialVersionUID = 1L; //eclipse me met un warning si je le met pas
private String nomAttributs[] = null; // contient les noms des attributs
private int nbExemples = 0; // nombre d'exemples
private int nbAttributs = 0; // nombre d'attributs
private Vector valeurAttributs[]; // tableau contenant les valeurs des attributs (string)
private Vector Exemples = null; // contient les exemples sous forme de chiffres
private Noeud root = null; // noeud racine
private JPanel jPanel = null;
private JTree arbre = null;

/**
* utilisation du pattern singleton
*
*/
public ID3(Window w) {
super();
win=w;
initialize();
}

public static ID3 getInstance() {
if (instance == null) {
//instance = new ID3();
}
return instance;
}

private void initialize() {

try {
readData();
createArbre();
afficherArbre();
} catch (Exception e1) {
e1.printStackTrace();
}

}

/**
* met la machine en route, cette methode est appelée à chaque changement de selection de fichier d'entrée
* @param fichier
* @throws Exception
*/
/**
* Cette fonction renvoie la valeur numérique correspondant
* à la valeur de l'attribut (string)
* @param attribut
* @param value
* @return
*/
public int valeurAttribut(int attribut, String value) {
int valeur = valeurAttributs[attribut].indexOf(value); //renvoie l'index de l'objet dans le vecteur (-1 si inexistant)
if (valeur < 0) //Value est inexistante dans le vecteur
{
valeurAttributs[attribut].addElement(value);
valeur = valeurAttributs[attribut].size() - 1;
}
return valeur;
}

/**
* Cette fonction ouvre le fichier contenant les exemples et les intègre au programme
* @param nomFichier
* @throws Exception
*/
public void readData() throws Exception {
Exemples = new Vector();
nbExemples = 0;
nbAttributs = 0;

nbAttributs = win.get_nb_att();
nbExemples = win.get_nb_exp();

nomAttributs = new String[nbAttributs];
for (int i = 0; i < nbAttributs; i++) {
nomAttributs[i] = (String) win.tab_appr.getValueAt(0, i+1); //nomAttributs contient le nom de tous les attributs + le comcept cible
}
for (int i = 0; i < nbAttributs; i++)
System.out.println("nom d'attribut"+i+"est"+nomAttributs[i]);

valeurAttributs = new Vector[nbAttributs];
for (int i = 0; i < nbAttributs; i++) {
valeurAttributs[i] = new Vector();
}

/*
* recupération des données
*/
int[] exemple;
//while(true)
//{
exemple = new int[nbAttributs];
String value;

for (int i = 1; i < nbExemples; i++) {
for (int j = 0; j < nbAttributs; j++) {
value = (String) win.tab_appr.getValueAt(i, j+1);
exemple[j] = valeurAttribut(j, value);
System.out.println("exemple"+j+"est"+exemple[j]);
}
Exemples.add(exemple);
}
for (int i = 0; i < nbAttributs; i++)
System.out.println("valeur attribut"+i+"est+"+valeurAttributs[i]);

//}

}

/**
* c'est comme le Port-Salut
*
*/
public void createArbre() {
root = new Noeud(valeurAttributs, Exemples, nbAttributs, nomAttributs);
root.generationArbre();
}

/**
* c'est comme le Port-Salut
*
*/
public void afficherArbre() {
if (arbre != null) {
jPanel.remove(arbre);
}
DefaultMutableTreeNode racine = root.generationAffichage();
arbre = new JTree(racine);
getContentPane().add(arbre,BorderLayout.CENTER);
JScrollPane myScrollpane = new JScrollPane(arbre);

this.setVisible(true);

}

/**
* This method initializes jPanel
*
* @return javax.swing.JPanel
*/
private JPanel getJPanel() {
if (jPanel == null) {
jPanel = new JPanel();
jPanel.setLayout(new BorderLayout());

}
return jPanel;
}
}

Noeud.java

package test1;

import java.util.Vector;

import javax.swing.tree.DefaultMutableTreeNode;

public class Noeud {

private int attribut = -1; // -1 si les fils ont des valeurs differentes
private int valeur = -1; // valeur numérique de l'attribut père, qui enendre ce noeud
private Noeud pere = null; // c'est comme le Port-Salut
private Noeud[] fils = null; // c'est comme le Port-Salut
private String nomAttributs[];
private int nbAttributs = 0;
private Vector valeurAttributs[];
private Vector Exemples = new Vector(); // contient les sous-exemples des exemples du noeud père
// correspondant à la valeur qui engendra ce noeud
private int etiquette = -1; // -1 il ne s'agit pas d'une feuille
// sinon c'est une feuille et etiquette représente la valeur du concept cible

/**
*
* @param attributs2
* @param exemples
* @param attributs
* @param exemples2
*/
public Noeud(Vector[] attributs2, Vector exemples, int attributs, String[] nomAttributs) {
super();
Exemples = exemples;
nbAttributs = attributs;
valeurAttributs = attributs2;
this.nomAttributs = nomAttributs;
}

public DefaultMutableTreeNode generationAffichage() {
DefaultMutableTreeNode noeud = null;
if (pere != null && etiquette != -1) // feuille
{
noeud = new DefaultMutableTreeNode(valeurAttributs[pere.attribut].elementAt(valeur) + " : " +
nomAttributs[nbAttributs - 1] + " = " +
valeurAttributs[attribut].elementAt(etiquette));
return noeud;
}
if (pere != null && etiquette == -1) // noeud central
{
noeud = new DefaultMutableTreeNode(valeurAttributs[pere.attribut].elementAt(valeur) + " : " +
nomAttributs[attribut]);
for (int i = 0; i < fils.length; i++) {
if (fils[i] != null) {
noeud.add(fils[i].generationAffichage());
}
}
return noeud;
}
if (pere == null && etiquette == -1) //racine
{
noeud = new DefaultMutableTreeNode(nomAttributs[attribut]);
for (int i = 0; i < fils.length; i++) {
if (fils[i] != null) {
noeud.add(fils[i].generationAffichage());
}
}
return noeud;
}
return noeud;
}

public void complementNoeud(int valeur, Noeud pere) {
this.valeur = valeur;
this.pere = pere;
}

public void generationArbre() {
Vector sousExemle;
attribut = attributGanant();
if (attribut != -1) {
fils = new Noeud[valeurAttributs[attribut].size()];
for (int i = 0; i < valeurAttributs[attribut].size(); i++) {
sousExemle = sousExemple(attribut, i);
if (sousExemle.size() > 0) {
fils[i] = new Noeud(valeurAttributs, sousExemle, nbAttributs, nomAttributs);
fils[i].complementNoeud(i, this);
fils[i].generationArbre();
}
}

/*
* à partir d'ici les fils sont formés, on regarde s'ils ont la même étique
* si oui alors ce noeud peux devenir une feuille
*/
int previous = -1;
for (int i = 0; i < fils.length; i++) {
if (fils[i] != null && previous == -1) // on recherche la premiere etiquette valide
{
previous = fils[i].etiquette;
}
if (previous != -1 && fils[i] != null && fils[i].etiquette != previous) // on verifie si les noeuds inferieurs ont la même etiquette
{
return;
}
}
if (previous == -1) {
return;
}
//si on arrive jusqu'ici c'est que les noeuds inferieur ne servent à rien, donc on les virre
attribut = nbAttributs - 1;
etiquette = previous;
fils = null;
} else // feuille
{
attribut = nbAttributs - 1;
etiquette = ((int[]) Exemples.elementAt(0))[attribut];
}

}

/**
* Donne un sous ensemble d'exemple pour un attribut et une valeur de cette attribut
* @param pattribut
* @param pvaleur
* @return
*/
public Vector sousExemple(int pattribut, int pvaleur) {
Vector sousExemples = new Vector();
int num = Exemples.size();
for (int i = 0; i < num; i++) {
int[] exemple = (int[]) Exemples.elementAt(i);
if (exemple[pattribut] == pvaleur) {
sousExemples.addElement(exemple);
}
}
return sousExemples;
}

/**
* Determine quel attribut doit figurer dans ce noeud
* @return
*/
public int attributGanant() {
double gain[] = new double[nbAttributs - 1]; //[nbAttributs-1];
int attribut = -1;
double max = -1;
Vector attributeValues = new Vector();
for (int i = 0; i < nbAttributs - 1; i++) {
attributeValues.removeAllElements();
for (int j = 0; j < Exemples.size(); j++) {
attributeValues.addElement(new Integer(((int[]) Exemples.elementAt(j))[i]));
}
gain[i] = gainAttribut(attributeValues);
}
for (int i = 0; i < gain.length; i++) {
if (gain[i] > max) {
max = gain[i];
attribut = i;
}
}
if (max == 0) {
return -1;
}
return attribut;
}

/**
* donne le gain d'un vecteur
* @param data
* @return
*/
public double gainAttribut(Vector data) {
double gain = 0;
double max = data.size();
int nbApparitionTotal = 0;
int i = 0;
double nbApparition = 0;
double proba = 0;
while (nbApparitionTotal < max) {
nbApparition = 0;
for (int j = 0; j < data.size(); j++) {
if (((Integer) data.elementAt(j)).intValue() == i) {
nbApparition++;
nbApparitionTotal++;
}
}
proba = nbApparition / max;
gain += -(proba) * Math.log(proba);
i++;
}
return gain;
}

public int getAttribut() {
return attribut;
}

public Noeud[] getFils() {
return fils;
}

public Noeud getPere() {
return pere;
}

public int getValeur() {
return valeur;
}
}

Merci de m'aider