?? chap12-8.txt
字號:
// 程序12-8
import java.awt.event.*;
import java.awt.Graphics;
import java.awt.Font;
import java.applet.Applet;
public class brandishString extends Applet implements Runnable, MouseListener {
String str; // 要顯示的字符串
char strChars[ ]; // 字符串的字符數組表示
Thread runner = null; // 線程
boolean threadSuspended; // 線程的狀態
int strLength ; // 字符串的長度
static final int REGULAR_WD = 15; // 字符舞動的寬度
static final int REGULAR_HT = 36; // 字符舞動的高度
Font regularFont = new Font("Serif", Font.BOLD, REGULAR_HT); // 設置字體
public void init( ) {
str = getParameter("text"); // 獲取HTML文件中參數text的內容
if (str == null)
str = "Hello Java"; // 設置默認參數
strLength = str.length( );
strChars = new char[strLength];
str.getChars(0, strLength, strChars, 0); // 獲取字符數組
threadSuspended = false;
addMouseListener(this); // 當前對象自己監視自己
}
public void destroy( ) {
removeMouseListener(this); // 將當前對象從監聽者表中移出
}
public void start( ) {
runner = new Thread(this); // 創建一個線程
runner.start( ); // 啟動線程
}
public synchronized void stop( ) {
runner = null;
if (threadSuspended) {
threadSuspended = false;
notify( );
}
}
public void run( ) {
Thread me = Thread.currentThread( );
while (runner == me) {
try {
Thread.sleep(100);
synchronized(this) { // 對當前對象加鎖
if(threadSuspended)
wait( );
}
} catch (InterruptedException e){ } // 不需要編寫異常代碼
repaint( ); // 刷新屏幕
}
}
public void paint(Graphics g) {
int length = strChars.length;
for (int i = 0, x = 0; i < length; i++) {
g.setFont(regularFont);
int px = (int) (10 * Math.random( ) + x);
int py = (int) (10 * Math.random( ) + REGULAR_HT);
g.drawChars(strChars, i, 1, px, py); // 輸出一個字符
x += REGULAR_WD;
}
}
public synchronized void mousePressed(MouseEvent e) {
threadSuspended = !threadSuspended;
if (!threadSuspended)
notify( );
}
public void mouseEntered(MouseEvent e) { // 鼠標進入小程序,顯示Welcome
showStatus("Welcome");
}
public void mouseExited(MouseEvent e) { // 鼠標離開小程序,顯示 Bye...
showStatus("Bye ...");
}
// 本程序不需要下面兩個方法的功能,因此方法體為空
public void mouseReleased(MouseEvent e) {
}
public void mouseClicked(MouseEvent e) {
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -