Demande d'aide pour wordzap

Résolu
cs_javalang Messages postés 18 Date d'inscription dimanche 29 mars 2009 Statut Membre Dernière intervention 15 août 2009 - 29 mars 2009 à 23:33
cs_javalang Messages postés 18 Date d'inscription dimanche 29 mars 2009 Statut Membre Dernière intervention 15 août 2009 - 30 mars 2009 à 13:32
salut, wordzap est un jeu agréable pour moi, je voudrais réaliser un jeu qui lui ressemble. une personne peut m'aider pour le code?

2 réponses

cs_DARKSIDIOUS Messages postés 15814 Date d'inscription jeudi 8 août 2002 Statut Membre Dernière intervention 4 mars 2013 130
30 mars 2009 à 07:46
Salut,

Et si tu commençais par essayer de le coder toi-même et revenir nous poser des questions lorsque tu bloque sur quelque chose... non seulement tu aurais bien plus de chance d'obtenir de l'aide comme cà, mais en plus tu ferais plaisir à toute la communauté en respectant notre règlement...
______________________________________
DarK Sidious
3
cs_javalang Messages postés 18 Date d'inscription dimanche 29 mars 2009 Statut Membre Dernière intervention 15 août 2009
30 mars 2009 à 13:32
Bonjour
J'ai déjà fait deux classes. La première est WordZap. Elle reçoit deux autres classes Grille et LettresBoutons. Je suis arrivé au point où les lettres sont générées aléatoirement quand je clique sur btnReady. Mais je n'arrive pas à les affecter sur les boutons. Ensuite, je voudrais savoir comment affecter les lettres des boutons dans la grille. Ce sont là mes deux points de blocage.
Merci pour ton attention et ton aide.

-DEBUT WORDZAP=-=
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Toolkit;
import javax.swing.JButton;
import javax.swing.JFrame;

public class WordZap extends javax.swing.JFrame {

    private GridBagLayout layout;
    private GridBagConstraints cons;
    private Container conteneur;
    private Grille grille;
    private JButton btnReady;
    private GridBagConstraints contraintes;
    private LettresBoutons lesBoutons;
   
   
    public WordZap() {
     
        conteneur = this.getContentPane();
        layout = new GridBagLayout();
        contraintes = new GridBagConstraints();
        setLayout(layout);
        grille = new Grille();
        contraintes.fill = GridBagConstraints.BOTH;

        this.setSize(700, 500);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        Dimension frameSize = this.getSize();
        if (frameSize.height > screenSize.height) {
            frameSize.height = screenSize.height;
        }
        if (frameSize.width > screenSize.width) {
            frameSize.width = screenSize.width;
        }
        this.setLocation((screenSize.width - frameSize.width) / 2,
                (screenSize.height - frameSize.height) / 2);

        layout = new GridBagLayout();
        conteneur = this.getContentPane();
        conteneur.setLayout(layout);
        cons = new GridBagConstraints();
        cons.fill = GridBagConstraints.BOTH;
       
        addComp(grille, 5,0,7,1);
        lesBoutons = new LettresBoutons();
        addComp(lesBoutons, 0,4,3,1);
       
        setVisible(true);
        setResizable(false);
        this.setTitle("Jeu WordZap");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
 
    private void addComp(Component comp, int c, int r, int h, int w) {
        contraintes.gridx = c;
        contraintes.gridy = r;
        contraintes.gridheight = h;
        contraintes.gridwidth = w;
        layout.setConstraints(comp, contraintes);
        add(comp);
    }
   
    public static void main(String[] args) {    
        new WordZap();
    }
}
-FIN WORDZAP=-=

-DEBUT GRILLE=-=

import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.util.EnumMap;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Grille extends JPanel {

    private EnumMap<Operation, JTextField> txt1,  txt2,  txt3,  txt4,  txt5;
    private GridBagLayout layout;
    private GridBagConstraints contraintes;

    public Grille() {
        layout = new GridBagLayout();
        setLayout(layout);
        contraintes = new GridBagConstraints();

        txt1 = new EnumMap<Operation, JTextField>(Operation.class);
        txt2 = new EnumMap<Operation, JTextField>(Operation.class);
        txt3 = new EnumMap<Operation, JTextField>(Operation.class);
        txt4 = new EnumMap<Operation, JTextField>(Operation.class);
        txt5 = new EnumMap<Operation, JTextField>(Operation.class);     
       
        for (Operation op : Operation.values()) {

            JTextField txt = new JTextField(" ");
            txt.setFont(new Font(txt.getFont().getName(), Font.BOLD, 30));
            txt.setPreferredSize(new Dimension(60, 60));
            txt.setHorizontalAlignment(JTextField.CENTER);
            txt.setEditable(false);
            txt1.put(op, txt);

            txt = new JTextField("  ");
            txt.setHorizontalAlignment(JTextField.CENTER);
            txt.setFont(new Font(txt.getFont().getName(), Font.BOLD, 30));
            txt.setPreferredSize(new Dimension(60, 60));
            txt2.put(op, txt);
            txt.setEditable(false);

            txt = new JTextField("  ");
            txt.setHorizontalAlignment(JTextField.CENTER);
            txt.setFont(new Font(txt.getFont().getName(), Font.BOLD, 30));
            txt.setPreferredSize(new Dimension(60, 60));
            txt3.put(op, txt);
            txt.setEditable(false);

            txt = new JTextField("  ");
            txt.setHorizontalAlignment(JTextField.CENTER);
            txt.setFont(new Font(txt.getFont().getName(), Font.BOLD, 30));
            txt.setPreferredSize(new Dimension(60, 60));
            txt4.put(op, txt);
            txt.setEditable(false);

            txt = new JTextField("  ");
            txt.setHorizontalAlignment(JTextField.CENTER);
            txt.setFont(new Font(txt.getFont().getName(), Font.BOLD, 30));
            txt.setPreferredSize(new Dimension(60, 60));
            txt5.put(op, txt);
            txt.setEditable(false);
        }

        contraintes.fill = GridBagConstraints.BOTH;
        int r = 1;
        for (Operation op : Operation.values()) {
            addComp(txt1.get(op), r, 1, 1, 1);
            addComp(txt2.get(op), r, 2, 1, 1);
            addComp(txt3.get(op), r, 3, 1, 1);
            addComp(txt4.get(op), r, 4, 1, 1);
            addComp(txt5.get(op), r, 5, 1, 1);
            ++r;
        }
    }

    private void addComp(Component comp, int x, int y, int h, int w) {
        contraintes.gridx = y;
        contraintes.gridy = x;
        contraintes.gridwidth = h;
        contraintes.gridheight = w;
        layout.setConstraints(comp, contraintes);
        this.add(comp);
    }
}

-FIN GRILLE=-=

-DEBUT LETTRESBOUTONS=-=

import java.awt.Component;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JPanel;

public class LettresBoutons extends JPanel {
    private GridBagLayout layout;
    private GridBagConstraints contraintes;
    private JButton btnReady;
    private ButtonHandler btnHandler;
    private final String CONSONNES = "bcdfghjklmnpqrstvwxz";
    private final String VOYELLES = "aeiouy";
    private final String AUTRES = "bacedifoguhyjakelimonupyqaresitovuwyxaz";

