📄 tickermessage.java
字号:
package examples.awt;
import java.applet.*;
import java.awt.*;
/** A class to demonstrate how an applet can use
* a thread and parameters specified in HTML
*/
public class TickerMessage extends Applet
implements Runnable {
private String message;
private int delay;
private int stringWidth, x, y;
private Thread scrollingThread;
private static final Color textColor =
Color.black;
private boolean keepScrolling;
/** Initialize the applet and prepare to start
*/
public void init() {
Dimension mySize = getSize();
// get the message and period parameter values
// from the HTML file
message = getParameter( "message" );
if ( message == null ) {
message = "missing parameter 'message'";
}
String p = getParameter( "period" );
int period;
if ( p == null ) {
period = 20;
} else {
period = Integer.parseInt( p );
}
// Calculate the delay between repaints based
// on the size and specified period. This
// calculation is only very approximate because
// the time needed to do the painting is not
// accurately accounted for. Dividing the delay
// by an adjustment factor is a simple attempt.
delay = ( 1000 * period / mySize.width ) / 2;
// set the font and calculate the width and
// height using the font's metric information
Font f = new Font( "Helvetica", Font.BOLD, 18 );
setFont( f );
FontMetrics fm = getFontMetrics( f );
stringWidth = fm.stringWidth( message );
y = mySize.height / 2 + fm.getHeight() / 2;
x = 0;
}
/** Start the ticker applet running, creating a
* new thread as necessary
*/
public void start() {
if ( scrollingThread == null ) {
scrollingThread = new Thread( this );
keepScrolling = true;
scrollingThread.start();
}
}
/** Stop the ticker applet running
*/
public void stop() {
if ( scrollingThread != null ) {
keepScrolling = false;
scrollingThread = null;
}
}
/** Run the ticker. This method repaints
* the message repeatedly, which will cause it
* to move across the display. To save CPU,
* it will sleep between repaint operations.
*/
public void run() {
while ( keepScrolling ) {
repaint();
try {
Thread.sleep( delay );
} catch ( Exception e ) {
// ignore it
}
}
}
/** Paint the display area. This is
* called whenever the ticker starts
* or restarts.
*/
public void paint( Graphics g ) {
g.setColor( getBackground() );
g.fillRect( 0, 0,
getSize().width,
getSize().height );
}
/** Update the ticker on the display by
* "erasing" it at its current position
* and redrawing it at its new position,
* moved slightly to the left.
*/
public void update( Graphics g ) {
g.setColor( getBackground() );
g.drawString( message, x, y );
g.setColor( textColor );
x--;
if ( x < -stringWidth ) {
x = getSize().width;
}
g.drawString( message, x, y );
}
/** A very brief description of the applet
* @return the applet description
*/
public String getAppletInfo() {
return "Demonstration applet";
}
/** Provide information about the applet
* parameters
* @return a two-dimensional array containing
* parameter names, types, and
* descriptions
*/
public String[][] getParameterInfo() {
return new String[][] {
{ "message",
"String",
"The message to be displayed"
},
{ "period",
"int",
"The time (in sec) to display the message"
}
};
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -