採用多執行緒製作動畫(轉)
採用多執行緒製作動畫(轉)[@more@]import java.awt.*;
import java.applet.*;
//注意到這個程式和Hello程式有什麼不一樣嗎?在這個程式中多了implements Runnable。
public class carton extends Applet implements Runnable
{
Image img;
Thread thd = null;
int i;
int imgWidth = 150;
int imgHeight = 150;
int ncyc=1 ;
String namestr[] = new String[5] ;
//當執行緒被啟用時開始執行run()函式。
public void run()
{
for (int j=0; j<5; j++)
{
namestr[j] = Integer.toString(j,8)+".jpg" ;
}
ncyc = -1 ;
while (true)
{
if (ncyc<=3) ncyc= ncyc+1 ; //初始化迴圈控制引數
else ncyc = 0 ;
img = getImage(getCodeBase(), namestr[ncyc]) ;
if (img != null)
{
i=imgHeight;
//repaint();
}
try {Thread.sleep(1000);} catch (InterruptedException e){}
i=0;
while (i {
repaint();
try {Thread.sleep(50);} catch (InterruptedException e){}
i+=4;
}
}
}
//每次程式碼在新位置處重畫點陣圖,它都要呼叫repaint。該函式呼叫可過載的update方法。update方法與paint方法是相同的,這裡為啥不用怕paint(Graphics g)呢?除了paint方法在繪圖前要清除視窗,而update方法不清除(如果你把update方法改名為paint,你會看到有什麼不同)。
public void update(Graphics g)
{
if (img != null)
{
g.clipRect(0, 0, imgWidth, i);
g.drawImage(img, 0, i - imgHeight, null);
}
}
public void start()
{
if (thd == null)
{
thd = new Thread(this);
thd.start();
}
}
public void stop()
{
thd = null;
}
import java.applet.*;
//注意到這個程式和Hello程式有什麼不一樣嗎?在這個程式中多了implements Runnable。
public class carton extends Applet implements Runnable
{
Image img;
Thread thd = null;
int i;
int imgWidth = 150;
int imgHeight = 150;
int ncyc=1 ;
String namestr[] = new String[5] ;
//當執行緒被啟用時開始執行run()函式。
public void run()
{
for (int j=0; j<5; j++)
{
namestr[j] = Integer.toString(j,8)+".jpg" ;
}
ncyc = -1 ;
while (true)
{
if (ncyc<=3) ncyc= ncyc+1 ; //初始化迴圈控制引數
else ncyc = 0 ;
img = getImage(getCodeBase(), namestr[ncyc]) ;
if (img != null)
{
i=imgHeight;
//repaint();
}
try {Thread.sleep(1000);} catch (InterruptedException e){}
i=0;
while (i
repaint();
try {Thread.sleep(50);} catch (InterruptedException e){}
i+=4;
}
}
}
//每次程式碼在新位置處重畫點陣圖,它都要呼叫repaint。該函式呼叫可過載的update方法。update方法與paint方法是相同的,這裡為啥不用怕paint(Graphics g)呢?除了paint方法在繪圖前要清除視窗,而update方法不清除(如果你把update方法改名為paint,你會看到有什麼不同)。
public void update(Graphics g)
{
if (img != null)
{
g.clipRect(0, 0, imgWidth, i);
g.drawImage(img, 0, i - imgHeight, null);
}
}
public void start()
{
if (thd == null)
{
thd = new Thread(this);
thd.start();
}
}
public void stop()
{
thd = null;
}
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10617542/viewspace-958006/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 多執行緒資料採集執行緒
- 多執行緒-作業練習執行緒
- 作業系統:多執行緒作業系統執行緒
- 多執行緒應用執行緒
- .NET多執行緒程式設計(1):多工和多執行緒 (轉)執行緒程式設計
- 關於多執行緒(轉)執行緒
- 多執行緒和多執行緒同步執行緒
- 手撕Java多執行緒(四)執行緒之間的協作Java執行緒
- 多執行緒【執行緒池】執行緒
- 多執行緒--執行緒管理執行緒
- Java多執行緒——執行緒Java執行緒
- 執行緒與多執行緒執行緒
- pytest(13)-多執行緒、多程式執行用例執行緒
- .NET多執行緒程式設計(3):執行緒同步 (轉)執行緒程式設計
- ObjC 多執行緒簡析(一)-多執行緒簡述和執行緒鎖的基本應用OBJ執行緒
- VC多執行緒 C++ 多執行緒執行緒C++
- redis為什麼用單執行緒不用多執行緒Redis執行緒
- 多執行緒-執行緒控制之休眠執行緒執行緒
- 多執行緒-執行緒控制之加入執行緒執行緒
- 多執行緒-執行緒控制之禮讓執行緒執行緒
- 多執行緒-執行緒控制之中斷執行緒執行緒
- 多執行緒程式設計(轉)執行緒程式設計
- 多執行緒學習初步(轉)執行緒
- 玩轉java多執行緒 之多執行緒基礎 執行緒狀態 及執行緒停止實戰Java執行緒
- pytest多程式/多執行緒執行測試用例執行緒
- 多執行緒之初識執行緒執行緒
- Java多執行緒-執行緒中止Java執行緒
- Java多執行緒——執行緒池Java執行緒
- 多執行緒-執行緒概述等執行緒
- 多執行緒實用講解執行緒
- 用Delphi在工業控制和自動化實現多執行緒進行資料採集 (轉)執行緒
- 多執行緒系列(1),多執行緒基礎執行緒
- 多執行緒系列(二):多執行緒基礎執行緒
- 多執行緒------執行緒與程式/執行緒排程/建立執行緒執行緒
- 在Web應用程式中執行計劃任務(多執行緒) (轉)Web執行緒
- 多執行緒-執行緒控制之守護執行緒執行緒
- a、多執行緒執行緒
- MySQL併發複製系列二:多執行緒複製MySql執行緒