String[] q1 = {
“Question 1 : Un repas chez des amis est pour vous \n\n 1_ Je mange mais je vais me rattraper demain\n\n 2_Je vais voir mes amis, le repas n’a aucune importance\n\n 3_ Un moment de plaisir à tous les niveaux”,
};
int point=0;
you could build on this really simple example if you didn’t want to use a UI library. It would save the user having to type out there answers and dealing with all the problems with that instead you can have a number of radio buttons which the user can select from. just add a button to move to the next question etc etc.
class RadioButton
{
String label;
float x, y, radius;
boolean selected;
public RadioButton(String label, float x, float y, float radius) {
this.label = label;
this.x = x;
this.y = y;
this.radius = radius;
this.selected = false;
}
Boolean select(float mx, float my) {
float dx = x - mx;
float dy = y - my;
float d = dx * dx + dy * dy;
if(d < radius * radius) {
return selected = true;
}
return false;
}
void draw() {
stroke(32);
noFill();
circle(x, y, radius);
if(selected) {
fill(32);
circle(x, y, radius / 2);
}
fill(32);
textSize(16);
text(label, x - textWidth(label) / 2, y - 16);
}
}
class QuestionHandler
{
String question;
String[] answers;
RadioButton[] radioButtons;
public QuestionHandler(String question, String[] answers) {
this.question = question;
this.answers = new String[answers.length];
arrayCopy(answers, 0, this.answers, 0, answers.length);
radioButtons = new RadioButton[answers.length];
float radioButtonRadius = getRadioButtonSpacing(answers);
float startX = (width / 2) - (radioButtons.length * radioButtonRadius / 2) + radioButtonRadius / 2;
for(int i = 0; i < answers.length; i++) {
radioButtons[i] = new RadioButton(answers[i], startX + i * radioButtonRadius, height / 2, 16);
}
}
float getRadioButtonSpacing(String[] answers) {
float rad = -10000;
for(int i = 0; i < answers.length; i++) {
float tw = textWidth(answers[i]);
if(tw > rad)
rad = tw;
}
return rad * 1.5;
}
void select(float mx, float my) {
Boolean updatedSelection = false;
for(int i = 0; i < radioButtons.length; i++) {
RadioButton rb = radioButtons[i];
if(rb.select(mx, my) && !updatedSelection) {
updatedSelection = true;
}
else {
rb.selected = false;
}
}
}
void draw() {
fill(0);
text(question, width / 2 - textWidth(question) / 2, height / 4);
for(int i = 0; i < radioButtons.length; i++) {
RadioButton rb = radioButtons[i];
rb.draw();
}
}
}
QuestionHandler[] questionHandlers;
int currentQuestion;
void setup() {
size(640, 480, P2D);
questionHandlers = new QuestionHandler[] {
new QuestionHandler("Is purple a fruit?", new String[] { "YES", "NO" }),
new QuestionHandler("Which number is larger?", new String[] { "0", "46", "103234" })
};
currentQuestion = 0;
}
void draw() {
background(200);
textSize(32);
questionHandlers[currentQuestion].draw();
}
void mouseReleased() {
questionHandlers[currentQuestion].select(mouseX, mouseY);
}
Well thanks for reply but I don’t think that I’ll use this way, I have to present it during exams and I must explain everything so it musn’t be too hard, I have only few months of knowledge…
Is there any simple way to get what I want ?
By the way, after getting the answer, I’d like to get a number of points per answer…
I mean
QUESTION 1
A gives 1 points
B gives 2 pts
etc…
But I don’t remember how to do with if and else if
where you got that from? is that working?
but if not, why you have 3 lines of it?
possibly you just start with
the idea is you can
( after you focused the canvas with mouse click )
type on keyboard and get ( or aggregate ) the input to a variable,
and as the answers are a b or c that part is easy.
while the part you call ?readFloat? is not that easy.
When you do if (q1==2), I presume that you try to check if the user chose the second answer.
If that’s the case you missed the idea of the exemple I gave you.
The keyReleased() function is called everytime the user is releasing a key. The key that the user just released is stored in a variable called keyCode.
So what you want to check is that the user did press the number on the keyboard that correspond to the answer that you were expected.