problème interface graphique

wawawa
problème interface graphique

Bonjour,

Je ne comprends pas pourquoi rien ne s'affiche dans ma fenêtre. Je pense qu'il s'agit d'un problème de variable.
Merci de votre réponse.

package fourmiliere;
 
import javax.swing.JFrame;
import javax.swing.*;
import java.awt.*;
import javax.swing.border.TitledBorder;
 
public class phasedejeu extends JFrame{
 
       public phasedejeu() {
 
         try {
            jbInit();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setLocation(100,100);
        this.setSize(700,550);
       this.setTitle("Simulation");
      this.setVisible(true);
 
    }
    private JPanelDessin2 jPanel5 = new JPanelDessin2(this);
 
 
  private Dimension dim = jPanel5.getSize() ;
  private int larg = dim.width ;
  private int haut = dim.height;
 
  public int getLarg(){
    return larg;
  }
 
  public void setLarg(int x){
         larg=x;
  }
 
  public int getHaut(){
        return haut;
  }
 
  public void setHaut(int y){
         haut=y;
  }
 
 
    JPanel jPanel6 = new JPanel();
    JPanel jPanel7 = new JPanel();
    TitledBorder titledBorder1 = new TitledBorder("");
 
    Icon tigerIcon = new ImageIcon("stop.gif");
    JButton jButton1 = new JButton("",tigerIcon);
 
    Icon stopIcon = new ImageIcon("lecture2.gif");
     JButton jButton2 = new JButton("",stopIcon);
 
   Icon fourmIcon = new ImageIcon("fourmis.gif");
}

package fourmiliere;
 
import java.awt.*;
import javax.swing.*;
 
public class JPanelDessin2 extends JPanel {
 
    private phasedejeu jeu;
        public int largeur ;
        public int hauteur ;
 
     Image tete2,fourmiliere,texture;
 
 
     public JPanelDessin2(phasedejeu paramJeu){
 
           this.jeu=paramJeu;
           this.largeur = jeu.getLarg();
           this.hauteur = jeu.getHaut();
 
       texture=Toolkit.getDefaultToolkit().getImage("herbe.32934.jpg");
      tete2 = Toolkit.getDefaultToolkit().getImage("fourmi.jpg");
      fourmiliere = Toolkit.getDefaultToolkit().getImage("DSC_4798 Anthill l.jpg");
 
        try {
            jbInit();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
 
    public void paintComponent (Graphics g) {
                super.paintComponent(g);
                this.setBackground(Color.white);
                int x=largeur; int y=hauteur;
 
               //g.drawImage(texture,0,0,1350,758,this);
               g.drawImage(tete2,200,50,x,y, this);
               //g.drawImage(fourmiliere,50,100,100,100,this);
        }
 
    private void jbInit() throws Exception {
    }
 
}
fredericmazue

Et dans ton code où incorpores tu les JPanel à ton JFrame ?

Un autre remarque. Ta façon de charger les images n'est pas ce qu'il y a de plus solide. Ce que je veux dire c'est que si tu déplaces un élément de ton application ou si tu emballes tout dans un jar, ça risque ne ne pas marcher.

wawawa

Voila la class phasedejeu en entier:

package fourmiliere;



import javax.swing.JFrame;
import javax.swing.*;
import java.awt.*;
import javax.swing.border.TitledBorder;

/**
 * <p>Title: </p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2007</p>
 *
 * <p>Company: </p>
 *
 * @author not attributable
 * @version 1.0
 */
public class phasedejeu extends JFrame{

       public phasedejeu() {

           //Icon tigerIcon = new ImageIcon("Creative player 128.gif");

         try {
            jbInit();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setLocation(100,100);
        this.setSize(700,550);
       this.setTitle("Simulation");
      this.setVisible(true);

    }

    private void jbInit() throws Exception {
        jLabel1.setForeground(Color.white);
        jLabel1.setText("jLabel1");
        jTextField3.setText("jTextField3");
        jTextField4.setText("jTextField4");
        jLabel3.setForeground(Color.white);
        jLabel3.setText("jLabel3");
        jLabel4.setForeground(Color.white);
        jLabel4.setText("jLabel4");
        jTextField5.setText("jTextField5");
        jTextField6.setText("jTextField6");
        jLabel5.setForeground(Color.white);
        jLabel5.setText("jLabel5");
        jLabel6.setForeground(Color.white);
        jLabel6.setText("jLabel6");
        jLabel7.setForeground(Color.white);
        jLabel7.setText("jLabel7");
        jLabel8.setForeground(Color.white);
        jLabel8.setText("jLabel8");
        jLabel9.setForeground(Color.white);
        jLabel9.setText("jLabel9");
        jLabel10.setForeground(Color.white);
        jLabel10.setText("jLabel10");
        jPanel3.setLayout(flowLayout2);
        jPanel4.setLayout(gridLayout2);
        gridLayout2.setColumns(4);
        gridLayout2.setRows(2);
        jLabel11.setForeground(Color.white);
        jLabel11.setToolTipText("");
        jLabel11.setHorizontalAlignment(SwingConstants.CENTER);
        jLabel11.setHorizontalTextPosition(SwingConstants.CENTER);
        jLabel11.setText("           Vitesse de la Simulation");
        jPanel1.setBackground(SystemColor.desktop);
        jPanel1.setPreferredSize(new Dimension(100, 500));
        jPanel1.setToolTipText("");
        jPanel1.setLayout(flowLayout1);
        gridLayout1.setHgap(5);
        gridLayout1.setVgap(10);
        flowLayout1.setVgap(20);
        this.setResizable(true);

        jButton2.setMaximumSize(new Dimension(33, 33));
        jButton2.setMinimumSize(new Dimension(33, 33));
        jButton2.setPreferredSize(new Dimension(33, 33));
        jButton2.setHorizontalTextPosition(SwingConstants.CENTER);

        jButton1.setMaximumSize(new Dimension(33, 33));
        jButton1.setMinimumSize(new Dimension(33, 33));
        jButton1.setPreferredSize(new Dimension(33, 33));
        jButton1.setHorizontalTextPosition(SwingConstants.CENTER);

        jPanel3.setBackground(SystemColor.desktop);
        jPanel6.setBackground(SystemColor.desktop);
        jPanel7.setBackground(SystemColor.desktop);
        jPanel2.setBackground(SystemColor.desktop);
        jLabel2.setForeground(Color.white);
        jPanel4.setBackground(SystemColor.desktop);
        jSlider1.setBackground(SystemColor.desktop);
        jSlider1.setForeground(Color.white);
        this.getContentPane().add(jPanel1, java.awt.BorderLayout.WEST);
        jPanel1.add(jPanel2, null);
        jPanel2.setLayout(gridLayout1);
        gridLayout1.setColumns(1);
        gridLayout1.setRows(15);
        jLabel2.setText("jLabel2");
        jPanel2.add(jLabel2);
        jPanel2.add(jLabel10);
        jPanel2.add(jTextField6);
        jPanel2.add(jLabel9);
        jPanel2.add(jLabel8);
        jPanel2.add(jTextField5);
        jPanel2.add(jLabel7);
        jPanel2.add(jLabel6);
        jPanel2.add(jTextField4);
        jPanel2.add(jLabel5);
        jPanel2.add(jLabel4);
        jPanel2.add(jTextField3);
        jPanel2.add(jLabel3);
        jPanel2.add(jLabel1);
        jPanel3.add(jPanel4);
        jPanel4.add(jLabel11);
        jPanel4.add(jButton2);
        jPanel4.add(jSlider1);
        jPanel4.add(jButton1);
        this.getContentPane().add(jPanel5, java.awt.BorderLayout.CENTER);
        this.getContentPane().add(jPanel3, java.awt.BorderLayout.SOUTH);
        this.getContentPane().add(jPanel6, java.awt.BorderLayout.EAST);
        this.getContentPane().add(jPanel7, java.awt.BorderLayout.NORTH);
    }

    JPanel jPanel1 = new JPanel();
    JPanel jPanel2 = new JPanel();
    GridLayout gridLayout1 = new GridLayout();
    JLabel jLabel2 = new JLabel();
    JLabel jLabel1 = new JLabel();
    JTextField jTextField3 = new JTextField();
    JTextField jTextField4 = new JTextField();
    JLabel jLabel3 = new JLabel();
    JLabel jLabel4 = new JLabel();
    JTextField jTextField5 = new JTextField();
    JTextField jTextField6 = new JTextField();
    JLabel jLabel5 = new JLabel();
    JLabel jLabel6 = new JLabel();
    JLabel jLabel7 = new JLabel();
    JLabel jLabel8 = new JLabel();
    JLabel jLabel9 = new JLabel();
    JLabel jLabel10 = new JLabel();
    JPanel jPanel3 = new JPanel();
    FlowLayout flowLayout2 = new FlowLayout();
    JPanel jPanel4 = new JPanel();
    GridLayout gridLayout2 = new GridLayout();
    JSlider jSlider1 = new JSlider();
    JLabel jLabel11 = new JLabel();
    FlowLayout flowLayout1 = new FlowLayout();

    private JPanelDessin2 jPanel5 = new JPanelDessin2(this);


  private Dimension dim = jPanel5.getSize() ;
  private int larg = dim.width ;
  private int haut = dim.height;

  public int getLarg(){
    return larg;
  }

  public void setLarg(int x){
         larg=x;
  }

  public int getHaut(){
        return haut;
  }

  public void setHaut(int y){
         haut=y;
  }


    JPanel jPanel6 = new JPanel();
    JPanel jPanel7 = new JPanel();
    TitledBorder titledBorder1 = new TitledBorder("");

    Icon tigerIcon = new ImageIcon("stop.gif");
    JButton jButton1 = new JButton("",tigerIcon);

    Icon stopIcon = new ImageIcon("lecture2.gif");
     JButton jButton2 = new JButton("",stopIcon);

   Icon fourmIcon = new ImageIcon("fourmis.gif");
}

classe principale:

package fourmiliere;

/**
 * <p>Titre : </p>
 *
 * <p>Description : </p>
 *
 * <p>Copyright : Copyright (c) 2007</p>
 *
 * <p>Société : </p>
 *
 * @author non attribuable
 * @version 1.0
 */
public class principale {
    public principale() {
        try {
            jbInit();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public static void main(String[] args) {
        Bienvenue monBienvenue = new Bienvenue();
       phasedejeu maphasedejeu=new phasedejeu();
        Bilan monBilan=new Bilan();
      Erreur monErreur=new Erreur();
    }

    private void jbInit() throws Exception {
    }
}
fredericmazue

Es tu sûr que la taille du jpanel5 est correctement définie ?
Si je comprends quelque chose à ton code il me semble que non.
Si ça se trouve en raison du nombre important d'autres composants ton jpanel 5 est microscopique et tout ce que tu dessines tombe hors limite, donc tu ne vois rien.

De plus ce code

  public void setHaut(int y){
         haut=y;
  } 

Concrètement il sert à quoi ?
jrebillat

J'aime bien ce que fait Borland, la plupart du temps...

Mais leur méthode "jbInit()" c'est une horreur !
Je n'y comprends jamais rien... Oui, je sais, on va me répondre que ce n'est pas la peine de la regarder puisqu'elle est générée automatiquement.
En fait, je hais les générateurs automatiques. Le programmeur n'est plus le maître de sa réalisation et du coup, quand ça foire, il est perdu...!

Mon conseil c'est : Quand tu n'arrives pas à résoudre un problème, décompose-le en plusieurs, jusqu'à ce qu'ils soient suffisament petits pour que tu puisse les résoudre un par un.

Concrétement dans ton cas, à ta place je ferais plus de classes (d'abord une par JPanel) ce qui segmentera ton graphisme. Après tu pourras essayer de les afficher une à une, puis à plusieurs ensemble... jusqu'à trouver ce qui fait que cela ne s'affiche pas correctement.

fredericmazue

Quote:
En fait, je hais les générateurs automatiques. Le programmeur n'est plus le maître de sa réalisation et du coup, quand ça foire, il est perdu...!

Tout à fait d'accord.
D'ailleurs moi c'est avec Emacs que je suis le plus productif.
Oui je sais c'est une attitude de dinosaure, on me le dit souvent :D
Mais le dinosaure et son Emac ont plus d'un tour dans leur sac et sont rarement perdus dans du code illisible comme c'est le cas ici ;)