Bonjour,
Je voudrais :
0) demander de sélectionner une map
1) charger un fichier texte, le parser en fonction des | et remplir un tableau
2) generer une grille de Jlabel (qui seront mes images)
3) en fonction de mon tableau, associé au Jlabel les images correspondantes
4) tous les x millisecondes, faire une action (timer)
Ma form :
j'ai une form avec un Jpanel (Jpanel)
un Jbutton Quitter
Ci dessous ma classe complete
(err sur: Container cont = getContentPane(); dans gestGrille)
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
static JLabel[] tblGrille = new JLabel[450];
/** 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
gestGrille gen= new gestGrille();
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
static class gestGrille extends JPanel{
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);
}
}
// Met une image sur le Jlabel choisit
void attribImage(int numJlabel,String repImg)
{tblGrille[numJlabel].setIcon(new javax.swing.ImageIcon(repImg));
}
}
// 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{
}
}
}
}
Merci