⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tickermessage.java

📁 程序练习中包括书中实例程序代码和练习中要用到的代码,是压缩文件
💻 JAVA
字号:
package examples.applet;
import javax.swing.*;
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
/** A class to demonstrate how an applet can use
  * a thread and parameters specified in HTML
  */
public class TickerMessage extends JApplet
                           implements Runnable {
   private int delay;
   private Thread scrollingThread;
   private boolean keepScrolling;
   /** Initialize the applet and prepare to start
     */
   public void init() {
      String 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 / getSize().width ) / 2;
      TickerPanel tp = new TickerPanel( message );
      tp.setBorder( BorderFactory.createTitledBorder(
                    "Ticker Message" ) );
      setContentPane( tp );
   }
   /** 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
     * sleeps between repaint operations.
     */
   public void run() {
      while ( keepScrolling ) {
         repaint();
         try {
            Thread.sleep( delay );
         } catch ( InterruptedException e ) {
            e.printStackTrace();
         }
      }
   }
   /** 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"
         }
      };
   }
   /** Implements the applet's panel where the message
     * will be scrolled.
     */
   public class TickerPanel extends JPanel {
      private int strWidth, strHeight, x, y;
      private final int pixelShift = 1;
      private String msg;
      private final Color textColor = Color.black;
      private Rectangle clip;
      private Insets i;
      /** Class constructor
        * @param message The ticker message
        */
      public TickerPanel( String message ) {
         msg = message;
         Font f = new Font( "Helvetica", Font.BOLD, 18 );
         setFont( f );
         FontMetrics fm = getFontMetrics( f );
         strWidth = fm.stringWidth( msg );
         strHeight = fm.getHeight();
         addComponentListener( new ComponentAdapter() {
               public void
               componentResized( ComponentEvent e ) {
                  y = getSize().height / 2 + strHeight / 2;
                  x = getSize().width / 2 - strWidth / 2;
                  i = getInsets();
                  clip = new Rectangle( i.left, i.top,
                     getSize().width - i.left
                                     - i.right - 1,
                     getSize().height - i.top
                                      - i.bottom - 1 );
               }
            }
         );
      }
      /** Paint the panel with the message text
        * @param g The panel's graphics context
        */
      public void paintComponent( Graphics g ) {
         super.paintComponent( g );
         Rectangle clipOrig = g.getClipBounds();
         g.setClip( clip );
         g.setColor( getBackground() );
         g.drawString( msg, x, y );
         g.setColor( textColor );
         x -= pixelShift;
         if ( x < ( -strWidth + i.left ) ) {
            x = getSize().width - i.right;
         }
         g.drawString( msg, x, y );
         g.setClip( clipOrig );
      }
   }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -