begin process at 2008 08 22 02:02:55
1 229 745 membres
17 nouveaux aujourd'hui
14 267 membres club

Vous ne trouvez pas de réponse à votre problème ? Alors posez la question dans le forum.
Souvenez-vous qu'il n'y a jamais de question bête, mais rester dans l'ignorance parce que l'on n'ose pas poser une question, ça c'est une erreur !

Sujet : Grille dans un Jpanel [ Multimédia / Java2D ] (ABF)

Grille dans un Jpanel le 14/05/2008 14:46:45

ABF
>Slt, > >J'ai un soucis en Java avec Netbeans, > >Je veux réaliser un grille sur un Jpanel avec des images MAIS c'est pas concluant !!! > >Ci-dessous mon code : > > >public static void main(String args[]) { > java.awt.EventQueue.invokeLater(new Runnable() { > public void run() { > new Viewer().setVisible(true); > > genGrille gen = new genGrille; //marche pas !!!! > gen.generer(); > } > }); > } > > > class genGrille extends JPanel{ > > private JLabel[] tbl = new JLabel[450]; > private JPanel panel; > > // Constructeur de la classe Morpion > void generer() > { > // création Jpanel > Container cont = getContentPane(); > panel = new JPanel(); > cont.add(panel); > > > // Le gestionnaire de mise en forme du Panel panel est redéfini > // en grille de 15 lignes, 30 colonnes avec 0 pixels entre chaques > panel.setLayout(new GridLayout(15,30,0,0)); > > > // Création des images 450 (15*30) et ajout au Panel panel > for (int i=1 ; i<450 ; i++) > { > JButton btn = new JButton(""); > btn.setText(""); > btn.setBounds(30,30,30,30); // je veux du 30x30 pixels et le positionner en 0,0 > //btn.setMaximumSize(30,30); //impossible > //btn.setIcon("vide.bmp"); //impossible > panel.add(btn); > } > > > } > } > > >Merci de votre aide > > >++

Re : Grille dans un Jpanel le 14/05/2008 15:57:59

mouzart
slt

Post un code mieu formate si tu veux qu'on t'aide.

rgds

Re : Grille dans un Jpanel le 14/05/2008 16:15:11

ABF
Heu je t'envoie ca en MP car dans le Forum, le code se dés-indente

Re : Grille dans un Jpanel le 14/05/2008 21:23:44

Twinuts
Membre Club
(Admin CS)
Salut,

le post initial indenté :

Slt,

J'ai un soucis en Java avec Netbeans,

Je veux réaliser un grille sur un Jpanel avec des images MAIS c'est pas concluant !!!

Ci-dessous mon code :


public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Viewer().setVisible(true);
                
                genGrille gen = new genGrille; //marche pas !!!!
                gen.generer();
            }
        });
    }
    
    
    class genGrille extends JPanel{

private JLabel[] tbl = new JLabel[450];
        private JPanel panel;
        
// Constructeur de la classe Morpion
void generer()
{
             // création Jpanel
             Container cont = getContentPane();
             panel = new JPanel();
             cont.add(panel);
            
            
             // Le gestionnaire de mise en forme du Panel panel est redéfini
     // en grille de 15 lignes, 30 colonnes avec 0 pixels entre chaques
     panel.setLayout(new GridLayout(15,30,0,0));
            

            // Création des images 450 (15*30) et ajout au Panel panel
            for (int  i=1 ; i<450 ; i++)
            {
                JButton btn = new JButton("");
                btn.setText("");
                btn.setBounds(30,30,30,30);  // je veux du 30x30 pixels et le positionner en 0,0
                //btn.setMaximumSize(30,30); //impossible
                //btn.setIcon("vide.bmp");   //impossible
                panel.add(btn);
            }
            

        }
    }





------------------------------------
"On n'est pas au resto : ici on ne fait pas dans les plats tout cuits ..."

OoWORAoO

Re : Grille dans un Jpanel le 14/05/2008 21:36:54

ABF
Merci, comment tu as fait pour l'indentation ? POur mieux comprendre, j'ai mis tout le projet dans http://cbprogs.free.fr/GestionGare.zip Je le mettrais sur JavaFr plus tard quand il sera finit, ca aidera les debutants comme moi ;) Merci de m'aider

