Search code examples
javagraphicswindowdrawdrawstring

Display many text in same windows java


I want to display many text in the same windows but only the last text is displayed, there is my 2 class :


import Test.Graph;
import javax.swing.JFrame;


public class InterfaceGraphique extends JFrame {

    private static final long serialVersionUID = 1L;


     public InterfaceGraphique() {
        this.setTitle("My first Window");
        this.setSize(800,1000);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setContentPane(new Graph("test 1", 150,300,"erreur"));
        this.setContentPane(new Graph("test 2 ", 250,400,"normal"));
        this.setContentPane(new Graph("test3", 350,500,"valide"));


        this.setVisible(true);


    }

}
import java.awt.Graphics;
import javax.swing.JPanel;
import java.awt.Color;
import java.awt.Font;

public class Graph extends JPanel {
    private static final long serialVersionUID = 1L;
    private String fichier;
    private int x;
    private int y;
    String etat;

    public Graph(String fichier, int x, int y, String etat) {
        this.fichier = fichier;
        this.x = x;
        this.y = y;
        this.etat= etat;
    }

    @Override
    public void paintComponent(Graphics g){

        System.out.println("Je suis exécutée !"); 

        if(etat.contentEquals("erreur")) {
            Font font = new Font("Courier", Font.BOLD, 20);
            g.setFont(font);
            g.setColor(Color.red); 
        }
        if(etat.contentEquals("normal")) {
            Font font = new Font("Courier", Font.BOLD, 20);
            g.setFont(font);
            g.setColor(Color.black); 
        }
        if(etat.contentEquals("valide")) {
            Font font = new Font("Courier", Font.BOLD, 20);
            g.setFont(font);
            g.setColor(Color.green); 
        }
        g.drawString(fichier, x, y);

      }

    public static void main(String[] args){
        @SuppressWarnings("unused")
        InterfaceGraphique ig = new InterfaceGraphique();



    }
}

When I compile the test class I have a window with only displayed "test 3" in green. Thanks for help and sorry for bad english. PS: I'm new in JAVA especially in GUI so you can tell me other error to fix thanks!


Solution

  • Modified the InterfaceGraphique.java

    package com.java.graphics;
    
    import java.io.File;
    import java.io.IOException;
    
    import javax.swing.JFrame;
    
    public class InterfaceGraphique extends JFrame{
    
        private static final long serialVersionUID = 1L;
    
    
        public InterfaceGraphique() {
            System.out.println("Craetion");
    
            this.setTitle("My first Window");
            this.setSize(800, 1000);
            this.setLocationRelativeTo(null);
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            // First Grapth Object
            Graph graph = new Graph();
    
            try {
                parcourir2("./Test", graph, new Integer(150));
            } catch (IOException e) {
                e.printStackTrace();
            }       
            this.setContentPane(graph);
    
            this.setVisible(true);
        }
    
        public static  int parcourir2 (String Path, Graph graph, Integer xValue) throws IOException {
    
                File repertoire = new File(Path);
                File[] liste = repertoire.listFiles(); 
                if (liste != null) {         
                    for (int i = 0; i < liste.length; i++) {    
                        if (liste[i].isDirectory()) {
                            xValue = parcourir2(liste[i].getAbsolutePath(), graph, xValue);
                        }
    
                        if(liste[i].isFile()) {
                            Point nom = new Point(liste[i].getName(),xValue, 150,"normal");
                            graph.addPoint(nom);
                           }
    
                        xValue = xValue + 50;
    
                    }
                } else {
                    System.err.println( "Nom de repertoire invalide");
                }
    
                return xValue;
    
            }
    }
    

    output: enter image description here