Sign in to follow this  
kouami

Water rendering

Recommended Posts

Hi Guys, I am trying to code a water simulation after reading the following article on gamedev : http://www.gamedev.net/reference/articles/article718.asp I am coding it in JAVA. the problem I am having is rendering it the water. Can anyone help me out? That will be very much appreciated. You can find my code below. Thanks a lot. package poc; import java.applet.Applet; import java.awt.Graphics; import java.awt.Image; import java.awt.Toolkit; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.event.MouseMotionListener; import javax.swing.ImageIcon; public class Water extends Applet implements Runnable, MouseListener,MouseMotionListener { private Image offImage, backGroundImage; private String imageName = "D:\\Documents and Settings\\ekakoll\\Desktop\\impact.gif"; private Graphics offGraphics; private int appletWidth; private int appletHeight; private Thread animation; private int[][] oldWater; private int[][] newWater; public void init(){ backGroundImage = loadImage(imageName); appletWidth = backGroundImage.getWidth(null); appletHeight = backGroundImage.getHeight(null); oldWater = new int[appletWidth + 1][appletHeight + 1]; newWater = new int[appletWidth + 1][appletHeight + 1]; this.setSize(appletWidth,appletHeight); } public void start(){ if(animation == null){ animation = new Thread(this); animation.start(); } } public void stop(){ animation = null; } public void run(){ while(true){ gameUpdate(); gameRender(); paintScreen(); try{ Thread.sleep(100); } catch(InterruptedException e){} } } private Image loadImage(String fileName){ return new ImageIcon(fileName).getImage(); } private void processWater(){ for(int i = 1; i < appletWidth; i ++){ for(int j = 1; j < appletHeight; j ++ ){ newWater[i][j] = ((oldWater[i - 1][j] + oldWater[i + 1][j] + oldWater[i][j - 1] + oldWater[i][j + 1]) / 2) - newWater[i][j]; newWater[i][j] -= newWater[i][j] >> 4; } } } private void renderWater(){ for(int i = 1; i < appletWidth; i ++){ for(int j = 1; j < appletHeight; j ++ ){ } } } private void gameUpdate(){ processWater(); } private void gameRender(){ if(offImage == null){ offImage = createImage(appletWidth, appletHeight); if(offImage == null){ System.out.println("offImage is null ..."); return; } } else{ offGraphics = offImage.getGraphics(); offGraphics.clearRect(0,0,appletWidth, appletHeight); offGraphics.drawImage(backGroundImage,0,0,this); } } private void paintScreen(){ Graphics g; try{ g = this.getGraphics(); if((g != null) && (offImage != null)) g.drawImage(offImage,0,0,null); Toolkit.getDefaultToolkit().sync(); g.dispose(); } catch(Exception e){} } @Override public void mouseClicked(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseEntered(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseExited(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mousePressed(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseReleased(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseDragged(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseMoved(MouseEvent e) { // TODO Auto-generated method stub } }

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this