Re : Grille dans un Jpanel le 14/05/2008 22:19:08

mouzart
 				 						Voila ce qui te manque a toi de choisir le bon x/y et les bon param des constraints
panel.setLayout(new GridLayout(15,30,0,0));
            

            // Création des images 450 (15*30) et ajout au Panel panel
            for (int  i=1 ; i<450 ; i++)
            {
GridBagConstraints contraintes= new GridBagConstraints();

contraintes.gridx = x;//la position par rapport a l axe des absciss
contraintes.gridy = y;//la position vertical
contraintes.gridwidth = largeur;
contraintes.gridheight = hauteur;
contraintes.fill = typeElargissement;
contraintes.anchor = typePosition;
contraintes.ipadx = etalementHorizontal;
contraintes.ipady = etalementVertical;

                JButton btn = new JButton("");
                btn.setText("");
                btn.setBounds(30,30,30,30);  // je veux du 30x30 pixels et le positionner en 0,0
                //btn.setMaximumSize(30,30); //impossible
                //btn.setIcon("vide.bmp");   //impossible
panel.getLayout()).setConstraints(sousComposant,contraintes);
                panel.add(btn);
            }

Re : Grille dans un Jpanel le 14/05/2008 22:54:27

ABF
Merci MAIS

genGrille gen = new genGrille();
gen.generer();

Plante maintenant

==================================
MA CLASSE COMPLETE :




package gestiongare;

import java.util.*;         //lecture + timer
import java.util.Timer;     //timer
import java.io.*;           //fichier
import java.lang.String;    //split

import java.awt.*;
import javax.swing.*;



/**
*
* @author  Brice
*/
public class Viewer extends javax.swing.JFrame {
    
    static Timer t;
    static String [][] tblMap; //tbl de la map 2 dimensions
    

    /** Creates new form Viewer */
    public Viewer() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        btnEnd = new javax.swing.JButton();
        jPanel = new javax.swing.JPanel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Viewer");

        btnEnd.setText("QUITTER");
        btnEnd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEndActionPerformed(evt);
            }
        });

        jPanel.setBackground(new java.awt.Color(204, 204, 204));

        javax.swing.GroupLayout jPanelLayout = new javax.swing.GroupLayout(jPanel);
        jPanel.setLayout(jPanelLayout);
        jPanelLayout.setHorizontalGroup(
            jPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 557, Short.MAX_VALUE)
        );
        jPanelLayout.setVerticalGroup(
            jPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 296, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(876, Short.MAX_VALUE)
                .addComponent(btnEnd)
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(406, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(jPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 172, Short.MAX_VALUE)
                .addComponent(btnEnd)
                .addContainerGap())
        );

        getAccessibleContext().setAccessibleParent(jPanel);

        pack();
    }// </editor-fold>                        

