Created
February 14, 2014 00:29
-
-
Save samm81/8986976 to your computer and use it in GitHub Desktop.
Wave
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.awt.Canvas; | |
import java.awt.Color; | |
import java.awt.Dimension; | |
import java.awt.Graphics; | |
import java.awt.event.MouseAdapter; | |
import java.awt.event.MouseEvent; | |
import java.awt.image.BufferedImage; | |
import java.util.Random; | |
import javax.swing.JFrame; | |
@SuppressWarnings("serial") | |
public class Wave extends Canvas{ | |
static int x = 500; | |
static int y = 500; | |
int blocksize = 1; | |
static double damping = 1;//.99; | |
static int startStrength = 10; | |
static int[][] buffer1 = new int[x][y]; | |
static int[][] buffer2 = new int[x][y]; | |
BufferedImage img = null; | |
BufferedImage bg = null; | |
public static void main(String[] args) throws InterruptedException { | |
for(int i=0;i<x;i++){ | |
for(int j=0;j<y;j++){ | |
buffer1[i][j] = 0; | |
buffer2[i][j] = 0; | |
} | |
} | |
buffer1[x/2][y/2] = startStrength; | |
JFrame f = new JFrame(); | |
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
f.setLocation(100, 100); | |
Canvas c = new Wave(); | |
f.add(c); | |
f.pack(); | |
f.setVisible(true); | |
c.repaint(); | |
while(true){ | |
for(int i=1;i<x-1;i++){ | |
for(int j=1;j<y-1;j++){ | |
buffer2[i][j] -= (( | |
buffer1[i-1][j] + | |
buffer1[i+1][j] + | |
buffer1[i][j-1] + | |
buffer1[i][j+1] | |
) / 2); | |
buffer2[i][j] *= -1 * damping; | |
//buffer2[i][j] = Math.abs(buffer2[i][j]); | |
} | |
} | |
c.repaint(); | |
Thread.sleep(50); | |
int swap; | |
for(int i=0;i<x;i++){ | |
for(int j=0;j<y;j++){ | |
swap = buffer2[i][j]; | |
buffer2[i][j] = buffer1[i][j]; | |
buffer1[i][j] = swap; | |
} | |
} | |
} | |
} | |
public Wave() { | |
this.setSize(new Dimension(x*blocksize, y*blocksize)); | |
this.addMouseMotionListener(new MouseAdapter(){ | |
public void mouseMoved(MouseEvent e) { | |
int x = e.getX(); | |
int y = e.getY(); | |
buffer1[x][y] = startStrength; | |
} | |
}); | |
} | |
@Override | |
public void update(Graphics g) { | |
if(img == null) | |
img = new BufferedImage(x*blocksize, y*blocksize, BufferedImage.TYPE_INT_RGB); | |
if(bg == null){ | |
bg = new BufferedImage(x*blocksize, y*blocksize, BufferedImage.TYPE_INT_RGB); | |
generate(bg.getGraphics(), bg.getHeight(), bg.getWidth()); | |
} | |
img.getGraphics().clearRect(0, 0, img.getWidth(), img.getHeight()); | |
paintImg(img.getGraphics()); | |
paint(g); | |
} | |
private void generate(Graphics g, int height, int width) { | |
int blocksize = 50; | |
Random r = new Random(); | |
for(int i=0;i<x;i++){ | |
for(int j=0;j<y;j++){ | |
int red = r.nextInt(255); | |
int blue = r.nextInt(255); | |
int green = r.nextInt(255); | |
Color c = new Color(red, green, blue); | |
g.setColor(c); | |
g.fillRect(i*blocksize, j*blocksize, blocksize, blocksize); | |
} | |
} | |
} | |
@Override | |
public void paint(Graphics g) { | |
g.drawImage(img, 0, 0, null); | |
} | |
// for every pixel (x,y) in the buffer | |
// | |
// Xoffset = buffer(x-1, y) - buffer(x+1, y) | |
// Yoffset = buffer(x, y-1) - buffer(x, y+1) | |
// | |
// Shading = Xoffset | |
// | |
// t = texture(x+Xoffset, y+Yoffset) | |
// | |
// p = t + Shading | |
// | |
// plot pixel at (x,y) with colour p | |
// | |
// end loop | |
public void paintImg(Graphics g) { | |
for(int i=1;i<x-1;i++){ | |
for(int j=1;j<y-1;j++){ | |
int xoffset = buffer2[i-1][j] - buffer2[i+1][j]; | |
int yoffset = buffer2[i][j-1] - buffer2[i][j+1]; | |
int s = xoffset * 10; | |
int t = bg.getRGB(i + xoffset, j + yoffset); | |
//int t = bg.getRGB(i, j); | |
int r = t >> 16 & 0xFF; | |
int gr = t >> 8 & 0xFF; | |
int b = t & 0xFF;; | |
r += s; | |
if(r > 255) r = 255; | |
if(r < 0) r = 0; | |
gr += s; | |
if(gr > 255) gr = 255; | |
if(gr < 0) gr = 0; | |
b += s; | |
if(b > 255) b = 255; | |
if(b < 0) b = 0; | |
g.setColor(new Color(r, gr, b)); | |
g.fillRect(i*blocksize, j*blocksize, blocksize, blocksize); | |
/* | |
int height = buffer2[i][j]; | |
int c = (int) ((height * 255.0 / 200.0) + (255.0/2.0)); | |
Color color = new Color(c, c, 255); | |
g.setColor(color); | |
g.fillRect(i*blocksize, j*blocksize, blocksize, blocksize); | |
*/ | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment