Thanks BurnPizza and Riven!
Updated with a polar plasma.
//****************************************************************************
//
// Simple Per Pixel Plasma Generator
// Richard Eric Lope (relminator)
// http://rel.phatcode.net
//
// Thanks to BurntPizza and Riven for teaching me how to
// do a fast pixel write directly to a buffered image
//
//****************************************************************************
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Toolkit;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferInt;
import javax.swing.JFrame;
import javax.swing.JPanel;
//****************************************************************************
//
// Main Entry Point
//
//****************************************************************************
public class SimplePlasma extends JFrame
{
private static final long serialVersionUID = 1L;
final static int SCREEN_WIDTH = 640;
final static int SCREEN_HEIGHT = 480;
public SimplePlasma()
{
add( new Screen() );
setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
setSize( SCREEN_WIDTH, SCREEN_HEIGHT );
setLocationRelativeTo( null );
setTitle( "Plazma by Relminator" );
setResizable( false );
setVisible( true );
}
public static void main(String[] args)
{
new SimplePlasma();
}
}
//****************************************************************************
//
//
//
//****************************************************************************
class Screen extends JPanel implements Runnable
{
private static final long serialVersionUID = 1L;
private Thread animator;
private static final double FIXED_TIME_STEP = 1/60.0;
private double accumulator = 0;
private int fps = 0;
private int framesPerSecond = 0;
private double previousTime = 0;
private double oldTime = 0;
Plasma plasma = new Plasma();
public Screen()
{
addKeyListener(new TAdapter());
setFocusable(true);
setBackground(Color.BLACK);
setDoubleBuffered(true);
}
public void addNotify()
{
super.addNotify();
animator = new Thread( this );
animator.start();
}
public void paint( Graphics g )
{
super.paint(g);
Graphics2D g2D = (Graphics2D)g;
render( g2D );
Toolkit.getDefaultToolkit().sync();
g.dispose();
}
private class TAdapter extends KeyAdapter
{
public void keyPressed( KeyEvent e )
{
int key = e.getKeyCode();
if( key == KeyEvent.VK_ESCAPE )
{
System.exit(0);
}
}
}
public void update() // update ng gameloop here
{
plasma.update();
repaint();
}
public void render( Graphics2D g2D ) // drawing dito
{
g2D.drawImage( plasma.getImage(),
0,
0,
null );
g2D.setColor( Color.BLUE );
g2D.drawString( "FPS :" + fps, 0, 10 );
}
public void run() // runnable interface needs run() implemented
{
double dt = getDeltaTime( getSystemTime() );
// gameloop
while( true )
{
dt = getDeltaTime( getSystemTime() );
if( dt > FIXED_TIME_STEP ) dt = FIXED_TIME_STEP;
accumulator += dt;
while( accumulator >= FIXED_TIME_STEP )
{
update();
accumulator -= FIXED_TIME_STEP;
}
try
{
Thread.sleep(15);
}
catch (InterruptedException e)
{
System.out.println("interrupted");
}
}
}
public double getSystemTime()
{
return System.nanoTime() / 1000000000.0;
}
public double getDeltaTime( double timerInSeconds )
{
double currentTime = timerInSeconds;
double elapsedTime = currentTime - oldTime;
oldTime = currentTime;
framesPerSecond++;
if( (currentTime - previousTime) > 1.0 )
{
previousTime = currentTime;
fps = framesPerSecond;
framesPerSecond = 0;
}
return elapsedTime;
}
}
//****************************************************************************
//
// Screen and logic constants
//
//****************************************************************************
class Constants
{
public static final double FIXED_TIME_STEP = 1.0 / 60.0;
public final static int SCREEN_WIDTH = 640;
public final static int SCREEN_HEIGHT = 480;
}
//****************************************************************************
//
//
//
//****************************************************************************
class Plasma
{
private static final double K1 = 35;
private static final double K2 = 18;
private static final double K3 = 50;
private int kSin1 = (int)(K1 *(360/57.3));
private int kSin2 = (int)(K2 *(360/57.3));
private int kSin3 = (int)(K3 *(360/57.3));
private int[] sinLut1 = new int[2048 * 4];
private int[] sinLut2 = new int[2048 * 4];
private int[] sinLut3 = new int[2048 * 4];
private int a, b, c;
BufferedImage image = new BufferedImage( Constants.SCREEN_WIDTH,
Constants.SCREEN_HEIGHT,
BufferedImage.TYPE_INT_ARGB);
int[] pixels = ((DataBufferInt) image.getRaster().getDataBuffer()).getData();
BufferedImage texture = new BufferedImage( 256,
256,
BufferedImage.TYPE_INT_ARGB);
int[] texPixels = ((DataBufferInt) texture.getRaster().getDataBuffer()).getData();
public Plasma()
{
int size = sinLut1.length;
for( int i = 0; i < size; i++ )
{
sinLut1[i] = (int)(Math.sin(i/K1) * 127 + Math.cos(i/K1) * 67);
sinLut2[i] = (int)(Math.sin(i/K2) * 164 + Math.cos(i/K2) * 26);
sinLut3[i] = (int)(Math.sin(i/K3) * 32 + Math.cos(i/K3) * 280);
}
double j = 255 / 360.0f * 3;
double k = 255 / 360.0f * 2;
double l = 255 / 360.0f * 1;
double pa = 0;
double pb = 0;
double pc = 0;
for( int y = 0; y < 256; y++ )
{
int r = (int)( 255 * Math.abs(Math.sin(pa * Math.PI / 180.0)) );
int g = (int)( 255 * Math.abs(Math.sin(pb * Math.PI / 180.0)) );
int b = (int)( 255 * Math.abs(Math.sin(pc * Math.PI / 180.0)) );
c = (255 << 24) | (r << 16) | (g << 8) | (b);
for( int x = 0; x < 256; x++ )
{
texPixels[y * 256 + x] = c;
}
pa += j;
pb += k;
pc += l;
}
}
public void update()
{
a = (++a) % (kSin1 + 1);
b = (++b) % (kSin2 + 1);
c = (++c) % (kSin3 + 1);
int offset = 0;
for( int y = 0; y < Constants.SCREEN_HEIGHT; y++ )
{
for( int x = 0; x < Constants.SCREEN_WIDTH; x++ )
{
int tx = ( sinLut1[x + a + 2048] +
sinLut2[y + b + 2048] +
sinLut3[x + y + c + 2048] );
int ty = ( sinLut3[x + y + c + 2048] +
sinLut1[(tx) & 2047] +
sinLut1[x + a + 2048] +
sinLut1[y + a + 2048] +
sinLut2[x + b + 2048]);
tx = tx & 255;
ty = ty & 255;
int c = texPixels[ ty * 256 + tx ];
pixels[offset++] = c;
}
}
}
i
public BufferedImage getImage()
{
return image;
}
}
Jar and code:
www.rel.phatcode.net/junk.php?id=146
Enjoy!<