private void btnEndActionPerformed(java.awt.event.ActionEvent evt) {                                      
    new Splash().setVisible(true);
    dispose();
}                                      

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Viewer().setVisible(true);
                
                //demande une map a charger
                    String temp="";
                    try
                    {
                        DataInputStream fichier;
                        fichier=new DataInputStream(new BufferedInputStream(new FileInputStream("chemin_du_fichier" )));
                        temp=fichier.readUTF();
                          
                        //fichier ouvert
                        //genere la grille
                        genGrille gen = new genGrille();
                        gen.generer();
                        
                        loadMap(temp);
                          
                        fichier.close();
                        
                        
                        //lance le jeu
                        t = new Timer();
                        t.schedule(new Jeu(), 0, 200);
                        
                    }
                    catch(Exception e)
                    {System.err.println("Message_erreur : " + e.getMessage() );
                    }
            }
        });
    }
    

    /*static class genGrille extends JPanel{
        private JLabel[] tbl = new JLabel[450];

        void generer()
        {
           // Auncun espacement entre les cases de la grille
           setLayout(new GridLayout(15,30,0,0));
            
           // Création et Ajout des images 450
           for (int  i=1 ; i<450 ; i++) {
                GridBagConstraints contraintes= new GridBagConstraints();

                contraintes.gridx = x;//la position par rapport a l axe des absciss
                contraintes.gridy = y;//la position vertical
                contraintes.gridwidth = largeur;
                contraintes.gridheight = hauteur;
                contraintes.fill = typeElargissement;
                contraintes.anchor = typePosition;
                contraintes.ipadx = etalementHorizontal;
                contraintes.ipady = etalementVertical;
              
              
              
                JLabel btn = new JLabel();
                btn.setText("");
                btn.setPreferredSize(new Dimension(30, 30));
                btn.setIcon(new javax.swing.ImageIcon(".\\img\\vide.jpg"));
                add(btn);
                btn.setVisible(true);
           }
        }
    }*/
    
    // OU BIEN
    
    class genGrille extends JPanel{

private JLabel[] tbl = new JLabel[450];
        private JPanel panel;
        
void generer()
{
             // création Jpanel
             Container cont = getContentPane();
             panel = new JPanel();
             cont.add(panel);
            
            
             // Le gestionnaire de mise en forme du Panel panel est redéfini
     // en grille de 15 lignes, 30 colonnes avec 0 pixels entre chaques
     panel.setLayout(new GridLayout(15,30,0,0));
            

            // Création des images 450 (15*30) et ajout au Panel panel
            for (int  i=1 ; i<450 ; i++)
            {
                
                GridBagConstraints contraintes= new GridBagConstraints();

     contraintes.gridx = 30*i;//la position par rapport a l axe des absciss
     contraintes.gridy = 15*i;//la position vertical
     contraintes.gridwidth = 900;
     contraintes.gridheight = 450;
     contraintes.fill = 1;//typeElargissement      |
     contraintes.anchor = 1;//typePosition         | PAS COMPRIS
     contraintes.ipadx = 1;//etalementHorizontal   |
     contraintes.ipady = 1;//etalementVertical     |
                
                JLabel btn = new JLabel();
                btn.setText("");
                btn.setPreferredSize(new Dimension(30, 30));
                btn.setIcon(new javax.swing.ImageIcon(".\\img\\vide.jpg"));
                add(btn);
                btn.setVisible(true);
                panel.add(btn);
            }
            

        }
    }
    
    
    
    
    // CHARGER LE FICHIER
    // PARSE CHAQUE LIGNE
    // MET EN FORME LE TABLEAU de la MAP
    static void loadMap(String fic) throws FileNotFoundException{
        
        int ligne;      //ligne du tbl de la map
        int colonne;    //colonne du tbl de la map

        String filePath = fic;
        Scanner scanner=new Scanner(new File(filePath));  // ????
        
        // On boucle sur chaque champ detecté
        ligne = 0;
        while (scanner.hasNextLine()) {
            String line = scanner.nextLine();
            
            //ajout tableau
            String[] tblTmp = line.split("|",10);
            
            colonne = 0;
            for (int  i=0 ; i<tblTmp.length ; i++)
            {tblMap[ligne][colonne] = tblTmp[i];
             colonne = colonne +1;
            }
            
        ligne = ligne + 1; // on passe à la ligne suivante
        }

        scanner.close();
    }
    

    
    static class Jeu extends TimerTask {
        
        private int xTrain; // horizontale
        private int yTrain; // verticale
        
        private Icon imgTmp; //image temporaire sur laquelle on va aller
        
        // on actionne le train
        public void run(){
            
            
            // on regarde sur chacun des 4 case a cote
            if(xTrain>0){                           // si on est pas a gauche
                if (yTrain>0 && yTrain<30){         // si on est pas en bordure Haut Bas
                    if (tblMap[xTrain][yTrain] != ""){
                        
                    }
                }else{
                    
                }
            }

        }
    }
    
    
    
    
    
    
    
    
    
    
    
    
    // Variables declaration - do not modify                    
    private javax.swing.JButton btnEnd;
    private javax.swing.JPanel jPanel;
    // End of variables declaration                  

}







Re : Grille dans un Jpanel le 14/05/2008 23:00:38

ABF
Je pense qu'avec la source (projet) sur le site c'est plus clair et avec les couleur de l'IDE


Classé sous : grille, void, jpanel, panel, btn

Participer à cet échange

Pub



Appels d'offres

CalendriCode

Août 2008
LMMJVSD
    123
45678910
11121314151617
18192021222324
25262728293031

VS Express FR Gratuit !

VS Express en français et 100% gratuit !

Téléchargements

Logiciels à télécharger sur le même thème :

Boutique

Boutique de goodies CodeS-SourceS