// // // Java Plasma written 1997 Martin Steen // // import java.awt.*; import java.awt.image.*; public class ms_plasma extends java.applet.Applet implements Runnable { Image img; int w; int h; Thread Timer; byte[] pix; byte[] newpal; byte[][] ptab; IndexColorModel icm; short[] pl1,pl2; byte[] data; int plastime = 0; int plasma_size; public void init() { w = size().width; h = size().height; resize(w,h); plasma_size = 1; switch(w) { case 80: plasma_size = 0; break; case 320: plasma_size = 2; break; } pix = new byte[w*h]; newpal = new byte[768]; ptab = new byte[3][256]; make_palette(); init_data(); img = createImage(new MemoryImageSource(w,h,icm,pix,0,w)); } public void init_data() { int t,x,y; pl1 = new short[30000]; pl2 = new short[30000]; data = new byte[70000]; for (y = 0; y < 256; y++) { int offs = y * 256; int ym = y - 128; for (x = 0; x < 256; x++) { int xm = x - 128; data[offs+x] = (byte) (83 * (3+(Math.cos(0.09*Math.sqrt(xm*xm + ym*ym))) + Math.cos(x/21.0)+Math.cos(y/26.0))); } } for (t = 0; t < 30000; t++) { pl1[t] = (short) (48+Math.cos(t/37.0)*47.0+256*(short)(48+47*(Math.sin(t/31.0)))); pl2[t] = (short) (48+Math.sin(t/24.0)*47.0+256*(short)(48+47*(Math.cos(t/19.0)))-pl1[t]); } } public void make_palette() { int j = 0, i = 0; for (int r = 0; r < 256; r++) { for (int t = 64; t <= 256; t <<= 1) { newpal[i++] = (byte) ((Math.sin(r * Math.PI * 2 / t) * 31.0 + 31)*4); } } for (i = 0; i < 256; i++) { ptab[0][i] = newpal[j++]; ptab[1][i] = newpal[j++]; ptab[2][i] = newpal[j++]; } icm = new IndexColorModel(8,256,ptab[0],ptab[1],ptab[2]); } public void calc_plasma(int t) { short p2 = pl2[t]; short x,y; int si = 0; int di; di = (int) pl1[t]; si = 0; switch (plasma_size) { case 2: { byte p; for (y = 0; y < 100; y++) { for (x = 0; x < 160; x++) { p = (byte) (data[di++] + data[di+p2]); pix[si+320] = p; pix[si+321] = p; pix[si++] = p; pix[si++] = p; } di += 96; si += 320; } } break; case 1: { for (y = 0; y < 100; y++) { for (x = 0; x < 160; x++) { pix[si++] = (byte) (data[di++] + data[di+p2]); } di += 96; } } break; case 0: { for (y = 0; y < 100; y += 2) { for (x = 0; x < 160; x += 2) { pix[si++] = (byte) (data[di++] + data[di+p2]); di++; } di += 96 + 256; } } break; } } public void start() { if (Timer == null) { Timer = new Thread(this); Timer.start(); } } public void run() { int x,y,red,blue,index,gr; Timer.setPriority(Thread.MIN_PRIORITY); while (true) { repaint(); try{Thread.sleep(10);} catch (InterruptedException e){} } } public void update(Graphics g) { calc_plasma(plastime++); if (plastime == 30000) plastime = 0; img.flush(); g.drawImage(img,0,0,this); } public void paint(Graphics g) { img.flush(); g.drawImage(img,0,0,this); } }