package poker; import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.util.*; @SuppressWarnings("serial") public class Jeu extends JPanel { public static Carte tab_cartes[] = new Carte[52]; JLabel label; JFrame frame; public Choice c; Random Rand = new Random(); public Jeu(JFrame frame) { super(new BorderLayout()); this.frame = frame; JPanel choicePanel = createSimpleDialogBox(); label = new JLabel("Test Poker", JLabel.CENTER); label.setBorder(BorderFactory.createEmptyBorder(10,10,10,10)); choicePanel.setBorder(BorderFactory.createEmptyBorder(20,20,5,20)); //Lay out the main panel. add(label, BorderLayout.NORTH); add(choicePanel, BorderLayout.CENTER); } private static void createAndShowGUI() { //Make sure we have nice window decorations. JFrame.setDefaultLookAndFeelDecorated(true); JDialog.setDefaultLookAndFeelDecorated(true); //Create and set up the window. JFrame frame = new JFrame("Poker"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Set up the content pane. Container contentPane = frame.getContentPane(); contentPane.setLayout(new GridLayout(1,1)); contentPane.add(new Jeu(frame)); //Display the window. frame.pack(); frame.setVisible(true); } public JPanel createSimpleDialogBox() { JButton voteButton = null; voteButton = new JButton("Mélanger"); final Choice c = new Choice(); for (int i = 0 ; i < 52 ; i++) { c.addItem(tab_cartes[i].valeur + " de " + tab_cartes[i].couleur); } voteButton.addActionListener ( new ActionListener() { public void actionPerformed(ActionEvent e) { shuffle(tab_cartes); c.removeAll(); for (int i = 0 ; i < 52 ; i++) { c.addItem(tab_cartes[i].valeur + " de " + tab_cartes[i].couleur); } JOptionPane.showMessageDialog(frame, "Jeu mélangé"); } } ); return createPane(voteButton, c); } public JPanel createPane(JButton showButton, Choice c) { JPanel box = new JPanel(); box.setLayout(new BoxLayout(box, BoxLayout.PAGE_AXIS)); JPanel pane = new JPanel(new BorderLayout()); pane.add(c, BorderLayout.CENTER); pane.add(box, BorderLayout.NORTH); pane.add(showButton, BorderLayout.SOUTH); return pane; } public static void exch(Carte[] tab, int i, int j) { Carte swap = tab[i]; tab[i] = tab[j]; tab[j] = swap; } public static void shuffle(Carte[] tab) { int N = tab.length; for (int i = 0; i < N; i++) { int r = i + (int) (Math.random() * (N-i)); exch(tab, i, r); } } public static void main(String[] args) { //Jeu j = new Jeu(); init_jeu(); javax.swing.SwingUtilities.invokeLater ( new Runnable() { public void run() { createAndShowGUI(); } } ); //j.afficher_jeu(); } /*private void afficher_jeu() { for (int i = 0 ; i < 52 ; i++) { System.out.println(tab_cartes[i].couleur + tab_cartes[i].valeur); } }*/ private static void init_jeu() { String tab_valeurs[] = {"As","Deux","Trois","Quatre","Cinq","Six","Sept","Huit","Neuf","Dix","Valet","Dame","Roi"}; for (int i = 0 ; i < 13 ; i++) { tab_cartes[i] = new Carte("Pique", tab_valeurs[i]); } for (int i = 13 ; i < 26 ; i++) { tab_cartes[i] = new Carte("Trêfle", tab_valeurs[i-13]); } for (int i = 26 ; i < 39 ; i++) { tab_cartes[i] = new Carte("Carreau", tab_valeurs[i-26]); } for (int i = 39 ; i < 52 ; i++) { tab_cartes[i] = new Carte("Coeur", tab_valeurs[i-39]); } } }