scg/ch09/MultiplicationTable

From FANG

Jump to: navigation, search

001 package scg.ch09;
002 
003 import java.awt.Color;
004 
005 import fang2.core.Game;
006 import fang2.sprites.StringSprite;
007 
008 public class MultiplicationTable
009   extends Game {
010   /** The number of columns of numbers in the table */
011   public static final int COLUMNS = 10;
012 
013   /** The number of rows of numbers in the table */
014   public static final int ROWS = 6;
015 
016   /** StringSprite + space between them */
017   public static final double SPACING = 1.0 /
018     (+ Math.max(ROWS, COLUMNS));
019 
020   /** Scale of StringSprites (for leaving space) */
021   public static final double ACTUAL_SCALE = SPACING - 0.02;
022 
023   /** Color for entries labeling the table */
024   public static final Color LABEL_COLOR = getColor("misty rose");
025 
026   /** Offset from edge to labels (on top or left) */
027   public static final double LABEL_OFFSET = SPACING / 2.0;
028 
029   /** Color for entries in the table */
030   public static final Color TABLE_COLOR = getColor("yellow green");
031 
032   /** Offset to first row or column (top/left) */
033   public static final double TABLE_OFFSET = LABEL_OFFSET + SPACING;
034 
035   /**
036    * Setup all of the sprites.
037    */
038   @Override
039   public void setup() {
040     labelRows();
041     labelColumns();
042     fillProductTable();
043   }
044 
045   /**
046    * Fill in the multiplication table. row and column are the numbers
047    * being multiplied together in a given cell on the screen
048    */
049   private void fillProductTable() {
050     double yOffset = TABLE_OFFSET;
051     for (int row = 0; row != ROWS; ++row{
052       double xOffset = TABLE_OFFSET;
053       for (int column = 0; column != COLUMNS; ++column{
054         makeOneEntry(xOffset, yOffset, row * column, TABLE_COLOR);
055         xOffset += SPACING;
056       }
057       yOffset += SPACING;
058     }
059   }
060 
061   /**
062    * Generate label sprites down the left side of screen for rows
063    */
064   private void labelColumns() {
065     double yOffset = LABEL_OFFSET;
066     double xOffset = TABLE_OFFSET;
067     for (int column = 0; column != COLUMNS; ++column{
068       makeOneEntry(xOffset, yOffset, column, LABEL_COLOR);
069       xOffset += SPACING;
070     }
071   }
072 
073   /**
074    * Generate label sprites across top of screen for the columns
075    */
076   private void labelRows() {
077     double yOffset = TABLE_OFFSET;
078     double xOffset = LABEL_OFFSET;
079     for (int row = 0; row != ROWS; ++row{
080       makeOneEntry(xOffset, yOffset, row, LABEL_COLOR);
081       yOffset += SPACING;
082     }
083   }
084 
085   /**
086    * Create one StringSprite in the table or labels. All StringSprites
087    * in the table are right justified (so units digits all aligned
088    * vertically). The size of the sprites is the ACTUAL_SCALE which
089    * should be smaller then the SCALE which is used for spacing.
090    *
091    @param   x      x-coordinate of the sprite on the screen
092    @param   y      y-coordinate of the sprite on the screen
093    @param   value  numeric value for the text of the sprite
094    @param   color  color to give the sprite
095    *
096    @return  a reference to the newly created StringSprite
097    */
098   private StringSprite makeOneEntry(double x, double y, int value,
099     Color color{
100     StringSprite tableEntry = new StringSprite();
101     tableEntry.setScale(ACTUAL_SCALE);
102     tableEntry.setLocation(x, y);
103     tableEntry.setColor(color);
104     tableEntry.rightJustify();
105     tableEntry.setText(Integer.toString(value));
106     addSprite(tableEntry);
107     return tableEntry;
108   }
109 }
110 
111 //Uploaded on Mon Mar 29 21:39:26 EDT 2010


Download/View scg/ch09/MultiplicationTable.java





Views
Personal tools
Add to 
del.icio.usAdd to 
diggAdd to 
FacebookAdd to 
favoritesAdd to 
GoogleAdd to 
MySpaceAdd to 
PrintAdd to 
SlashdotAdd to 
StumbleUponAdd to 
Twitter

Games
Games