JLabel position et chemin d'image

cs_ABF Messages postés 227 Date d'inscription samedi 21 mai 2005 Statut Membre Dernière intervention 26 avril 2012 - 16 mai 2008 à 15:49
cs_ABF Messages postés 227 Date d'inscription samedi 21 mai 2005 Statut Membre Dernière intervention 26 avril 2012 - 18 mai 2008 à 18:49
Bonjour,

j'ai un soucis de GridLayout.
j'essaie de faire une grille d'image avec des Jlabel cote à cote

QUESTION :
1° Le chargement de mes images est impec mais je n'arrive pas à les coller entre elles

2°De plus, je n'arrive pas à donner le chemin relatif (non dur) de mon image


MON CODE :

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

public class Main {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {

JFrame myFrame = new JFrame();
JPanel panel = new JPanel();
GridLayout jours = new GridLayout(3, 2, 10, 50);
panel.setLayout(jours);
panel.applyComponentOrientation( ComponentOrientation.RIGHT_TO_LEFT);

for (int i=1 ; i<7 ; i++)
{
ImageIcon image = new ImageIcon("C:\\Users\\Renfield\\Documents\\NetBeansProjects\\JavaApplication8\\src\\javaapplication8\\vide.jpg");
JLabel lbl = new JLabel(image);
lbl.setPreferredSize(new Dimension(30, 30));
panel.add (lbl);
}

myFrame.setContentPane(panel);
myFrame.setTitle("Viewer by Brice");
myFrame.setBounds(0,0,300,300);
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myFrame.setVisible(true);

}

}



Merci de votre aide ++

25 réponses

Ombitious_Developper Messages postés 2333 Date d'inscription samedi 28 février 2004 Statut Membre Dernière intervention 26 juillet 2013 38
18 mai 2008 à 16:26
Salut:

public class MapInfo {

    private int[][] data;
   
    private int trainX;
   
    private int trainY;
   
    private int rows;
   
    private int columns;
   
    public int getColumns() {
        return columns;
    }

    public void setColumns(int columns) {
        this.columns = columns;
    }

    public int getRows() {
        return rows;
    }

    public void setRows(int rows) {
        this.rows = rows;
    }

    public MapInfo() {
    }
   
    public int[][] getData() {
        return data;
    }
   
    public void setData(int[][] data) {
        this.data = data;
    }

    public int getTrainX() {
        return trainX;
    }

    public void setTrainX(int trainX) {
        this.trainX = trainX;
    }

    public int getTrainY() {
        return trainY;
    }

    public void setTrainY(int trainY) {
        this.trainY = trainY;
    }
}
0
Ombitious_Developper Messages postés 2333 Date d'inscription samedi 28 février 2004 Statut Membre Dernière intervention 26 juillet 2013 38
18 mai 2008 à 16:27
Salut:

public class MapLoadingException extends Exception {
   
    public MapLoadingException(String msg) {
        super(msg);
    }
   
    public MapLoadingException(Throwable caught) {
        super(caught);
    }
   
    public MapLoadingException(String msg, Throwable caught) {
        super(msg, caught);
    }
}
0
Ombitious_Developper Messages postés 2333 Date d'inscription samedi 28 février 2004 Statut Membre Dernière intervention 26 juillet 2013 38
18 mai 2008 à 16:28
Salut:

// Map.txt

# Map
# Auteur
# Date

# Dimension de la map
columns 11
rows 8

# Positions du train
x 5
y 5

# Données
1 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 1
0
Ombitious_Developper Messages postés 2333 Date d'inscription samedi 28 février 2004 Statut Membre Dernière intervention 26 juillet 2013 38
18 mai 2008 à 16:28
Salut:

public class MapLoader {

    private static int[][] toMatrix(List data, int rows, int columns) {
        int[][] matrix = new int[rows][columns];
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < columns; ++j) {
                matrix[i][j] = data.get(i*columns + j);
            }
        }
        return matrix;
    }
   
    public MapInfo loadMap(String filename) throws MapLoadingException {
        try {
            StreamTokenizer st = new StreamTokenizer(new FileReader(filename));
            st.commentChar('#');
            st.parseNumbers();
            st.eolIsSignificant(true);
            st.lowerCaseMode(true);
           
            String lastToken = null;
            List data = new Vector();
            int columns = -1;
            int rows = -1;
            int x = -1;
            int y = -1;
           
            while(true) {
                int token = st.nextToken();
                if (token == StreamTokenizer.TT_EOF) {
                    break;
                }
               
                switch (token) {
                case StreamTokenizer.TT_NUMBER:
                    {
                        int n = (int)st.nval;
                        if (lastToken != null) {
                            if (lastToken.equals("columns")) {
                                columns = n;
                            } else if (lastToken.equals("rows")) {
                                rows = n;
                            } else if (lastToken.equals("x")) {
                                x = n;
                            } else if (lastToken.equals("y")) {
                                y = n;
                            }
                            lastToken = null;
                        } else {
                            data.add(n);
                        }
                        break;
                    }
                   
                case StreamTokenizer.TT_WORD:
                    {
                        lastToken = st.sval;
                        break;
                    }
                }
            }
           
            if (x == -1) {
                throw new MapLoadingException("Missing parameters (x attribute)");
            }
           
            if (y == -1) {
                throw new MapLoadingException("Missing parameters (y attribute)");
            }
           
            if (columns == -1) {
                throw new MapLoadingException("Missing parameters (columns attribute)");
            }
           
            if (rows == -1) {
                throw new MapLoadingException("Missing parameters (rows attributes)");
            }
           
            MapInfo mi = new MapInfo();
            mi.setTrainX(x);
            mi.setTrainY(y);
            mi.setColumns(columns);
            mi.setRows(rows);
            mi.setData(toMatrix(data, rows, columns));
           
            return mi;
        } catch (IOException e) {
            throw new MapLoadingException(e);
        }
    }
}
0

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

Posez votre question
cs_ABF Messages postés 227 Date d'inscription samedi 21 mai 2005 Statut Membre Dernière intervention 26 avril 2012
18 mai 2008 à 18:49
Re,

18 mai 2008 18:41:58 javaapplication10.MyFrame main
GRAVE: null
javaapplication10.MapLoadingException: Missing parameters (x attribute)
at javaapplication10.MapLoader.loadMap(MapLoader.java:79)
at javaapplication10.MyFrame.main(MyFrame.java:45)


===========================================

OK j'ai tout compris à par la dernière classe !
De plus je ne vois pas comment exploiter tous cela,

pour l'instant j'ai dans ma classe MyFrame :

import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
import java.awt.*;


public class MyFrame extends JFrame {
public static Grid grid = new Grid();

public static String [][]tblMap;
public static int posTrainX;
public static int posTrainY;

public MyFrame() {
super("Grid");
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

//Grid grid = new Grid();
getContentPane().add("Center", grid);
grid.setPreferredSize(new Dimension(930, 450));
pack();
setResizable(false);
}

public static void main(String[] args) {
try {
//affiche la frame
MyFrame f = new MyFrame();
f.setVisible(true);

//loap la map
MapLoader map = new MapLoader();
map.loadMap("map\\mapdefaut.txt");

} catch (MapLoadingException ex) {
Logger.getLogger(MyFrame.class.getName()).log(Level.SEVERE, null, ex);
}
}
}


Ensuite, pourquoi: case StreamTokenizer.TT_WORD ?
C'est pour les chr entre les "|" dans le fichier map ?

PS : Je serais toute la soirée sur MSN
0
Rejoignez-nous