Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/jdwsoftware/wfh/model/MathGame.java


1   package com.jdwsoftware.wfh.model;
2   
3   import javax.swing.JComponent;
4   import java.awt.Graphics;
5   import java.awt.Image;
6   import java.awt.Color;
7   import java.awt.Font;
8   
9   import java.util.Hashtable;
10  import java.util.Random;
11  
12  /**
13   * <p><b>Title</b>: Woody's Fishing Hole</p>
14   * <p><b>Description</b>: Educational Fishing Game</p>
15   * <p><b>Copyright</b>: Copyright (C) 2003 JDW Software, LLC</p>
16   * <p><b>Company</b>: JDW Software, LLC</p>
17   *
18   * <p>This game diplays an simple math problem, a number 1-9 a
19   *    plus sign and another number 1-9.  The answers are the sum
20   *    of the two numbers</p>
21   *
22   * @author <a href="mailto:jdw@jdwsoftware.com">Jim Woodgate</a>
23   * @version $Revision: 1.8 $
24   */
25  
26  /*
27   * This file is part of Woodys Fishing Hole.
28   *
29   * Woodys Fishing Hole is free software; you can redistribute it
30   * and/or modify it under the terms of the GNU General Public License
31   * as published by the Free Software Foundation; either version 2 of
32   * the License, or (at your option) any later version.
33   *
34   * Woodys Fishing Hole is distributed in the hope that it will be
35   * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
36   * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
37   * GNU General Public License for more details.
38   *
39   * You should have received a copy of the GNU General Public License
40   * along with Woodys Fishing Hole; if not, write to the Free Software
41   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
42   * USA
43   */
44  
45  public class MathGame extends GameType {
46    private static final String COPYRIGHT = com.jdwsoftware.util.Copyright.COPYRIGHT;
47    private static final String VERSION = "$Id: MathGame.java,v 1.8 2003/12/02 04:22:32 jdw Exp $";
48  
49    private static final String[] ANSWERS = new String[] {" 0"," 1"," 2"," 3",
50                                                          " 4"," 5"," 6"," 7",
51                                                          " 8"," 9","10","11",
52                                                          "12","13","14","15",
53                                                          "16","17","18"};
54  
55    private Hashtable _images;
56    private Image     _answerImage;
57    private Font      _bigFont;
58  
59    private String _question;
60    private static Random r = new Random();
61  
62    public MathGame(JComponent caller) {
63      super(caller);
64      _answerImage = _caller.createImage(getWidth()*3/2, getHeight());
65      Graphics g = _answerImage.getGraphics();
66      g.setColor(Color.black);
67      g.fillRect(0,0, getWidth()*3/2, getHeight());
68  
69      _images = new Hashtable();
70  
71      _bigFont = new java.awt.Font("Monospaced", 1, 32);
72      for (int i=0; i<ANSWERS.length; i++) {
73        String key = ANSWERS[i];
74        Image image = _caller.createImage(getWidth(), getHeight());
75        g = image.getGraphics();
76        g.setColor(Color.black);
77        g.fillRect(0,0,getWidth(),getHeight());
78        g.setColor(Color.white);
79        g.fillRect(BORDER, BORDER, getWidth()-2*BORDER, getHeight()-2*BORDER);
80        g.setColor(Color.black);
81        g.setFont(_bigFont);
82        g.drawString(key, BORDER*3/2-1, BORDER+_bigFont.getSize());
83        _images.put(key, image);
84      }
85      newQuestion();
86    }
87  
88    public synchronized void newQuestion() {
89      int x = r.nextInt(9)+1;
90      int y = r.nextInt(9)+1;
91      _question = x+y<10 ? " "+(x+y) : ""+(x+y);
92      Graphics g = _answerImage.getGraphics();
93      g.setColor(Color.white);
94      g.fillRect(BORDER, BORDER, getWidth()*3/2-2*BORDER, getHeight()-2*BORDER);
95      g.setColor(Color.black);
96      g.setFont(_bigFont);
97      g.drawString(""+x+"+"+y, BORDER*3/2-1, BORDER+_bigFont.getSize());
98    }
99  
100   public Image getQuestion() {
101     return _answerImage;
102   }
103 
104   public String getAnswer() {
105     return _question;
106   }
107 
108   public String[] getValidAnswers() {
109     return ANSWERS;
110   }
111 
112   public Image getAnswer(String s) {
113     return (Image)_images.get(s);
114   }
115 
116 }