Bonjour,

je rencontre un probleme pour faire afficher au lancement de mon appli un graphique, je n'y arrive qu'en cliquant sur l'unique JButton du programme, sinon le graphique reste blanc :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
 
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                            
 
paintComponent(jGraph1.getGraphics());
 
}
où la fonction paintComponent est la suivante :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
 
public void paintComponent(Graphics g) {
 
g2 = (Graphics2D) g;
int i_space,j_space;
double x,y,t,d,scale_x,scale_y;
double xc[]= new double[3];
double yc[]= new double[3];
 
i_space=0;
j_space=0;
scale_x=380/8;
scale_y=scale_x;
x=4*scale_x;
for(y=0;y<=380;y=y+2)
{
if (i_space==0){    
g2.drawLine((int)x,(int)y,(int)x,(int)(y+2));
i_space=1;
} else {i_space=0;}
}
y=4*scale_y;
for(x=0;x<=380;x=x+2)
{
if (j_space==0){    
g2.drawLine((int)x,(int)y,(int)(x+2),(int)y);
j_space=1;
} else {j_space=0;}
}
 
d=2.5;
xc[0]=-d*Math.sqrt(3)/6;
yc[0]=d/2;
xc[1]=d*Math.sqrt(3)/3;
yc[1]=0;
 
 
 
xc[2]=-d*Math.sqrt(3)/6; 
yc[2]=-d/2;
 
g2.drawOval((int)((4+(xc[0]-1))*scale_x),(int)((4-(yc[0]+1))*scale_y),2*(int)scale_x,2*(int)scale_y);
g2.drawOval((int)((4+(xc[1]-1))*scale_x),(int)((4-(yc[1]+1))*scale_y),2*(int)scale_x,2*(int)scale_y);
g2.drawOval((int)((4+(xc[2]-1))*scale_x),(int)((4-(yc[2]+1))*scale_y),2*(int)scale_x,2*(int)scale_y);
 
     }
Cette fonction trace un repère (x,y) et 3 cercles équidistants.

En mettant :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
 
public class NewJFrame extends JFrame {
 
 
    public NewJFrame() {
        super();        
        initComponents();
        paintComponent(jGraph1.getGraphics());        
 
    }
 
 
....
}
ça compile bien mais au moment de l'exécution de la gui, on dirait que l'appel de "paintComponent(jGraph1.getGraphics())" ne fait rien.

Si je déplace cet appel dans la méthode initComponents(), j'obtiens l'erreur suivante:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at NewJFrame.paintComponent(NewJFrame.java:59)
at NewJFrame.initComponents(NewJFrame.java:125)
at NewJFrame.<init>(NewJFrame.java:34)
at NewJFrame$3.run(NewJFrame.java:235)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
at java.awt.EventQueue.dispatch
Event(EventQueue.java:597)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)

J'ai les variables private suivantes:


Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
 
 
    private javax.swing.JButton jButton1;
    private org.jgraph.JGraph jGraph1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    private java.awt.Graphics2D g2;
J'utilise NetBeans 6.1 pour réaliser cette gui. La méthode initComponents() est générée automatiquement à partir du design :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
 
private void initComponents() {
 
        jButton1 = new javax.swing.JButton();
        jTextField1 = new javax.swing.JTextField();
        jTextField2 = new javax.swing.JTextField();
        jGraph1 = new org.jgraph.JGraph();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();        
        jGraph1.setModel(null);
 
 
 
 
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setAlwaysOnTop(true);
 
        jButton1.setText("start");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
 
.......
}
Le main est le suivant:

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
 
public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                new NewJFrame().setVisible(true);
 
            }
        });
 
    }
Si quelqu'un pouvait voir ce qui ne va pas pour faire afficher dès le lancement ce graphique sans à avoir à appuyer sur le JButton1, ça serait sympa.

Merci d'avance pour toute réponse.