import java.awt.*; public class FlashingText extends Sprite { // This Sprite renders text between two alternating colors. // The period of the Sprite determines how many frames // pass before changing the color of the text. private String text; private int x, y; private Color evenColor, oddColor; private int period; private Font font; boolean isEvenColor; public String name () { return "FlashingText"; } public String toString () { return name() + "[text="+this.text + "; position=("+this.x+","+this.y+")" + "; colors=("+this.evenColor+","+this.oddColor+")" + "; period="+this.period + "; font="+this.font + "]"; } public String getState () { return name() + "[color=" + getColor() + "]"; } // constructors public FlashingText (String text, int x, int y) { this.text = text; this.x = x; this.y = y; this.evenColor = Color.black; this.oddColor = Color.white; this.period = 5; this.font = new Font("Helvetica",Font.BOLD,36); this.resetState(); } public FlashingText (String text, int x, int y, Color c1, Color c2, int period) { this(text,x,y); this.evenColor = c1; this.oddColor = c2; this.period = period; } public void setFont (Font f) { this.font = f; } // required Sprite methods protected void resetState () { this.isEvenColor = true; } protected void drawState (Graphics g) { g.setColor(getColor()); g.setFont(this.font); g.drawString(text,x,y); } protected void updateState () { if ((getCurrentStateNumber()%period) == 0) { this.isEvenColor = !this.isEvenColor; } } private Color getColor () { if (isEvenColor) { return this.evenColor; } else { return this.oddColor; } } }