Thomas Darimont
Erfahrenes Mitglied
Hallo,
hier findet man ein schönes tutorial zum Thema Plasma:
http://student.kuleuven.be/~m0216922/CG/plasma.html
Weitere coole Grafik tutorials:
http://student.kuleuven.be/~m0216922/CG/index.html
schaut mal hier:
Gruß Tom
hier findet man ein schönes tutorial zum Thema Plasma:
http://student.kuleuven.be/~m0216922/CG/plasma.html
Weitere coole Grafik tutorials:
http://student.kuleuven.be/~m0216922/CG/index.html
schaut mal hier:
Java:
/**
*
*/
package de.tutorials;
import static java.lang.Math.sin;
import static java.lang.Math.sqrt;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.GraphicsEnvironment;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferInt;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import javax.swing.JFrame;
/**
* @author Tom
*
*/
public class PlasmaExample extends JFrame {
BufferStrategy bufferStrategy;
boolean stop;
int w = 256;
int h = 256;
Runnable renderLoop = new Runnable() {
public void run() {
BufferedImage image = new BufferedImage(w, h,
BufferedImage.TYPE_INT_ARGB);
DataBufferInt dataBufferInt = (DataBufferInt) image.getRaster()
.getDataBuffer();
int paletteShift = 0;
int[] colorIndecesLookupTable = generateLookUpTable();
while (!stop) {
Graphics2D g = (Graphics2D) bufferStrategy.getDrawGraphics();
g.clearRect(0, 0, getWidth(), getHeight());
int[] colorPalette = generateColorPalette(256, Color.WHITE,
Color.BLACK);
for (int x = 0; x < w; x++) {
for (int y = 0; y < h; y++) {
int color = colorPalette[(colorIndecesLookupTable[y * w
+ x] + paletteShift)
% colorPalette.length];
dataBufferInt.setElem(y * w + x, color);
}
}
g.drawImage(image, 0, 0, getWidth(), getHeight(), null);
g.dispose();
bufferStrategy.show();
try {
TimeUnit.MILLISECONDS.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
paletteShift += 4;
}
setVisible(false);
System.exit(0);
}
private int[] generateLookUpTable() {
int[] colorIndeceslookupTable = new int[w * h];
for (int x = 0; x < w; x++) {
for (int y = 0; y < h; y++) {
int colorIndex = (int) (128.0
+ (128.0 * sin(x / 16.0))
+ 128.0
+ (128.0 * sin(y / 32.0))
+ 128.0
+ (128.0 * sin(sqrt((x - w / 2.0) * (x - w / 2.0)
+ (y - h / 2.0) * (y - h / 2.0)) / 8.0))
+ 128.0 + (128.0 * sin(sqrt(x * x + y * y) / 8.0)));
colorIndeceslookupTable[w * y + x] = colorIndex;
}
}
return colorIndeceslookupTable;
}
private int[] generateColorPalette(int colors, Color start, Color end) {
int[] palette = new int[2 * colors];
int startRgb = start.getRGB();
int colorShift = (end.getRGB() - startRgb) / colors;
for (int i = 0; i < colors; i++) {
int color = startRgb + i * colorShift;
palette[i] = color;
palette[2 * colors - i - 1] = color;
}
return palette;
};
};
public PlasmaExample() {
super("PlasmaExample");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(640, 480);
setVisible(true);
createBufferStrategy(2);
bufferStrategy = getBufferStrategy();
// Fullscreen ?
GraphicsEnvironment.getLocalGraphicsEnvironment()
.getDefaultScreenDevice().setFullScreenWindow(this);
this.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
stop = true;
}
}
});
Executors.newSingleThreadExecutor().execute(renderLoop);
}
/**
* @param args
*/
public static void main(String[] args) {
new PlasmaExample();
}
}
Gruß Tom