熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java高級技術 >> 正文

線程控制動畫

2013-11-23 19:49:59  來源: Java高級技術 

  線程是程序的一部分是系統調度的基本單位線程是控制動畫的理想選擇將動畫的工作放在線程上可以釋放出程序的其他部分來處理別的任務
  線程的現實是通過javalang中的Thread類要使某一個類能使用線程必須實現Runnable接口該接口包含了唯一一個方法run()run()方法是線程類的核心動畫程序中產生運動通過調用線程的start ()方法致使run()方法被調用下邊這個程序描繪了一個運動中的圓
  
  import javaawt*;
  import javaawtevent*;
  import javaxswing*;
  
  public class Cartoon extends JApplet implements Runnable
  {
  Graphics screenBuffer = null;//創建圖形緩沖區
  Image screenImage = null;
  private Thread runner;
  private int x = ;
  private int move = ;
  
  public void init ( )
  {
  screenImage = createImage ( );
  screenBuffer = screenImagegetGraphics ( );
  }
  public void start ( )
  {
  if (runner == null)
  {
  runner = new Thread( this );
  runnerstart();
  }
  }
  public void run( )
  {
  Thread circle = ThreadcurrentThread ( );
  while ( runner == circle )//指向同一對象便開始運行
  {
  x += move;
  if ( ( x > ) || ( x < ))
  move *= ;
  repaint ( );
  }
  }
  public void drawCircle( Graphics gc )
  {
  GraphicsD gD = ( GraphicsD ) gc;
  gDsetColor ( Colorblue );
  gDfillRect ( );
  gDsetColor ( Coloryellow );
  gDfillRect ( );
  gDsetColor ( Colorred );
  gDfillOval ( x );
  }
  public void paint( Graphics g )
  {
  screenBuffersetColor ( Colorwhite );
  screenBufferfillRect ();
  drawCircle ( screenBuffer );
  //將緩沖區的圖像復制到主緩沖區中
  gdrawImage ( screenImage this );
  }
  }

From:http://tw.wingwit.com/Article/program/Java/gj/201311/27531.html
  • 上一篇文章:

  • 下一篇文章:
  • 推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.