Bonjour,
Je voudrais utiliser un listener.
J'ai fait un programme qui affiche une image. Quand on appuie sur un bouton, on trace un rectangle sur l'image.
J'arrive a avoir une fenetre avec une image, mais quand j'appuie sur le bouton il ne se passe rien, et visiblement j'ai un pointeur null
j'ai beau chercher, je ne vois pas où est ce que je pointe sur une objet vide ??
j'ai dû passer à coté de quelque chose...
si vous pouviez m'indiquer ce que j'ai oublié ?
merci
Image
Modif
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 import javax.swing.* ; import java.awt.event.* ; import java.awt.* ; public class Image implements ActionListener { private JFrame fen = new JFrame("Image"); private JPanel panneauPrinc = new JPanel(new BorderLayout()); private JPanel panneauDessous = new JPanel(new FlowLayout()); private JButton dessine = new JButton("Dessine"); private Modif panneauImageAModifier = new Modif(); public static void main(String[] args) { new Image(); } private Image() { panneauDessous.add(dessine); panneauPrinc.add(panneauDessous,BorderLayout.SOUTH); panneauPrinc.add(panneauImageAModifier,BorderLayout.CENTER); dessine.addActionListener (this); //fait le lien avec ActionPerformed fen.setContentPane(panneauPrinc); fen.pack(); fen.setVisible(true); } public void actionPerformed(ActionEvent evt) { if ( evt.getSource()==dessine ) panneauImageAModifier.dessiner(); } }
et voilà la liste de mes erreurs
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 import javax.swing.* ; import java.awt.* ; public class Modif extends JPanel { public static Graphics gc; public void paintComponent(Graphics gc) { Toolkit toolkit = getToolkit(); //environnement outil gc.drawImage(toolkit.getImage("image.gif"), 0,0,this); //dessin de Duke } public void dessiner() { gc.setColor(Color.blue); gc.drawRect(20,20,15,15); } }
comment puis je avoir autant d'erreur ?Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Modif.dessiner(Modif.java:17)
at Image.actionPerformed(Image.java:35)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Partager