|
| 1 | +package org.teachingkidsprogramming.recipes.quizzes.graders; |
| 2 | + |
| 3 | +import java.awt.Font; |
| 4 | +import java.awt.Graphics2D; |
| 5 | + |
| 6 | +import javax.swing.JPanel; |
| 7 | + |
| 8 | +import org.teachingextensions.logo.Colors; |
| 9 | +import org.teachingextensions.logo.Paintable; |
| 10 | +import org.teachingextensions.logo.Tortoise; |
| 11 | +import org.teachingextensions.simpleparser.Parser; |
| 12 | +import org.teachingkidsprogramming.recipes.quizzes.graders.AdLibsQuizAdapter.Pieces; |
| 13 | + |
| 14 | +public class QuizBuzzGrader implements Paintable |
| 15 | +{ |
| 16 | + private static class Model |
| 17 | + { |
| 18 | + public String three; |
| 19 | + public Model(String three) |
| 20 | + { |
| 21 | + this.three = three; |
| 22 | + } |
| 23 | + } |
| 24 | + private boolean[] answers; |
| 25 | + public static int TURTLE_SPEED = 9; |
| 26 | + private QuizBuzzAdapter quiz; |
| 27 | + private void displayScreen() |
| 28 | + { |
| 29 | + QuizUtils.prepareScoringScreen(answers, this, TURTLE_SPEED); |
| 30 | + } |
| 31 | + public void grade(QuizBuzzAdapter quiz) |
| 32 | + { |
| 33 | + this.quiz = quiz; |
| 34 | + answers = new boolean[]{grade1You(), grade2Won(), grade3The(), grade4Game()}; |
| 35 | + displayScreen(); |
| 36 | + } |
| 37 | + public void paint(Graphics2D g, JPanel caller) |
| 38 | + { |
| 39 | + QuizUtils.displayScores(g, 300, answers); |
| 40 | + Tortoise.hide(); |
| 41 | + drawRewardShape(g); |
| 42 | + } |
| 43 | + public void drawRewardShape(Graphics2D g) |
| 44 | + { |
| 45 | + drawYou(g); |
| 46 | + drawWin(g); |
| 47 | + drawThe(g); |
| 48 | + drawGame(g); |
| 49 | + } |
| 50 | + private void drawGame(Graphics2D g) |
| 51 | + { |
| 52 | + quiz.template4 = ""; |
| 53 | + Pieces pieces = new Pieces(); |
| 54 | + quiz.question4(pieces); |
| 55 | + pieces.middle = "am"; |
| 56 | + String word = Parser.parse(quiz.template4, pieces); |
| 57 | + drawWord(g, word, 0, 4, true); |
| 58 | + } |
| 59 | + private void drawThe(Graphics2D g) |
| 60 | + { |
| 61 | + quiz.word3 = ""; |
| 62 | + Pieces model = new Pieces(); |
| 63 | + model.middle = "H"; |
| 64 | + quiz.question3("T{middle}E", model); |
| 65 | + drawWord(g, quiz.word3, 3, 2, false); |
| 66 | + } |
| 67 | + private void drawWin(Graphics2D g) |
| 68 | + { |
| 69 | + quiz.word2 = "WO"; |
| 70 | + quiz.question2("n"); |
| 71 | + drawWord(g, quiz.word2, 1, 0, false); |
| 72 | + } |
| 73 | + private void drawWord(Graphics2D g, String word, int x, int y, boolean horizontal) |
| 74 | + { |
| 75 | + char[] letters = word.toUpperCase().toCharArray(); |
| 76 | + int dx = horizontal ? 1 : 0; |
| 77 | + int dy = horizontal ? 0 : 1; |
| 78 | + for (int i = 0; i < letters.length; i++) |
| 79 | + { |
| 80 | + char c = letters[i]; |
| 81 | + drawLetter(getPosition(x + dx * i), getPosition(y + dy * i), c, g); |
| 82 | + } |
| 83 | + } |
| 84 | + private int getPosition(int i) |
| 85 | + { |
| 86 | + return 100 + i * 53; |
| 87 | + } |
| 88 | + private void drawYou(Graphics2D g) |
| 89 | + { |
| 90 | + quiz.word1 = "NOT"; |
| 91 | + quiz.question1("y", "u"); |
| 92 | + drawWord(g, quiz.word1, 0, 1, true); |
| 93 | + } |
| 94 | + private void drawLetter(int x, int y, char c, Graphics2D g) |
| 95 | + { |
| 96 | + g.setColor(Colors.Browns.BurlyWood); |
| 97 | + g.drawRect(x, y, 50, 50); |
| 98 | + g.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 36)); |
| 99 | + int charWidth = g.getFontMetrics().charWidth(c); |
| 100 | + int charHeight = g.getFontMetrics().getAscent(); |
| 101 | + int textY = y + (40 - charHeight) / 2 + charHeight; |
| 102 | + int textX = x + (50 - charWidth) / 2; |
| 103 | + g.drawString("" + c, textX, textY); |
| 104 | + } |
| 105 | + private boolean grade1You() |
| 106 | + { |
| 107 | + quiz.word1 = "fake"; |
| 108 | + quiz.question1("f", "o"); |
| 109 | + return "foo".equals(quiz.word1); |
| 110 | + } |
| 111 | + private boolean grade2Won() |
| 112 | + { |
| 113 | + quiz.word2 = "passe"; |
| 114 | + quiz.question2("d"); |
| 115 | + return "passed".equals(quiz.word2); |
| 116 | + } |
| 117 | + private boolean grade3The() |
| 118 | + { |
| 119 | + quiz.word3 = "fake"; |
| 120 | + quiz.question3("12{three}4", new Model("3")); |
| 121 | + return "1234".equals(quiz.word3); |
| 122 | + } |
| 123 | + private boolean grade4Game() |
| 124 | + { |
| 125 | + quiz.template4 = "fake"; |
| 126 | + quiz.question4(new Pieces()); |
| 127 | + return "g{middle}e".equals(quiz.template4); |
| 128 | + } |
| 129 | +} |
0 commit comments