// =================================================================== // import // =================================================================== import java.applet.*; import java.awt.*; import java.util.*; import java.awt.image.*; // =================================================================== // draw // =================================================================== class KcFire { Image w_img; IndexColorModel w_icm; MemoryImageSource w_mi; int w_width; int w_hight; int w_pixels[]; int COLOR_TABLE_SIZE = 256; public KcFire(Applet oya, int width, int hight) { w_width = width; w_hight = hight; // --------- color table byte r[] = new byte[COLOR_TABLE_SIZE]; byte g[] = new byte[COLOR_TABLE_SIZE]; byte b[] = new byte[COLOR_TABLE_SIZE]; for (int i=0; i<COLOR_TABLE_SIZE/4; i++) { r[i] = (byte) (i*4); g[i] = 0; b[i] = 0; r[i+32] = (byte) 255; g[i+32] = (byte) (i*4); b[i+32] = 0; r[i+64] = (byte) 255; g[i+64] = (byte) 255; b[i+64] = (byte) (i*4); r[i+96] = (byte) 255; g[i+96] = (byte) 255; b[i+96] = (byte) 255; } w_icm = new IndexColorModel(8 , COLOR_TABLE_SIZE , r , g , b); // --------- pixels w_pixels = new int[w_width*w_hight]; for( int i = 0; i < w_width*w_hight; i ++ ) { w_pixels[i] = 0; } w_mi = new MemoryImageSource(w_width , w_hight , w_icm ,w_pixels , 0 , w_width); w_mi.setAnimated( true ); w_img = oya.createImage(w_mi); } void drawFire() { int pix_num = (w_hight-1)*w_width; // --------- 火種 for( int i = 0; i < w_width; i ++ ) { w_pixels[i+pix_num] = (int)(COLOR_TABLE_SIZE * Math.random()); } // --------- ぼかし for( int i = w_width; i < (w_width * w_hight)-1; i ++ ) { w_pixels[i-w_width] = (w_pixels[i] + w_pixels[i-1] + w_pixels[i+1] + w_pixels[i-w_width])/4 ; if( w_pixels[i-w_width] > 0 ) { w_pixels[i-w_width] --; } } } public void paint() { drawFire(); // --------- ピクセルが変更されたでよ w_mi.newPixels(); } public Image getImage() { return w_img; } } // =================================================================== // main // =================================================================== public class KcApp extends Applet implements Runnable { Thread w_thread = null; KcFire w_draw; int w_width; int w_hight; // --------------------------------- アプレット起動時に実行 public void init() { } // --------------------------------- アプレットがあるHTMLページに入ったときに実行 public void start() { if (w_thread == null) { // スレッドの起動 w_thread = new Thread(this); w_thread.start(); Dimension dim = getSize(); w_width = dim.width; //getWidth(); w_hight = dim.height; //getHeight(); w_draw = new KcFire( this, w_width, w_hight ); } } // --------------------------------- アプレットがあるHTMLページから出たときに実行 public void stop() { if (w_thread != null) { // スレッドの停止 w_thread = null; } } // --------------------------------- アプレット終了時に実行 public void destroy() { } // --------------------------------- 描画 public void paint(Graphics g) { if( w_draw == null ) return; g.drawImage(w_draw.getImage() ,0,0,this); } // --------------------------------- 再描画 public void update(Graphics g) { paint(g); } // --------------------------------- スレッドの実行 public void run() { Thread thisThread = Thread.currentThread(); while (w_thread == thisThread) { // 再描画 repaint(); if( w_draw != null ) { w_draw.paint(); } try { // 0.01秒間の停止 thisThread.sleep(10); } catch(Exception e) { } } } }