This site requires JavaScript, please enable it in your browser!
Greenfoot back
adlafforest
adlafforest wrote ...

2017/4/3

images

1
2
3
adlafforest adlafforest

2017/4/3

#
super(500, 400, 1); getBackground().setColor(Color.GRAY); getBackground().fill(); questions = new ArrayList<Question>(); String questionText = "What country is this?"; GreenfootImage drawedImage = new GreenfootImage("France.png"); getBackground().drawImage(drawedImage, 170, 70); ArrayList<Answer> answers = new ArrayList<Answer>(); answers.add(new Answer("Belgium", false)); answers.add(new Answer("Switzerland", false)); answers.add(new Answer("France", true)); answers.add(new Answer("Netherland", false)); Collections.shuffle(answers); Question question = new Question(questionText, answers); String explanation = "The correct answer was France. "; question.setExplanation(explanation); questions.add(question); Hi, I have several 'questions,' like this. Basically, I am making a quiz where a picture of a country shows up and the player has to determine, among the displayed answers, what country it is. Right now, the France.png image is a background for each question but I would like to be able to have a different image for each question. How would I go about doing that? I don't want to create different actor classes for each image because I have over 40 question (and therefore images). Thank you
Nosson1459 Nosson1459

2017/4/3

#
You can create a separate GreenfootImage for each image and then use it for whatever you want. You can draw that to the World frame, or you can have ONE actor which you change the image to. (That's all that I thought of on the spot.)
adlafforest adlafforest

2017/4/3

#
Thanks! What would the coding look like if I wanted to have one specific image show up for each question? I've just started coding and I'm not sure how to code what you described..
Nosson1459 Nosson1459

2017/4/3

#
I think it would make more sense to have an actor which you will change the image to.
/** All this coding should be in the world/class that the questions are being controlled from */

// instance field
private Actor imageActor = new Actor() {};

addObject(imageActor, 170, 70); // or where ever since by actors it goes by the middle of the image

// then whenever you want a new image just do:
imageActor.setImage("France.png"); // change to the name of the image
adlafforest adlafforest

2017/4/3

#
public class QuizWorld extends World { private List<Question> questions; private int questionNum; private int wrong; private int correct; private Actor imageActor = new Actor() {}; public QuizWorld() { super(500, 400, 1); getBackground().setColor(Color.GRAY); getBackground().fill(); questions = new ArrayList<Question>(); String questionText = "What country is this?"; ArrayList<Answer> answers = new ArrayList<Answer>(); addObject(imageActor, 250, 110); imageActor.setImage("France.png"); answers.add(new Answer("Belgium", false)); answers.add(new Answer("Switzerland", false)); answers.add(new Answer("France", true)); answers.add(new Answer("Netherland", false)); Collections.shuffle(answers); Question question = new Question(questionText, answers); String explanation = "The correct answer was France. "; question.setExplanation(explanation); questions.add(question); I know I'm doing something completely wrong because no image is showing up.
Nosson1459 Nosson1459

2017/4/3

#
The code that you did works for me. The only things that I can think of are that either it's being blocked by other actors, or the image "France.png" isn't what you thought it was. Do you get errors or you just don't see the image? If you can't figure it out then post the full class code (with code-tags). Need help with embedding? click here! Posting code? read this!
adlafforest adlafforest

2017/4/3

#
"IOException writing debug log" shows up when I try to open my "Answer" actor class. The coding I previously sent was from my QuizWorld class (that is where I store all of the questions and answers).
adlafforest adlafforest

2017/4/3

#
*I meant "Question" actor class (not "Answer")
adlafforest adlafforest

2017/4/3

#
This is my "Question" actor class: import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.util.List; import java.util.ArrayList; /** * A question the user is asked. * * @author Michael Berry (mjrb4) * @version 07/05/09 */ public class Question extends Actor { private String text; private List<Answer> answers; private Explanation explanation; /** * Create a new question. */ public Question(String question, List<Answer> answers) { text = QuizWorld.wordWrap(question); this.answers = answers; } /** * Set the explanation for this question. */ public void setExplanation(String explanation) { this.explanation = new Explanation(explanation); } /** * Get the explanation for this question. */ public Explanation getExplanation() { return explanation; } /** * Determine whether this question has an explanation. */ public boolean hasExplanation() { return explanation != null; } /** * Draw when added to the world. */ public void addedToWorld(World world) { GreenfootImage image = new GreenfootImage(500, 100); image.setFont(new Font("SansSerif", true, false, 16)); for(int i=0 ; i<text.length ; i++) { image.drawString(text, 10, 20+(i*20)); } setImage(image); for(int i=0 ; i<answers.size() ; i++) { getWorld().addObject(answers.get(i), 250, (text.length*30)+i*40); } } }
adlafforest adlafforest

2017/4/3

#
This is my "QuizWorld" class: public class QuizWorld extends World { private List<Question> questions; private int questionNum; private int wrong; private int correct; private Actor imageActor = new Actor() {}; public QuizWorld() { super(500, 400, 1); getBackground().setColor(Color.GRAY); getBackground().fill(); questions = new ArrayList<Question>(); String questionText = "What country is this?"; ArrayList<Answer> answers = new ArrayList<Answer>(); addObject(imageActor, 250, 110); imageActor.setImage("France.png"); answers.add(new Answer("Belgium", false)); answers.add(new Answer("Switzerland", false)); answers.add(new Answer("France", true)); answers.add(new Answer("Netherland", false)); Collections.shuffle(answers); Question question = new Question(questionText, answers); String explanation = "The correct answer was France. "; question.setExplanation(explanation); questions.add(question); ...(there are a bunch of other questions here)... showQuestion(); Greenfoot.start(); } /** * Wipes the world and shows the question. */ private void showQuestion() { clear(); addObject(questions.get(questionNum), 250, 50); } /** * Clears the world of the previous question. */ private void clear() { removeObjects(getObjects(null)); } /** * Records an answer as correct and advances. */ public void rightAnswer() { correct++; advance(true); } /** * Records an answer as incorrect and advances. */ public void wrongAnswer() { wrong++; advance(false); } /** * Advances to the next question. */ public void nextQuestion() { questionNum++; if(questionNum==questions.size()) { end(); } else { showQuestion(); } } /** * Called when the user has clicked on an answer. * @correct true if the answer is correct, false otherwise. */ private void advance(boolean correct) { Question question = questions.get(questionNum); if(question.hasExplanation()) { clear(); if(correct) { addObject(new Text("Correct!"), 250, 30); } else { addObject(new Text("Wrong!"), 250, 30); } addObject(question.getExplanation(), 250, 140); addObject(new Text("Press space to continue..."), 250, getHeight()-30); } else { nextQuestion(); } } /** * Called at the end of the game - displays the score. */ private void end() { clear(); int total = wrong+correct; Text text = new Text("You scored: " + correct + "/" + total + "."); Text text2 = new Text("That's " + (int)((((double)correct)/total)*100) + "%!"); addObject(text, 250, 50); addObject(text2, 250, 70); Greenfoot.stop(); } /** * Wraps a single string into an array of strings, maximum 60 characters. * Uses clever regular expression stuff that I was too lazy to work out * myself! * @author http://joust.kano.net/weblog/archives/000060.html */ public static String wordWrap(String str) { Pattern wrapRE = Pattern.compile(".{0,59}(?:\\S(?:-| |$)|$)"); List list = new LinkedList(); Matcher m = wrapRE.matcher(str); while (m.find()) list.add(m.group()); return (String) list.toArray(new String); } }
Nosson1459 Nosson1459

2017/4/3

#
adlafforest wrote...
"IOException writing debug log" shows up when I try to open my "Answer" actor class.
What are you asking, about the images or something that has nothing to do with them? What do you mean "Open", you mean opening the editor or pressing "new Question(String question, List<Answer> answers)", or neither? You didn't use code-tags.
adlafforest adlafforest

2017/4/3

#
I mean opening the editor.
adlafforest adlafforest

2017/4/3

#
Basically I have an entire quiz that works but the last issue I am having is that I don't know how to have different images show up on the screen, alongside each question. For example, I need to have the picture of France when I am asking "What country is this?" (and the correct answer is France). I need to have a picture of Ireland show up on the screen (alongside the possible answers), whenever the correct answer is Ireland.
adlafforest adlafforest

2017/4/3

#
This is how my quiz is: What country is this? France Belgium Switzerland (image location) Netherlands The player needs to click on the correct answer. What I need is to be able to put an image of France to the right of the possible answers.
Nosson1459 Nosson1459

2017/4/3

#
It's still not so clear, I've never seen this before. My first question is does the image work? My second question is what exactly happened? You tried to open the editor of the Question class and then the terminal window popped open and said: "IOException writing debug log"? That would mean that Greenfoot had a problem writing the debug log to your system, maybe try restarting Greenfoot, by closing the program's window and then reopening the project.
adlafforest wrote...
Basically I have an entire quiz that works but the last issue I am having is that I don't know how to have different images show up on the screen, alongside each question. For example, I need to have the picture of France when I am asking "What country is this?" (and the correct answer is France). I need to have a picture of Ireland show up on the screen (alongside the possible answers), whenever the correct answer is Ireland.
This is how my quiz is: What country is this? France Belgium Switzerland (image location) Netherlands The player needs to click on the correct answer. What I need is to be able to put an image of France to the right of the possible answers.
I understand your scenario, and the coding I gave should work in displaying an image on the screen, but now you're talking about separate issues (IOException).
There are more replies on the next page.
1
2
3