📄 bufferedtickermessage.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 BufferedTickerMessage 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;
// the following fields were added to support
// double buffering
private Dimension offDim;
private Image offImage;
private Graphics offGraphics;
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 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 metrics 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
* 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 ) {
// set up the off-screen Graphics object
Dimension d = getSize();
if ( offGraphics == null
|| offDim.width != d.width
|| offDim.height != d.height ) {
offDim = d;
offImage = createImage( d.width,
d.height );
offGraphics = offImage.getGraphics();
}
// clear the off-screen Graphics object
offGraphics.setColor( getBackground() );
offGraphics.fillRect( 0, 0,
d.width, d.height );
offGraphics.setColor( textColor );
x--;
if ( x < -stringWidth ) {
x = getSize().width;
}
offGraphics.drawString( message, x, y );
// paint the image onto the screen
g.drawImage( offImage, 0, 0, this );
}
/** 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 + -