    public LettresBoutons() {
        layout = new GridBagLayout();
        setLayout(layout);
        contraintes = new GridBagConstraints();
        JPanel panel = new JPanel();
        panel.setLayout(new GridLayout(2, 4));

        panel.setLayout(new GridLayout(2, 4));
        panel.add(new JButton(" "));
        panel.add(new JButton(" "));
        panel.add(new JButton(" "));
        panel.add(new JButton(" "));
        panel.add(new JButton(" "));
        panel.add(new JButton(" "));
        panel.add(new JButton(" "));
        panel.add(new JButton(" "));
        btnReady = new JButton("Ready ?");
        btnHandler = new ButtonHandler();
        btnReady.addActionListener((ActionListener) btnHandler);

        addComp(panel, 0, 0, 1, 1);
        addComp(btnReady, 1, 0, 4, 1);
    }

    private void JetLettres() {

        JPanel panel = new JPanel();
        panel.setLayout(new GridLayout(2, 4));

        final int nbVoyelles = 3;
        final int nbConsonnes = 3;
        final int nbAutres = 2;

        ArrayList<String> lettres = new ArrayList<String>();

        Random random = new Random(System.currentTimeMillis());
        for (int i = 0; i < nbVoyelles; ++i) {
            char voyelle = VOYELLES.charAt(random.nextInt(VOYELLES.length()));
            lettres.add("" + voyelle);
        }
        for (int j = 0; j < nbConsonnes; ++j) {
            char consonnes = CONSONNES.charAt(random.nextInt(CONSONNES.length()));
            lettres.add("" + consonnes);
        }
        for (int k = 0; k < nbAutres; ++k) {
            char autre = AUTRES.charAt(random.nextInt(AUTRES.length()));
            lettres.add("" + autre);
        }
        Collections.shuffle(lettres);
        System.out.println(lettres);
        for (int m = 0; m < 8; ++m) {
            panel.add(new JButton(lettres.get(m)));
        }
    }

    private void addComp(Component comp, int x, int y, int h, int w) {
        contraintes.gridx = y;
        contraintes.gridy = x;
        contraintes.gridwidth = h;
        contraintes.gridheight = w;
        layout.setConstraints(comp, contraintes);
        this.add(comp);
    }

    private class ButtonHandler implements ActionListener {
        public void actionPerformed(ActionEvent event) {
            if (event.getSource() == btnReady) {
                JetLettres();
            }
        }
    }
}

-FIN LETTRESBOUTONS=-=
0
Rejoignez-nous