// Very old code from college project in 1996-1997 - // I've gotten a lot better at programming since then! import java.applet.*; import java.awt.*; import java.net.*; ////////////////////////////// // Extensions to Canvas, // // enabling graphics // // functions to be accessed // ////////////////////////////// class Title extends Canvas { Image TitleGraphic; public Title(Image inImage) { TitleGraphic=inImage; } // end constructor public void paint (Graphics g) { g.setColor (Color.yellow); g.fillRect (1,0,504,56); g.drawImage (TitleGraphic, 10, 2, 370, 52, this); g.setColor (Color.red); g.drawString("'S SMARTIE QUIZ",376,52); g.setColor (Color.black); g.drawRect (1,0,504,56); } // end paint } // end Title class class Question1 extends Canvas { Image CrossGraphic; Image TickGraphic; int Mark; public Question1(Image inImage1, Image inImage2) { Mark =0; // Initial value is undecided CrossGraphic =inImage1; // Set up Cross Graphic TickGraphic =inImage2; // Set up Tick Graphic } // end constructor public void paint (Graphics g) { g.setColor (Color.white); g.fillRect (1,1,247,56); g.setColor (Color.magenta); g.drawString("1.",10,20); g.setColor (Color.black); g.drawRect (1,1,247,56); g.drawString("Which colour has never been used for",30,20); g.drawString("Smartie lids? (This changed twice recently!)",30,35); if (Mark==1) g.drawImage(CrossGraphic, 10, 22, 20, 30, this); if (Mark==2) g.drawImage(TickGraphic, 10, 22, 20, 30, this); } // end paint method public void SetMark(int inMark) { Mark = inMark; } } // end Question1 class Question2 extends Canvas { Image CrossGraphic; Image TickGraphic; int Mark; public Question2(Image inImage1, Image inImage2) { Mark=0; // Initial value is undecided CrossGraphic=inImage1; // Set up Cross Graphic TickGraphic=inImage2; // Set up Tick Graphic } // end constructor public void paint (Graphics g) { g.setColor(Color.white); g.fillRect(1,1,247,56); g.setColor(Color.orange); g.drawString("2.",10,20); g.setColor(Color.black); g.drawRect(1,1,247,56); g.drawString("In the 'Missing Blue Smarties' mystery,",30,20); g.drawString("WHO kidnapped the blue Smarties?",30,35); if (Mark==1) g.drawImage(CrossGraphic, 10, 22, 20, 30, this); if (Mark==2) g.drawImage(TickGraphic, 10, 22, 20, 30, this); } // end paint method public void SetMark(int inMark) { Mark = inMark; } } // end Question2 class Question3 extends Canvas { Image CrossGraphic; Image TickGraphic; int Mark; public Question3(Image inImage1, Image inImage2) { Mark=0; // Initial value is undecided CrossGraphic=inImage1; // Set up Cross Graphic TickGraphic=inImage2; // Set up Tick Graphic } // end constructor public void paint (Graphics g) { g.setColor(Color.white); g.fillRect(1,1,247,56); g.setColor(Color.green); g.drawString("3.",10,20); g.setColor(Color.black); g.drawRect(1,1,247,56); g.drawString("Pocket sized electronic noise generators",30,20); g.drawString("were given away with which promotion?",30,35); if (Mark==1) { g.drawImage(CrossGraphic, 10, 22, 20, 30, this); } if (Mark==2) { g.drawImage(TickGraphic, 10, 22, 20, 30, this); } } // end paint method public void SetMark(int inMark) { Mark = inMark; } // end SetMark method } // end Question3 class Question4 extends Canvas { Image CrossGraphic; Image TickGraphic; int Mark; public Question4(Image inImage1, Image inImage2) { Mark=0; // Initial value is undecided CrossGraphic=inImage1; // Set up Cross Graphic TickGraphic=inImage2; // Set up Tick Graphic } // end constructor public void paint (Graphics g) { g.setColor(Color.white); g.fillRect(1,1,247,56); g.setColor(Color.red); g.drawString("4.",10,20); g.setColor(Color.black); g.drawRect(1,1,247,56); g.drawString("Recently a promotion gave away 'Smartian'",30,20); g.drawString("toys. Besides being from outer space,",30,35); g.drawString("what was special about them?",30,50); if (Mark==1) { g.drawImage(CrossGraphic, 10, 22, 20, 30, this); } if (Mark==2) { g.drawImage(TickGraphic, 10, 22, 20, 30, this); } } // end paint method public void SetMark(int inMark) { Mark = inMark; } // end SetMark method } // end Question4 class Question5 extends Canvas { Image CrossGraphic; Image TickGraphic; int Mark; public Question5(Image inImage1, Image inImage2) { Mark=0; // Initial value is undecided CrossGraphic=inImage1; // Set up Cross Graphic TickGraphic=inImage2; // Set up Tick Graphic } // end constructor public void paint (Graphics g) { g.setColor(Color.white); g.fillRect(1,1,247,56); g.setColor(Color.blue); g.drawString("5.",10,20); g.setColor(Color.black); g.drawRect(1,1,247,56); g.drawString("Which of these is NOT a",30,20); g.drawString("'Smartian'?",30,35); if (Mark==1) { g.drawImage(CrossGraphic, 10, 22, 20, 30, this); } if (Mark==2) { g.drawImage(TickGraphic, 10, 22, 20, 30, this); } } // end paint method public void SetMark(int inMark) { Mark = inMark; } // end SetMark method } // end Question5 class Results extends Canvas { Image Sad; Image Neutral; Image Happy; int Face; String DisplayString; public Results(Image inImage1, Image inImage2, Image inImage3) { Face=0; Sad=inImage1; Neutral=inImage2; Happy=inImage3; DisplayString="Make your choices and press the button!"; } // end constructor public void paint (Graphics g) { g.setColor(Color.yellow); g.fillRect(1,3,504,56); g.setColor(Color.red); g.drawString(DisplayString,160,35); g.setColor(Color.black); g.drawRect(1,3,504,56); if (Face==1) { g.drawImage(Sad, 20, 5, 53, 53, this); } if (Face==2) { g.drawImage(Neutral, 20, 5, 53, 53, this); } if (Face==3) { g.drawImage(Happy, 20, 5, 53, 53, this); } } // end paint public void SetFace(int inFace) { Face = inFace; } // end SetFace method public void SetText(String inString) { DisplayString = inString; } // end SetText method } // end Results class ////////////////////////// // Extensions to Panel, // // enabling components // // to be created // ////////////////////////// class Problem1 extends Panel { List Answers; Image CrossGraphic; Image TickGraphic; Question1 Question; public Problem1 (Image inGraphic1, Image inGraphic2) { CrossGraphic=inGraphic1; TickGraphic =inGraphic2; Answers = new List(6,false); Question=new Question1(CrossGraphic, TickGraphic); Answers.addItem("Red"); Answers.addItem("Transparent"); Answers.addItem("Silver"); Answers.addItem("Yellow"); Answers.addItem("Black"); Answers.addItem("White"); setLayout(new GridLayout(1,2)); add(Question); add(Answers); } // end Problem1 constructor public String getSelection() { return Answers.getSelectedItem(); } // end checkAnswer } // end Problem1 class Problem2 extends Panel { List Answers; Image CrossGraphic; Image TickGraphic; Question2 Question; public Problem2 (Image inGraphic1, Image inGraphic2) { CrossGraphic=inGraphic1; TickGraphic =inGraphic2; Question=new Question2(CrossGraphic, TickGraphic); Answers = new List(5,false); Answers.addItem("Donald"); Answers.addItem("Henrietta"); Answers.addItem("Big Reg"); Answers.addItem("Dr Devious"); Answers.addItem("Billy The Bonce"); setLayout(new GridLayout(1,2)); add(Question); add(Answers); } // end Problem2 constructor public String getSelection() { return Answers.getSelectedItem(); } // end checkAnswer } // end Problem2 class Problem3 extends Panel { List Answers; Image CrossGraphic; Image TickGraphic; Question3 Question; public Problem3 (Image inGraphic1, Image inGraphic2) { CrossGraphic=inGraphic1; TickGraphic =inGraphic2; Question=new Question3(CrossGraphic, TickGraphic); Answers = new List(5,false); Answers.addItem("Cool Dude Purple"); Answers.addItem("Gruesome Greenies"); Answers.addItem("Zapping Orange"); Answers.addItem("Missing Blue Smarties"); Answers.addItem("Hologram Magic Tubes"); setLayout(new GridLayout(1,2)); add(Question); add(Answers); } // end Problem3 constructor public String getSelection() { return Answers.getSelectedItem(); } // end checkAnswer } // end Problem3 class Problem4 extends Panel { List Answers; Image CrossGraphic; Image TickGraphic; Question4 Question; public Problem4 (Image inGraphic1, Image inGraphic2) { CrossGraphic=inGraphic1; TickGraphic =inGraphic2; Question=new Question4(CrossGraphic, TickGraphic); Answers = new List(5,false); Answers.addItem("They glowed in the dark"); Answers.addItem("They squeeked"); Answers.addItem("They squirted water"); Answers.addItem("They inflated"); Answers.addItem("They floated"); setLayout(new GridLayout(1,2)); add(Question); add(Answers); } // end Problem4 constructor public String getSelection() { return Answers.getSelectedItem(); } // end checkAnswer } // end Problem4 class Problem5 extends Panel { List Answers; Image CrossGraphic; Image TickGraphic; Question5 Question; public Problem5(Image inGraphic1, Image inGraphic2) { CrossGraphic=inGraphic1; TickGraphic =inGraphic2; Question=new Question5(CrossGraphic, TickGraphic); Answers = new List(5,false); Answers.addItem("Smorg"); Answers.addItem("Babok"); Answers.addItem("Yoz"); Answers.addItem("Zock"); Answers.addItem("Zim"); setLayout(new GridLayout(1,2)); add(Question); add(Answers); } // end Problem5 constructor public String getSelection() { return Answers.getSelectedItem(); } // end checkAnswer } // end Problem5 /////////////////////////// // Extensions to Applet, // // enabling components // // to be linked together // /////////////////////////// public class quiz extends java.applet.Applet { Image TitleGraphic, CrossGraphic, TickGraphic; Image SadGraphic, NeutralGraphic, HappyGraphic; Title ti; Problem1 q1; Problem2 q2; Problem3 q3; Problem4 q4; Problem5 q5; Button Submit; Results ResultsDisplay; int score=0; String Retrieve; public void init() { URL TitleURL, CrossURL, TickURL; URL SadURL, NeutralURL, HappyURL; try { TitleURL = new URL (getDocumentBase(),"/smarties/quiz/java/title.gif"); CrossURL = new URL (getDocumentBase(),"/smarties/quiz/java/mark_wrong.gif"); TickURL = new URL (getDocumentBase(),"/smarties/quiz/java/mark_right.gif"); SadURL = new URL (getDocumentBase(),"/smarties/quiz/java/face_sad.gif"); NeutralURL = new URL (getDocumentBase(),"/smarties/quiz/java/face_neutral.gif"); HappyURL = new URL (getDocumentBase(),"/smarties/quiz/java/face_happy.gif"); } catch (MalformedURLException e) { System.out.println("Bad URL... Image not found"); return; } TitleGraphic = getImage(TitleURL); CrossGraphic = getImage(CrossURL); TickGraphic = getImage(TickURL); SadGraphic = getImage(SadURL); NeutralGraphic = getImage(NeutralURL); HappyGraphic = getImage(HappyURL); ti=new Title(TitleGraphic); q1=new Problem1(CrossGraphic, TickGraphic); q2=new Problem2(CrossGraphic, TickGraphic); q3=new Problem3(CrossGraphic, TickGraphic); q4=new Problem4(CrossGraphic, TickGraphic); q5=new Problem5(CrossGraphic, TickGraphic); Submit = new Button ("Mark Smartie Exam"); ResultsDisplay=new Results(SadGraphic, NeutralGraphic, HappyGraphic); setLayout(new GridLayout(8,1)); add(ti); add(q1); add(q2); add(q3); add(q4); add(q5); add(Submit); add(ResultsDisplay); } // end init public boolean action (Event evt, Object arg) { if (evt.target instanceof Button) { score=0; Retrieve=(q1.getSelection()); if (!Retrieve.equals("Transparent")) { q1.Question.SetMark(1); } else { q1.Question.SetMark(2); score++; } q1.Question.repaint(); Retrieve=(q2.getSelection()); if (!Retrieve.equals("Billy The Bonce")) { q2.Question.SetMark(1); } else { q2.Question.SetMark(2); score++; } q2.Question.repaint(); Retrieve=(q3.getSelection()); if (!Retrieve.equals("Zapping Orange")) { q3.Question.SetMark(1); } else { q3.Question.SetMark(2); score++; } q3.Question.repaint(); Retrieve=(q4.getSelection()); if (!Retrieve.equals("They glowed in the dark")) { q4.Question.SetMark(1); } else { q4.Question.SetMark(2); score++; } q4.Question.repaint(); Retrieve=(q5.getSelection()); if (!Retrieve.equals("Zim")) { q5.Question.SetMark(1); } else { q5.Question.SetMark(2); score++; } q5.Question.repaint(); switch(score) { case 0: ResultsDisplay.SetFace(1); ResultsDisplay.SetText("Score = 0 That wasn't very good was it?"); break; case 1: ResultsDisplay.SetFace(1); ResultsDisplay.SetText("Score = 1 Smarties aren't really your bag are they?"); break; case 2: ResultsDisplay.SetFace(1); ResultsDisplay.SetText("Score = 2 Well I've seen better."); break; case 3: ResultsDisplay.SetFace(2); ResultsDisplay.SetText("Score = 3 That's not too bad..."); break; case 4: ResultsDisplay.SetFace(2); ResultsDisplay.SetText("Score = 4 That's pretty good! I'm impressed!"); break; case 5: ResultsDisplay.SetFace(3); ResultsDisplay.SetText("Score = 5 YES! YES! YES! YES!"); break; } ResultsDisplay.repaint(); score=0; return true; } return false; } // end action } // End SmartieQuiz