1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
| import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Random;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Main {
// ====================DECLARATION ET
// INITIALISATION===========================//
static private JTextField question = new JTextField(30);
static private JTextField reponse = new JTextField(30);
static private int index, score, total;
static private JButton start = new JButton("START");
static private JButton validate = new JButton("VALIDER");
static private JButton stop = new JButton("STOP");
static private JButton closeFrame = new JButton("QUITTER");
static private ArrayList<Integer> indexesAlreadyTaken = new ArrayList<>();
static private String preguntas, reponseAnswer, reponseClavier;
static String[][] Objects = getData();
public static void main(String[] args) {
// =====================MISE EN FORME DE LA
// FENETRE==========================//
final JFrame frame = new JFrame();
frame.setVisible(true);
frame.setTitle("QUIZ");
frame.setSize(800, 600);
frame.setResizable(true);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// =================================================================//
// ======================FOND ECRAN============================//
JPanel content = new ImagePanel(new ImageIcon("fGris.jpeg").getImage());
// =================================================================//
// ======================AJOUT PANNEAU DANS LA
// FENETRE==============================//
frame.setContentPane(content);
// =================================================================//
// ======================CHOIX TAILLE ET POLICE DU
// JLABEL==============================//
Font f = new Font("Serif", Font.PLAIN, 36); // par exemple
question.setFont(f);
reponse.setFont(f);
Font ft = new Font("Serif", Font.PLAIN, 40);
validate.setFont(ft);
closeFrame.setFont(ft);
stop.setFont(ft);
start.setFont(ft);
// =================================================================//
// ============MISE EN FORME DES BOUTONS MISE A ZERO ET
// QUITTER============================//
question.setBackground(Color.white);
question.setBorder(null);
reponse.setBackground(Color.white);
reponse.setBorder(null);
// =================================================================//
// ======================AJOUT DES ELEMENTS AU
// PANNEAU============================//
content.add(question);
content.add(reponse);
content.add(start);
content.add(validate);
content.add(stop);
content.add(closeFrame);
// =================================================================//
// =====================AFFICHAGE ICONE COIN DE
// FENETRE=======================//
frame.setIconImage(new ImageIcon("fGris.jpeg").getImage());
// =================================================================//
frame.validate();
// =================================================================//
// ==========EVENEMENT EN CAS DE CLIC SUR BOUTON
// START============================//
start.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (e.getSource() == start) {
reponse.setText(" ");
do {
Random random = new Random();
index = random.nextInt(Objects.length);
preguntas = Objects[index][0];
reponseAnswer = Objects[index][1];
} while (indexesAlreadyTaken.contains(index));
indexesAlreadyTaken.add(index);
question.setText(String.format(" %s", preguntas));
reponseClavier = reponse.getText();
}
}
});
// ===============================================================================//
// ===============================================================================//
// ==========EVENEMENT EN CAS DE CLIC SUR BOUTON
// QUITTER============================//
stop.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (e.getSource() == stop) {
question.setText(String.format(
"Tu as terminé !! Ton score est de << %d/%d >>.",
score, total));
}
}
});
// ===============================================================================//
// ==========EVENEMENT EN CAS DE CLIC SUR BOUTON
// QUITTER============================//
closeFrame.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (e.getSource() == closeFrame) {
frame.dispose();
}
}
});
// ===============================================================================//
// ==================EVENEMENT EN CAS DE CLIC SUR BOUTON MISE A
// ZERO==============//
validate.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (e.getSource() == validate) {
if (reponseClavier.equalsIgnoreCase(reponseAnswer)) {
reponse.setText("Bonne réponse!! Tu es un champion! ");
score++;
} else {
reponse.setText(String.format(
"Dommage ! La bonne réponse était: %s. ",
reponseAnswer));
}
}
}
});
}
// =================================================================//
// =================================================================//
// =================================================================//
// =================================================================//
/*
* Classe image background
*/
static class ImagePanel extends JPanel {
private static final long serialVersionUID = 1L;
private Image img;
public ImagePanel(String img) {
this(new ImageIcon(img).getImage());
}
public ImagePanel(Image img) {
this.img = img;
}
public void paintComponent(Graphics g) {
g.drawImage(img, 0, 0, null);
}
/*
* ====================================================
*/
}
// ==================================================================== //
static String[][] getData() {
String[][] data = { { "Qui mord?", "Crocodile" },
{ "Qui fait cui cui?", "Canari" },
{ "Qui est Willy?", "Baleine" },
{ "Quel est ce fruit?", "Orange" }, };
return data;
}
} |
Partager