FileDocCategorySizeDatePackage
Ticker.javaAPI DocJ2ME MIDP 2.08867Thu Nov 07 12:02:28 GMT 2002javax.microedition.lcdui

Ticker

public class Ticker extends Object
Implements a "ticker-tape", a piece of text that runs continuously across the display. The direction and speed of scrolling are determined by the implementation. While animating, the ticker string scrolls continuously. That is, when the string finishes scrolling off the display, the ticker starts over at the beginning of the string.

There is no API provided for starting and stopping the ticker. The application model is that the ticker is always scrolling continuously. However, the implementation is allowed to pause the scrolling for power consumption purposes, for example, if the user doesn't interact with the device for a certain period of time. The implementation should resume scrolling the ticker when the user interacts with the device again.

The text of the ticker may contain line breaks. The complete text MUST be displayed in the ticker; line break characters should not be displayed but may be used as separators.

The same ticker may be shared by several Displayable objects ("screens"). This can be accomplished by calling {@link Displayable#setTicker setTicker()} on each of them. Typical usage is for an application to place the same ticker on all of its screens. When the application switches between two screens that have the same ticker, a desirable effect is for the ticker to be displayed at the same location on the display and to continue scrolling its contents at the same position. This gives the illusion of the ticker being attached to the display instead of to each screen.

An alternative usage model is for the application to use different tickers on different sets of screens or even a different one on each screen. The ticker is an attribute of the Displayable class so that applications may implement this model without having to update the ticker to be displayed as the user switches among screens.

since
MIDP 1.0

Fields Summary
private String
message
The message set in this Ticker
private String
displayedMessage
The message to display in this Ticker
private int
messageLoc
The current location on screen of the scrolling message
private int
messageWidth
The pixel width of the message being displayed
private int
tickSpeed
tickSpeed is the distance in pixels the message will travel during one tick
private static final Image
TICKER_IMG
The Image to display with this Ticker
static final long
TICK_RATE
TICK_RATE is the number of milliseconds between ticks
static final int
PREFERRED_HEIGHT
The preferred height of a Ticker is the same for all Tickers
static final int
DECORATION_HEIGHT
The height of the border around this ticker on one side
Constructors Summary
public Ticker(String str)
Constructs a new Ticker object, given its initial contents string.

param
str string to be set for the Ticker
throws
NullPointerException if str is null

        synchronized (Display.LCDUILock) {
            setupText(str);
        }
    
Methods Summary
public java.lang.StringgetString()
Gets the string currently being scrolled by the ticker.

return
string of the ticker
see
#setString

        // SYNC NOTE: return of atomic value, no locking necessary
        return message;
    
voidpaintContent(Graphics g)
Paint the contents of this Ticker

param
g The Graphics object to paint to

        // Optimization: The TickerPainter in Screen sets the clip to be
        // Screen.CONTENT_HEIGHT. If the clip height is greater than that,
        // then we go ahead and draw the images as well as the text
        if (g.getClipHeight() > Screen.CONTENT_HEIGHT) {

            // NOTE: We test to see if the width of the Display is
            // wider than the width of our image, if so, we re-draw
            // the image offset to the right as many times as necessary
            // to fill the width of the Display. Specific ports would
            // probably want to optimize this for the specific width
            // of their device.
            for (int imgLoc = 0; imgLoc < Display.WIDTH; imgLoc += 96) {
                // We draw the top border
                g.drawImage(TICKER_IMG, imgLoc, 0,
                            Graphics.TOP | Graphics.LEFT);
                // We draw the bottom border
                g.drawImage(TICKER_IMG, imgLoc, Screen.CONTENT_HEIGHT + 3,
                            Graphics.TOP | Graphics.LEFT);
            }
        }

        // We draw the text of the message regardless of the clip, because
        // its possible the paint call from TickerPainter has coalesced with
        // other paint calls and if we don't paint the text, we may miss our
        // chance to update the Ticker display. Note this has the side effect
        // of advancing the ticker on subsequent repaints even if there is not
        // a TickerPainter timer task firing repaints. This is ok because if
        // a Ticker is visible on the screen, it should always be "running".

        g.setColor(Display.ERASE_COLOR);
        g.fillRect(0, 2, Display.WIDTH, Screen.CONTENT_HEIGHT + 1);

        g.setColor(Display.FG_COLOR);
        messageLoc -= tickSpeed;
        g.drawString(displayedMessage, messageLoc, 2,
                     Graphics.TOP | Graphics.LEFT);

        // Once the message is completely off the left side of
        // the screen, we reset its location to be the right side
        // of the screen
        if (messageLoc <= -messageWidth) {
            messageLoc = Display.WIDTH;
        }
    
voidreset()
Reset this Ticker's message location to the right side of the screen

        messageLoc = Display.WIDTH;
    
public voidsetString(java.lang.String str)
Sets the string to be displayed by this ticker. If this ticker is active and is on the display, it immediately begins showing the new string.

param
str string to be set for the Ticker
throws
NullPointerException if str is null
see
#getString

        synchronized (Display.LCDUILock) {
            setupText(str);
        }
    
private final voidsetupText(java.lang.String message)
Initialize this Ticker with the given text

param
message The text this Ticker will display

        if (message == null) {
            throw new NullPointerException();
        }

        /*
         * Search the message for linebreak characters, and replace 
         * with spaces.
         */
        StringBuffer msg = new StringBuffer(message);
        int offset = 0;
        boolean modified = false;
        while ((offset = message.indexOf('\n", offset)) != -1) {
          msg.setCharAt(offset, ' ");
          offset++;
          modified = true;
        }

        /* 
         * Save the original unmodified message so that getString() 
         * returns that. If the message is modified because it 
         * contains linebreak characters, then set the display message
         * to the modified string.
         */
        this.message = message;
        this.displayedMessage = modified ? msg.toString() : message;
        messageWidth = Screen.CONTENT_FONT.stringWidth(this.displayedMessage);

        if (messageWidth < 5) { 
            tickSpeed = messageWidth;
        } else {
            tickSpeed = 5;
        }

	reset();