Précédent   Forum du club des développeurs et IT Pro > Java > Interfaces Graphiques en Java > AWT/SWING
AWT/SWING
Forum d'entraide pour les API Swing et AWT. Avant de poster -> FAQ AWT/SWING
Partagez cette discussion sur d'autres réseaux sociaux : Viadeo Twitter Google Facebook Digg Delicious MySpace Yahoo
Réponse
 
Outils de la discussion
Publicité
'
Vieux 11/12/2012, 21h49   #1
Bullit84
Futur Membre du Club
 
Homme Pierre
Étudiant
Inscription : décembre 2009
Messages : 42
Détails du profil
Informations personnelles :
Nom : Homme Pierre
Âge : 23
Localisation : France

Informations professionnelles :
Activité : Étudiant
Secteur : High Tech - Multimédia et Internet

Informations forums :
Inscription : décembre 2009
Messages : 42
Points : 16
Points : 16
Par défaut Mise à jour d'un JPanel

Bonsoir à tous !

Je sais que des soucis de ce genre ont été mainte fois postés, mais je fouine sur internet depuis pas mal d'heure maintenant et je commence à devenir fou ...

Au lancement de l'app, mon JPanel est vide, il n'y a que ses contours. Mais après avoir cliqué sur le bouton "Démarrer" mon algo se lance (voyageur de commerce par algo génétique). Dans mon JPanel devrait alors s'afficher le graphe de mon parcours le plus court mais mon JPanel reste vierge !

J'appelle dans ma classe main le setter modifié pour donner un nouveau tableau à Tab_Trajet (instancié au début à null). Et ensuite le repaint() qui je pensais mettrais à jour mon JPanel suivant le nouveau Tab_Trajet masi quedal !

Code :
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
 
package view;
 
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.BorderFactory;
import javax.swing.JPanel;
 
public class panelGraphe extends JPanel
{
    private int[] Tab_Trajet = null; // = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13 ,14} 
    private int[][] Tab_Coord;
 
    public panelGraphe(JPanel panelGraphe)
    {               
        //Panel du Graphe
        this.setBorder(BorderFactory.createLineBorder(Color.BLACK));
        this.setPreferredSize(new Dimension(900, 500));
        this.setMaximumSize(new Dimension(900, 500));
        this.setMinimumSize(new Dimension(900, 500));
    }       
 
    @Override
    public void paint(Graphics g)
    {           
        if(Tab_Trajet != null)
        {
            super.paint(g);
            Tab_Coord = new int[Tab_Trajet.length][2];
            int i = 1, j = 1;
            int x = 50; int y = 50;
            int nb_bloc = (Tab_Trajet.length-1) / 5;
            int size_y = 400 / nb_bloc;
            String point;
 
            while(j <= Tab_Trajet.length)
            {
                point = Integer.toString(j);
                g.drawString(point, x, y-8);
                g.fillRect(x, y, 6, 6);
                Tab_Coord[j-1][0] = x;
                Tab_Coord[j-1][1] = y;
                x += 200;
 
                if(i == 5)
                {
                    y += size_y;
                    x = 50;
                    i = 0;
                }
                i++; j++;
            }
 
            j = 1;
            int X1, Y1, X2, Y2;
            while(j < Tab_Trajet.length)
            {
                X1 = Tab_Coord[j-1][0]; Y1 = Tab_Coord[j-1][1];
                X2 = Tab_Coord[j][0]; Y2 = Tab_Coord[j][1];
                g.drawLine(X1, Y1, X2, Y2);
                j++;
            }
 
            X2 = Tab_Coord[Tab_Trajet.length-1][0];
            Y2 = Tab_Coord[Tab_Trajet.length-1][1];  
            g.setColor(Color.RED);
 
            g.drawLine(50, 50, X2, Y2);
        }
    }
 
    public void setTab_Trajet(int[] Tab_Trajet)
    {
        this.Tab_Trajet = Tab_Trajet;
        this.removeAll();
        this.validate();
        this.repaint();
    }
}
Bullit84 est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 12/12/2012, 14h52   #2
gandalf72000
Nouveau Membre du Club
 
Homme
Développeur Java
Inscription : janvier 2011
Messages : 34
Détails du profil
Informations personnelles :
Sexe : Homme
Localisation : France, Indre et Loire (Centre)

Informations professionnelles :
Activité : Développeur Java

Informations forums :
Inscription : janvier 2011
Messages : 34
Points : 30
Points : 30
il manque peut-être un

?
gandalf72000 est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 12/12/2012, 17h49   #3
sinok
Modérateur
 
Avatar de sinok
 
Inscription : août 2004
Messages : 8 644
Détails du profil
Informations personnelles :
Âge : 33
Localisation : France, Paris (Île de France)

Informations forums :
Inscription : août 2004
Messages : 8 644
Points : 12 452
Points : 12 452
Je dirais plutôt ton algo de dessin où le contenu de Tab_Trajet n'a aucun impact. Tu utilises uniquement la taille de Tab_Trajet dans ton paint.

De plus, il est amplement recommandé en Swing d'étendre la méthode protected paintComponent et non paint pour dessiner de sur un composant, la méthode paint s'occupant plus que du dessiner du composant en lui même.


Sinon, essaie de suivre les conventions de code java histoire de permettre une lecture facilité aux gens qui t'aident


ftp://ftp-developpez.com/cyberzoide/java/JavaStyle.pdf
__________________
Hey, this is mine. That's mine. All this is mine. I'm claiming all this as mine. Except that bit. I don't want that bit. But all the rest of this is mine. Hey, this has been a really good day. I've eaten five times, I've slept six times, and I've made a lot of things mine. Tomorrow, I'm gonna see if I can't have sex with something.
sinok est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 13/12/2012, 18h18   #4
Bullit84
Futur Membre du Club
 
Homme Pierre
Étudiant
Inscription : décembre 2009
Messages : 42
Détails du profil
Informations personnelles :
Nom : Homme Pierre
Âge : 23
Localisation : France

Informations professionnelles :
Activité : Étudiant
Secteur : High Tech - Multimédia et Internet

Informations forums :
Inscription : décembre 2009
Messages : 42
Points : 16
Points : 16
Merci de vos réponses mais j'ai tout repris autrement et ça a fonctionné
Bullit84 est déconnecté   Envoyer un message privé Réponse avec citation 00
Réponse Cette discussion est résolue.
Outils de la discussion

Navigation rapide


Fuseau horaire GMT +2. Il est actuellement 17h30.


 
 
 
 
Partenaires

Hébergement Web