Graphics et java.lang.NullPointerException

nonoz Messages postés 57 Date d'inscription mercredi 29 janvier 2003 Statut Membre Dernière intervention 23 juin 2004 - 8 avril 2004 à 22:43
safisoft Messages postés 489 Date d'inscription vendredi 30 janvier 2004 Statut Membre Dernière intervention 13 mai 2009 - 9 avril 2004 à 13:35
Donc voila j'ai fait un gridbag dans lequel j'ai mis un jpanel et je souhaiterais pouvoir dessine dans ce jpanel.

lorsque je fais ca:
public void paint (g)
{
g.setColor(Color.red);
g.drawOval(50,20,100,100);
}
j'arrive bien a dessiner un cercle mais ca me cache tout le reste.

comment faire pour dessiner dans le jpanle?
merci

package calculatrice;

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;
import javax.swing.border.*;

public class appletcalculatrice extends JApplet {
  boolean isStandalone = false;
  JPanel PanneauPrincipal = new JPanel();
  GridBagLayout LayoutPanneauPrincipal = new GridBagLayout();
  JButton jButtonPoint = new JButton();
  TitledBorder titledBorder1;
  JButton jButton1 = new JButton();
  JButton jButton0 = new JButton();
  JButton jButton2 = new JButton();
  JButton jButton3 = new JButton();
  JButton jButton4 = new JButton();
  JButton jButton5 = new JButton();
  JButton jButton6 = new JButton();
  JButton jButton7 = new JButton();
  JButton jButton8 = new JButton();
  JButton jButton9 = new JButton();
  JButton jButton10 = new JButton();
  JButton jButton11 = new JButton();
  JButton jButton12 = new JButton();
  JButton jButton13 = new JButton();
  JButton jButton14 = new JButton();
  JButton jButtonCos = new JButton("cos");
  JButton jButtonSin = new JButton("sin");
  JButton jButtonTan = new JButton("tan");
  JTextField jTextField1 = new JTextField();
  JButton jButton15 = new JButton();

  JPanel jPanelEcran= new JPanel();
  Graphics g = jPanelEcran.getGraphics();

  /**Obtenir une valeur de paramètre*/
  public String getParameter(String key, String def) {
    return isStandalone ? System.getProperty(key, def) :
      (getParameter(key) != null ? getParameter(key) : def);
  }

