Grille dans un Jpanel

cs_ABF Messages postés 227 Date d'inscription samedi 21 mai 2005 Statut Membre Dernière intervention 26 avril 2012 - 14 mai 2008 à 14:46
cs_ABF Messages postés 227 Date d'inscription samedi 21 mai 2005 Statut Membre Dernière intervention 26 avril 2012 - 14 mai 2008 à 23:00
>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
>
>
>++

7 réponses

mouzart Messages postés 161 Date d'inscription mardi 26 avril 2005 Statut Membre Dernière intervention 6 novembre 2008 4
14 mai 2008 à 15:57
slt

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

rgds
0
cs_ABF Messages postés 227 Date d'inscription samedi 21 mai 2005 Statut Membre Dernière intervention 26 avril 2012
14 mai 2008 à 16:15
Heu je t'envoie ca en MP car dans le Forum, le code se dés-indente
0
Twinuts Messages postés 5375 Date d'inscription dimanche 4 mai 2003 Statut Modérateur Dernière intervention 14 juin 2023 111
14 mai 2008 à 21:23
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
0
cs_ABF Messages postés 227 Date d'inscription samedi 21 mai 2005 Statut Membre Dernière intervention 26 avril 2012
14 mai 2008 à 21:36
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
0

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
mouzart Messages postés 161 Date d'inscription mardi 26 avril 2005 Statut Membre Dernière intervention 6 novembre 2008 4
14 mai 2008 à 22:19
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);
            }
0
cs_ABF Messages postés 227 Date d'inscription samedi 21 mai 2005 Statut Membre Dernière intervention 26 avril 2012
14 mai 2008 à 22:54
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

}
0
cs_ABF Messages postés 227 Date d'inscription samedi 21 mai 2005 Statut Membre Dernière intervention 26 avril 2012
14 mai 2008 à 23:00
Je pense qu'avec la source (projet) sur le site c'est plus clair et avec les couleur de l'IDE
0
Rejoignez-nous