首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> Java 程式設計 >> Java 相關知識

[JAVA100例] 第73例:Applet 參數傳遞與雙緩衝文字滾動廣告

     編輯:Java 相關知識

本例綜合演示 Applet 參數獲取、雙緩衝繪圖技術、線程驅動動畫,實現帶陰影效果的文字橫向滾動廣告。

核心代碼結構

import java.awt.*;
import java.applet.*;

public class ShadowText extends Applet implements Runnable {
  private Image img, offI;
  private Graphics offG;
  private Thread thread = null;
  private int height, width;
  private String text;
  private int FontSize;
  private Font font;
  private int textcolor, backcolor, shadowcolor;

  public void init() {
    width = this.size().width;
    height = this.size().height;

    // 1. 獲取 HTML 參數(帶默認值)
    String s = getParameter("Text");
    text = (s != null) ? s : "Hello";

    s = getParameter("FontSize");
    FontSize = (s != null) ? Integer.parseInt(s) : 30;

    s = getParameter("Fore");
    textcolor = (s == null) ? 0x000000 : Integer.parseInt(s, 16);

    s = getParameter("Back");
    backcolor = (s == null) ? 0x000000 : Integer.parseInt(s, 16);

    s = getParameter("shadow");
    shadowcolor = (s == null) ? 0x000000 : Integer.parseInt(s, 16);

    this.setBackground(new Color(backcolor));

    // 2. 雙緩衝準備:預先繪製帶陰影的文字圖片
    img = createImage(width + 300, height);
    Graphics temp = img.getGraphics();
    temp.setColor(new Color(backcolor));
    temp.fillRect(0, 0, width, height);
    temp.setColor(new Color(shadowcolor));
    font = new Font("TimesRoman", Font.BOLD, FontSize);
    temp.setFont(font);
    temp.drawString(text, 10, height * 3 / 4);           // 陰影
    temp.setColor(new Color(textcolor));
    temp.drawString(text, 10 - 3, height * 3 / 4 - 3);   // 主文字(偏移產生陰影效果)

    // 3. 離屏緩衝區
    offI = createImage(width, height);
    offG = offI.getGraphics();
  }

  public void start() {
    if (thread == null) {
      thread = new Thread(this);
      thread.start();
    }
  }

  public void run() {
    int x = width;
    while (thread != null) {
      try {
        offG.drawImage(img, x, 0, this);  // 在離屏緩衝區繪製滾動圖片
        repaint();
        thread.sleep(20);
      } catch (InterruptedException e) {}
      x -= 3;
      if (x < -img.getWidth(this)) x = width;  // 循環滾動
    }
  }

  public void update(Graphics g) {
    paint(g);  // 避免默認 update 清屏導致閃爍
  }

  public void paint(Graphics g) {
    g.drawImage(offI, 0, 0, this);  // 將離屏緩衝區內容一次性繪製到屏幕
  }
}

HTML 參數配置示例

<applet code="ShadowText.class" width="400" height="100">
  <param name="Text" value="歡迎來到 Java 世界!">
  <param name="FontSize" value="36">
  <param name="Fore" value="FF0000">     <!-- 紅色文字 -->
  <param name="Back" value="FFFFCC">     <!-- 淡黃背景 -->
  <param name="shadow" value="808080">   <!-- 灰色陰影 -->
</applet>

關鍵技術點深度解析

  1. 參數傳遞:getParameter(String name) 從 HTML <param> 獲取配置,支援十六進制顏色解析 Integer.parseInt(s, 16)
  2. 雙緩衝技術:
    • 離屏緩衝區 offI(createImage 創建)
    • 先在離屏繪製完整幀,再 paint() 一次性拷貝到屏幕
    • 覆寫 update() 避免默認清屏導致閃爍
  3. 預渲染優化:img 預先繪製好帶陰影的長條文字圖片,滾動時只需 drawImage 位移,極大降低 CPU 佔用
  4. 線程動畫循環:實現 Runnable,run() 中控制位移與幀率(20ms/幀 ≈ 50fps)
  5. 生命週期:start() 啟動線程,stop()(未實現)應停止線程釋放資源

現代 Web 實現對比

技術Applet 方式現代 Web 方式
參數傳遞<param> + getParameter()HTML data-* 屬性 + JS 讀取
雙緩衝createImage + 手動管理Canvas 雙緩衝 / requestAnimationFrame
動畫循環Thread + sleep()requestAnimationFrame 遞歸調用
文字陰影手動偏移繪製兩次CSS text-shadow / Canvas shadowColor
滾動動畫手動位移 drawImageCSS animation / JS 修改 transform

遷移建議

純文字滾動廣告現代首選 CSS @keyframes + text-shadow,零 JS、GPU 加速、響應式適配;複雜動畫用 Canvas + requestAnimationFrame 或 Web Animations API。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved