Jump to content
  • Advertisement
Sign in to follow this  
kouami

Water rendering

This topic is 3824 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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[j] = ((oldWater[i - 1][j] + oldWater[i + 1][j] + oldWater[j - 1] + oldWater[j + 1]) / 2) - newWater[j]; newWater[j] -= newWater[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
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!