import javax.swing.*; // Pour les composants graphiques que l'on ajoutera dans la m?thode creerWidget
import java.awt.*; // Pour la JFrame
import javax.swing.JButton;
public class Vue extends JFrame {
public JButton [][] TesBoutons;
JLabel level;
JLabel trie;
JLabel score;
int i;
int j;
Model m;
public Vue() {
initattribut();
creerWidget();
setSize(900, 900); // Fixe la taille par dfaut
setVisible(true); // Affiche la fenetre
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// Gestion de la fermeture
setTitle("Bejeweled");
}
public void creerWidget() {
JPanel a = new JPanel();
a.add(level);
JPanel b = new JPanel();
b.add(trie);
JPanel c = new JPanel();
c.add(score);
JPanel d = new JPanel(new FlowLayout(FlowLayout.RIGHT, 100, 60));
d.setLayout(new BoxLayout(d, BoxLayout.X_AXIS));
d.add(a);
d.add(b);
d.add(c);
JPanel e = new JPanel();
for ( i=0; i<8; i++){
for( j = 0; j<8; j++) {
e.add(TesBoutons[i][j]);
//puis leurs tailles, couleurs, aspect, etc.
}//fin du for des j
}//fin du for des i
e.setLayout(new GridLayout(8, 8));
JPanel f = new JPanel();
f.setLayout(new BoxLayout(f, BoxLayout.Y_AXIS));
f.add(d);
f.add(e);
JPanel b1 = new JPanel();
b1.add(f);
setContentPane(b1);
// cr?ation de tous les composants graphiques de la fentre
}
public void initattribut(){
TesBoutons = new JButton[8][8];
for (int i=0; i<8; i++) {
for (int j = 0; j < 8; j++) {
TesBoutons[i][j] = new JButton("");
TesBoutons[i][j].setActionCommand("" + i + "" + j);
TesBoutons[i][j].setPreferredSize(new Dimension(80, 80));
TesBoutons[i][j].setIcon(m.getListeImages()[m.getTesbouton(i,j)]);
TesBoutons[i][j].setDisabledIcon(m.getListeImages()[m.getTesbouton(i,j)]);
}//fin du for des j
}//fin du for des i
level = new JLabel("Level :");
trie = new JLabel("Tries :");
score = new JLabel("Score :");
}
}
public class Model {
public int[] bestScores;
public File scoresPath;
public ImageIcon[] listimage;
Vue v;
public Model(Vue v){
this.v=v;
charger();
Lire();
ecrire();
}
//Les Méthodes
protected void charger(){
File folder = new File("IHM2/src");
listimage = new ImageIcon[8];
int cpt = 0;
for(final File fileEntry : folder.listFiles()){
if(fileEntry.getName().charAt(0) == '.')
continue;
listimage[cpt] = new ImageIcon("IHM2/src"+'/' + fileEntry.getName());
cpt++;
}
}
public void Lire(){
int cpt = 0;String s;
try {
BufferedReader br = new BufferedReader(new FileReader(scoresPath));
while ((s = br.readLine()) != null) {
bestScores[cpt] = Integer.parseInt(s);
cpt++;
}
br.close();
}catch(Exception e){
e.printStackTrace();
}
}
public void ecrire(){
try{
BufferedWriter bw = new BufferedWriter(new FileWriter(scoresPath));
bw.write(""+ bestScores[0]);
bw.newLine();
bw.write("" + bestScores[1]);
bw.newLine();
bw.close();
}catch(Exception e){
e.printStackTrace();
}
}
//Les Getters
public ImageIcon[] getListeImages() {
charger();
return listimage;
}
public JButton getTesbouton(int i,int j) {
return v.TesBoutons[i][j];
}
public void setTesBoutons(JButton[][] tesBoutons) {
v.TesBoutons = tesBoutons;
}
}
class Appli {
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater( new Runnable() {
public void run() {
Vue v = new Vue();
Model m = new Model(v);
}
});
}
}
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre questionimport java.io.File; import java.io.FileNotFoundException; import java.io.PrintStream; import java.io.PrintWriter; import java.util.ArrayList; import java.util.List; import java.util.Random; import java.util.Scanner; public class Test { private List<Integer> bestScores = new ArrayList<Integer>(); private File scoresPath; public void lire() { try { final Scanner sc = new Scanner(scoresPath); while (sc.hasNextInt()) bestScores.add(sc.nextInt()); sc.close(); } catch(FileNotFoundException fnfe) { fnfe.printStackTrace(); } } public void ecrire() { try { final PrintWriter pw = new PrintWriter(scoresPath); for (final int i : bestScores) pw.println(i); pw.close(); } catch(FileNotFoundException fnfe) { fnfe.printStackTrace(); } } public void addScore(int score) { bestScores.add(score); } public void printScore(final PrintStream pw) { for (final int i : bestScores) pw.println(i); } public static void main(String[] args) { Test t = new Test(); t.scoresPath = new File("score.txt"); t.lire(); /* ajout de quelques scores */ Random rand = new Random(); for(int i = 0; i < 10; i++) t.addScore(rand.nextInt()); t.ecrire(); t.printScore(System.out); } }
public void paplusde2(JButton TesBouton[][],int a) { for (int i = 0;i<8; i++){ for(int j = 0;j<8;j++){ while (// Image Bouton[i][j]=Image Bouton[i][j+1]=Image Bouton[i][j+2] ou que Image Bouton[i][j]=Image Bouton[i+1][j]=Image Bouton[i+2][j] ) if (TesBouton[i][j].listimage[a] == TesBouton[i][j + 1].getListeImages()[a] && TesBouton[i][j + 1].getListeImages()[a] = TesBouton[i][j + 2].getListeImages()[a] ) { TesBouton[i][j].setIcon(getListeImages()[getTesbouton(0, 7)]); TesBouton[i][j].setDisabledIcon(getListeImages()[getTesbouton(0, 7)]); } if (TesBouton[i][j].getListeImages()[a] = TesBouton[i + 1][j].getListeImages()[a] && TesBouton[i + 1][j].getListeImages()[a] = TesBouton[i + 2][j].getListeImages()[a]){ TesBouton[i][j].setIcon(getListeImages()[getTesbouton(0, 7)]); TesBouton[i][j].setDisabledIcon(getListeImages()[getTesbouton(0, 7)]); } } }
public void paplusde2(JButton TesBouton[][],int a) { for (int i = 0;i<8; i++){ for(int j = 0;j<8;j++){ while (// Image Bouton[i][j]=Image Bouton[i][j+1]=Image Bouton[i][j+2] ou que Image Bouton[i][j]=Image Bouton[i+1][j]=Image Bouton[i+2][j] ) if (TesBouton[i][j].listimage[a] == TesBouton[i][j + 1].getListeImages()[a] && TesBouton[i][j + 1].getListeImages()[a] = TesBouton[i][j + 2].getListeImages()[a] ) { //Fonction qui supprime la grappe + fonction qui fait descendre les images } if (TesBouton[i][j].getListeImages()[a] = TesBouton[i + 1][j].getListeImages()[a] && TesBouton[i + 1][j].getListeImages()[a] = TesBouton[i + 2][j].getListeImages()[a]){ //Fonction qui supprime la grappe + fonction qui fait descendre les images } } }