scg/ch05/EasyButton

From FANG

Jump to: navigation, search

01 package scg.ch05;
02 
03 import java.awt.Color;
04 
05 import fang2.attributes.Location2D;
06 import fang2.core.Game;
07 import fang2.sprites.CompositeSprite;
08 import fang2.sprites.RectangleSprite;
09 import fang2.sprites.StringSprite;
10 
11 /** A button class. Twice as wide as high with centered text message. */
12 public class EasyButton
13   extends CompositeSprite {
14   /** the button at the bottom of the screen */
15   private final RectangleSprite button;
16 
17   /** text displayed, centered, on the button */
18   private final StringSprite buttonMessage;
19 
20   /**
21    * Construct new button. Horizontal, FANGBlue with same color text.
22    */
23   public EasyButton() {
24     button = new RectangleSprite(1.00.5);
25     addSprite(button);
26     buttonMessage = new StringSprite();
27     addSprite(buttonMessage);
28   }
29 
30   /**
31    * Check if the button has been pressed.
32    *
33    @return  true if mouse clicked within the button, false otherwise.
34    */
35   public boolean isPressed() {
36     // The current game may have a mouse click or not.
37     Location2D mouseClick =
38       Game.getCurrentGame().getClick2D();
39     if (mouseClick != null{
40       if (intersects(mouseClick)) {
41         return true;
42       }
43     }
44     return false;
45   }
46 
47   /**
48    Set the color of the background rectangle
49    *
50    @param  color  the color to set the rectangle to.
51    */
52   @Override
53   public void setColor(Color color{
54     button.setColor(color);
55   }
56 
57   /**
58    Set text message; resize to fit in one line
59    *
60    @param  message  the new message for the button to display
61    */
62   public void setText(String message{
63     buttonMessage.setText(message);
64     // adjust size of text message to fit on the button
65     buttonMessage.setWidth(0.75);
66   }
67 
68   /**
69    Set the color of the text on the button.
70    *
71    @param  color  the color to set the text to
72    */
73   public void setTextColor(Color color{
74     buttonMessage.setColor(color);
75   }
76 }
77 
78 //Uploaded on Mon Mar 29 21:41:39 EDT 2010


Download/View scg/ch05/EasyButton.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