  /**Construire l'applet*/
  public appletcalculatrice() {
  }
  /**Initialiser l'applet*/
  public void init() {
    try {
      jbInit();
    }
    catch(Exception e) {
      e.printStackTrace();
    }
  }
  /**Initialiser le composant*/
  private void jbInit() throws Exception {
    titledBorder1 = new TitledBorder("");
    this.setSize(new Dimension(400,300));
    PanneauPrincipal.setLayout(LayoutPanneauPrincipal);
    PanneauPrincipal.setBackground(Color.lightGray);
    PanneauPrincipal.setMinimumSize(new Dimension(800, 600));
    PanneauPrincipal.setPreferredSize(new Dimension(800, 600));
    jButtonPoint.setMaximumSize(new Dimension(41, 27));
    jButtonPoint.setMinimumSize(new Dimension(41, 27));
    jButtonPoint.setPreferredSize(new Dimension(41, 27));
    jButtonPoint.setActionCommand("jButtonPoint");
    jButtonPoint.setText(".");
    jButton1.setMaximumSize(new Dimension(41, 27));
    jButton1.setMinimumSize(new Dimension(41, 27));
    jButton1.setPreferredSize(new Dimension(41, 27));
    jButton1.setText("1");
    jButton0.setMaximumSize(new Dimension(41, 27));
    jButton0.setMinimumSize(new Dimension(41, 27));
    jButton0.setPreferredSize(new Dimension(41, 27));
    jButton0.setText("0");
    jButton2.setText("2");
    jButton3.setText("3");
    jButton4.setText("4");
    jButton5.setText("5");
    jButton6.setText("6");
    jButton7.setText("7");
    jButton8.setText("8");
    jButton9.setText("9");
    jButton10.setBackground(SystemColor.activeCaption);
    jButton10.setFont(new java.awt.Font("Dialog", 1, 12));
    jButton10.setToolTipText("");
    jButton10.setText("EXE");
    jButton11.setMaximumSize(new Dimension(41, 27));
    jButton11.setMinimumSize(new Dimension(41, 27));
    jButton11.setPreferredSize(new Dimension(41, 27));
    jButton11.setText("*");
    jButton12.setMaximumSize(new Dimension(41, 27));
    jButton12.setMinimumSize(new Dimension(41, 27));
    jButton12.setPreferredSize(new Dimension(41, 27));
    jButton12.setText("/");
    jButton13.setText("+");
    jButton14.setMaximumSize(new Dimension(41, 27));
    jButton14.setMinimumSize(new Dimension(41, 27));
    jButton14.setPreferredSize(new Dimension(41, 27));
    jButton14.setText("-");
    jButtonSin.setPreferredSize(new Dimension(55, 27));
    jButtonTan.setPreferredSize(new Dimension(55, 27));
    jButton15.setBackground(SystemColor.activeCaption);
    jButton15.setFont(new java.awt.Font("Dialog", 1, 12));
    jButton15.setPreferredSize(new Dimension(57, 27));
    jButton15.setText("ON");
    jPanelEcran.setBackground(Color.white);
    jPanelEcran.setBorder(BorderFactory.createEtchedBorder());

//g.setColor(Color.red);
//g.drawOval(50,20,100,100);

    PanneauPrincipal.add(jButtonPoint,                        new GridBagConstraints(2, 6, 1, 1, 0.0, 0.0
            ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(1, 1, 1, 1), 0, 0));
    PanneauPrincipal.add(jButton1,                     new GridBagConstraints(1, 5, 1, 1, 0.0, 0.0
            ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(1, 1, 1, 1), 0, 0));
    PanneauPrincipal.add(jButton0,                   new GridBagConstraints(1, 6, 1, 1, 0.0, 0.0
            ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(1, 1, 1, 1), 0, 0));
    PanneauPrincipal.add(jButton2,                   new GridBagConstraints(2, 5, 1, 1, 0.0, 0.0
            ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(1, 1, 1, 1), 0, 0));
    PanneauPrincipal.add(jButton3,                   new GridBagConstraints(3, 5, 1, 1, 0.0, 0.0
            ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(1, 1, 1, 1), 0, 0));
    PanneauPrincipal.add(jButton4,                  new GridBagConstraints(1, 4, 1, 1, 0.0, 0.0
            ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(1, 1, 1, 1), 0, 0));
    PanneauPrincipal.add(jButton5,                  new GridBagConstraints(2, 4, 1, 1, 0.0, 0.0
            ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(1, 1, 1, 1), 0, 0));
    PanneauPrincipal.add(jButton6,                  new GridBagConstraints(3, 4, 1, 1, 0.0, 0.0
            ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(1, 1, 1, 1), 0, 0));
    PanneauPrincipal.add(jButton7,                  new GridBagConstraints(1, 2, 1, 2, 0.0, 0.0
            ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(1, 1, 1, 1), 0, 0));
    PanneauPrincipal.add(jButton8,                  new GridBagConstraints(2, 2, 1, 2, 0.0, 0.0
            ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(1, 1, 1, 1), 0, 0));
    PanneauPrincipal.add(jButton9,                  new GridBagConstraints(3, 2, 1, 2, 0.0, 0.0
            ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(1, 1, 1, 1), 0, 0));
    PanneauPrincipal.add(jButton11,              new GridBagConstraints(4, 2, 1, 2, 0.0, 0.0
            ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(1, 4, 1, 1), 0, 0));
    PanneauPrincipal.add(jButton12,               new GridBagConstraints(5, 2, 1, 2, 0.0, 0.0
            ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(1, 1, 1, 1), 0, 0));
    PanneauPrincipal.add(jButton13,              new GridBagConstraints(4, 4, 1, 1, 0.0, 0.0
            ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(1, 4, 1, 1), 0, 0));
    PanneauPrincipal.add(jButton14,              new GridBagConstraints(5, 4, 1, 1, 0.0, 0.0
            ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(1, 1, 1, 1), 0, 0));
    PanneauPrincipal.add(jButtonSin,           new GridBagConstraints(0, 3, 1, 2, 0.0, 0.0
            ,GridBagConstraints.SOUTH, GridBagConstraints.NONE, new Insets(1, 1, 1, 4), 0, 0));
    PanneauPrincipal.add(jButtonCos,           new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0
            ,GridBagConstraints.SOUTHEAST, GridBagConstraints.NONE, new Insets(1, 1, 1, 4), 0, 0));
    PanneauPrincipal.add(jButtonTan,       new GridBagConstraints(0, 5, 1, 1, 0.0, 0.0
            ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(1, 1, 1, 4), 0, 0));
    PanneauPrincipal.add(jButton10,     new GridBagConstraints(6, 5, 1, 1, 0.0, 0.0
            ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(1, 4, 1, 1), 0, 0));
    PanneauPrincipal.add(jTextField1,           new GridBagConstraints(0, 1, 12, 1, 0.0, 0.0
            ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(1, 1, 4, 1), 325, 0));
    PanneauPrincipal.add(jButton15,    new GridBagConstraints(6, 4, 1, 1, 0.0, 0.0
            ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(1, 4, 1, 1), 0, 0));
    PanneauPrincipal.add(jPanelEcran,        new GridBagConstraints(0, 0, 7, 1, 0.0, 0.0
            ,GridBagConstraints.SOUTHWEST, GridBagConstraints.NONE, new Insets(1, 1, 4, 1), 328, 66));
    this.getContentPane().add(PanneauPrincipal, BorderLayout.CENTER);

  }
  /**Obtenir les informations d'applet*/
  public String getAppletInfo() {
    return "Information applet";
  }
  /**Obtenir les informations de paramètre*/
  public String[][] getParameterInfo() {
    return null;
  }

 /* public void paint (g)
  {
    g.setColor(Color.red);
    g.drawOval(50,20,100,100);
  }*/

  //Initialiseur statique pour le paramètre LookAndFeel
  static {
    try {
      //UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
      //UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
    }
    catch(Exception e) {
    }
  }
}

