📄 ticker.java
字号:
/* * @(#)Ticker.java 1.55 01/08/09 * Copyright (c) 1999-2001 Sun Microsystems, Inc. All Rights Reserved. * * This software is the confidential and proprietary information of Sun * Microsystems, Inc. ("Confidential Information"). You shall not * disclose such Confidential Information and shall use it only in * accordance with the terms of the license agreement you entered into * with Sun. * * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING * THIS SOFTWARE OR ITS DERIVATIVES. */package javax.microedition.lcdui;/** * <p> 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. </p> * * <p> 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. </p> * * <p> The same ticker may be shared by several Screen objects. This can be * accomplished by calling {@link Screen#setTicker setTicker()} on all such * screens. 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. </p> * * <p> 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 Screen class so that * applications may implement this model without having to update the ticker * to be displayed as the user switches among screens. </p> */public class Ticker { /** The message to display in this Ticker */ private String message; /** The current location on screen of the scrolling message */ private int messageLoc; /** The pixel width of the message being displayed */ private int messageWidth; /** The Image to display with this Ticker */ private static final Image TICKER_IMG; // NOTE: the height of the TICKER_IMG is 1 pixel, the width // of the TICKER_IMG is 94 pixels /** TICK_RATE is the number of milliseconds between ticks */ static final long TICK_RATE = 250; /** * TICK_SPEED is the distance in pixels the message will * travel during one tick */ static final int TICK_SPEED = 5; /** * The preferred height of a Ticker is the same for * all Tickers */ static final int PREFERRED_HEIGHT; static { /** The image data for the Ticker's Image */ byte[] imgData = { (byte)0x89, (byte)0x50, (byte)0x4e, (byte)0x47, (byte)0x0d, (byte)0x0a, (byte)0x1a, (byte)0x0a, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x0d, (byte)0x49, (byte)0x48, (byte)0x44, (byte)0x52, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x5e, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x01, (byte)0x02, (byte)0x03, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x2a, (byte)0x64, (byte)0xf1, (byte)0xdf, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x0c, (byte)0x50, (byte)0x4c, (byte)0x54, (byte)0x45, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xbb, (byte)0xbb, (byte)0xbb, (byte)0x6a, (byte)0x6a, (byte)0x6a, (byte)0x02, (byte)0x02, (byte)0x02, (byte)0x6a, (byte)0x99, (byte)0x0e, (byte)0xd1, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x0d, (byte)0x49, (byte)0x44, (byte)0x41, (byte)0x54, (byte)0x78, (byte)0xda, (byte)0x63, (byte)0xf8, (byte)0x15, (byte)0x80, (byte)0x1d, (byte)0x02, (byte)0x00, (byte)0xc5, (byte)0x71, (byte)0x0f, (byte)0x79, (byte)0x9a, (byte)0x6a, (byte)0x17, (byte)0x2f, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x49, (byte)0x45, (byte)0x4e, (byte)0x44, (byte)0xae, (byte)0x42, (byte)0x60, (byte)0x82, }; TICKER_IMG = Image.createImage(imgData, 0, imgData.length); // The preferred height is the height of 1 line of text plus // the height of the top and bottom images (1 pixel each) // plus a pixel buffer (1 pixel top & bottom) around the message PREFERRED_HEIGHT = Screen.CONTENT_HEIGHT + 4; } /** * 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 */ public Ticker(String str) { synchronized (Display.LCDUILock) { setupText(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 */ public void setString(String str) { synchronized (Display.LCDUILock) { setupText(str); } } /** * Gets the string currently being scrolled by the ticker. * @return string of the ticker * * @see #setString */ public String getString() { // SYNC NOTE: return of atomic value, no locking necessary return message; } // package private, called by Screen /** * Paint the contents of this Ticker * * @param g The Graphics object to paint to */ void paintContent(Graphics g) { // Optimization: The TickerPainter in Screen sets the clip to be // Screen.CONTENT_HEIGHT. If the clip height is greater than that than // 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); } // NOTE: The default image is not transparent and thus this // code is optimized not to spend time erasing the backdrop // of an image which is not transparent. If a port does use // a transparent image you'll want to adjust the size of // the erased backdrop // We clear the entire backdrop for the message g.setColor(Display.ERASE_COLOR); g.fillRect(0, 2, Display.WIDTH, Screen.CONTENT_HEIGHT + 1); } // 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(messageLoc, 1, messageWidth, Screen.CONTENT_HEIGHT); g.setColor(Display.FG_COLOR); messageLoc -= TICK_SPEED; g.drawString(message, 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; } } // package private, called by Screen to reset the message to // the right side of the screen /** * Reset this Ticker's message location to the right side of the screen */ void reset() { messageLoc = Display.WIDTH; } // Called by both the constructor and the setString methods /** * Initialize this Ticker with the given text * * @param message The text this Ticker will display */ private final void setupText(String message) { if (message == null) { throw new NullPointerException(); } this.message = message; messageWidth = Screen.CONTENT_FONT.stringWidth(message); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -