cs_ABF
Messages postés227Date d'inscriptionsamedi 21 mai 2005StatutMembreDernière intervention26 avril 2012
-
14 mai 2008 à 14:46
cs_ABF
Messages postés227Date d'inscriptionsamedi 21 mai 2005StatutMembreDernière intervention26 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
>
>
>++
mouzart
Messages postés161Date d'inscriptionmardi 26 avril 2005StatutMembreDernière intervention 6 novembre 20084 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);
}
/**
*
* @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();
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);
/*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;
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 |
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