scg/ch07/GameTile

From FANG

Jump to: navigation, search

001 package scg.ch07;
002 
003 import java.awt.Color;
004 
005 import fang2.attributes.Location2D;
006 import fang2.core.Game;
007 import fang2.sprites.CompositeSprite;
008 import fang2.sprites.RectangleSprite;
009 import fang2.sprites.StringSprite;
010 
011 /**
012  * GameTile class. Provides a tile for a game of Tic-Tac-Toe (or any
013  * similar game). The tile can display a single character in front of a
014  * solid background. The tile can also report what their content is,
015  * whether or not they are occupied, and whether or not they have been
016  * clicked on.
017  */
018 public class GameTile
019   extends CompositeSprite {
020   /** the constant (final), class-wide (staticColor for the tile */
021   public static final Color DEFAULT_COLOR = 
022     Game.getColor("dark violet");
023 
024   /** the constant (final), class-wide (staticColor for the text */
025   public static final Color DEFAULT_CONTENT_COLOR = 
026     Game.getColor("lavender");
027 
028   /** The background of the tile */
029   private final RectangleSprite background;
030 
031   /** Current content of this tile */
032   private String content;
033 
034   /** the visible content of the tile */
035   private final StringSprite displayContent;
036 
037   /**
038    * Construct a new GameTile: content = ""
039    */
040   public GameTile() {
041     background = new RectangleSprite(1.01.0);
042     background.setColor(DEFAULT_COLOR);
043     addSprite(background);
044     content = "";
045     displayContent = new StringSprite();
046     displayContent.setLocation(0.000.075);
047     displayContent.setColor(DEFAULT_CONTENT_COLOR);
048     addSprite(displayContent);
049   }
050 
051   /**
052    * Get the current content of the tile.
053    *
054    @return  the content
055    */
056   public String getContent() {
057     return content;
058   }
059 
060   /**
061    * Is this tile occupied by one of the players. A tile is clear if the
062    * content string is empty, occupied if it has any non-empty value.
063    *
064    @return  true if tile is occupied; false otherwise.
065    */
066   public boolean isOccupied() {
067     return content.length() != 0;
068   }
069 
070   /**
071    * Was the mouse belonging to the given player just clicked inside of
072    this tile? Uses currentGame to read the input (using getClick2D).
073    *
074    @param   playerID  the id number of player whose turn it is.
075    *
076    @return  true if mouse clicked within the background, false
077    *          otherwise.
078    */
079   public boolean isPressed(int playerID{
080     // The current game may have a mouse click or not.
081     Location2D mouseClick = Game.getCurrentGame().getClick2D(playerID);
082     if (mouseClick != null{
083       if (intersects(mouseClick)) {
084         return true;
085       }
086     }
087     return false;
088   }
089 
090   /**
091    Set the color of the background rectangle
092    *
093    @param  color  the color to set the rectangle to.
094    */
095   @Override
096   public void setColor(Color color{
097     background.setColor(color);
098   }
099 
100   /**
101    Set the tile content to the given value. A single character is
102    * expected; if a multi-character string is passed in, the content
103    * will be set to the first character in the string.
104    *
105    @param  content  the new content value
106    */
107   public void setContent(String content{
108     if (content.length() 1{
109       content = content.substring(01);
110     }
111     this.content = content;
112     setText(content);
113   }
114 
115   /**
116    Set the color of the text on the background.
117    *
118    @param  color  the color to set the text to
119    */
120   public void setTextColor(Color color{
121     displayContent.setColor(color);
122   }
123 
124   /**
125    Set text message; resize to fit in one line
126    *
127    @param  message  the new message for the background to display
128    */
129   private void setText(String message{
130     displayContent.setText(message);
131     displayContent.setWidth(0.75);
132   }
133 }
134 
135 //Uploaded on Mon Mar 29 21:42:29 EDT 2010


Download/View scg/ch07/GameTile.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