4 réponses

nonoz Messages postés 57 Date d'inscription mercredi 29 janvier 2003 Statut Membre Dernière intervention 23 juin 2004
8 avril 2004 à 22:44
ah oui j'oubliais quand je mets
g.setColor(Color.red);
g.drawOval(50,20,100,100);
dans le private void jbInit()
j'obtiens l'erreur java.lang.NullPointerException
pourquoi?
remerci
0
cs_GodConan Messages postés 2113 Date d'inscription samedi 8 novembre 2003 Statut Contributeur Dernière intervention 6 octobre 2012 12
9 avril 2004 à 00:27
GodConan :clown)

c pas la kil faut surcharger paint ;o)
c normal si tu surcharge le paint de l applet sa dessine dessu ;o)
il faut surcharger le paint de ton JPanel ... ;o)

T deja fait voir comment faire ;o)...

++
0
nonoz Messages postés 57 Date d'inscription mercredi 29 janvier 2003 Statut Membre Dernière intervention 23 juin 2004
9 avril 2004 à 01:23
vi merci dans l'autre poste. :)
0
safisoft Messages postés 489 Date d'inscription vendredi 30 janvier 2004 Statut Membre Dernière intervention 13 mai 2009 6
9 avril 2004 à 13:35
si le pbm n'aie pas résolue, surcharge aussi update:
publiv void update(Graphics g){
repaint(g);
}

Ok
0
Rejoignez-nous