Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: water simulator

  1. #1
    Junior Member
    Join Date
    Apr 2013
    Thanked 0 Times in 0 Posts

    Default water simulator

    I am in a process of developing a simulator for water management. I am using a tank that is filled automatically at rate of 1 every second. and I need to design some agents that are using the water randomly and want to show their usage in GUI.
    below is my code

    please advice on how to proceed or where to have the consumer method.
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.Random;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    class RunControl {
    	public boolean runOK;
    class Data implements Runnable
    	private static final int left = 10;
    	private static final int top = 300;
    	private static final int width = 120;
    	private static final int height = 150;
    	private static final int inset = 5;
    	private static final Color tankColor = new Color(5,5,5);
    	private static final Color fillColor = new Color( 25, 25,112);
    	private static final Color emptyColor = new Color(240,248,255);
    	private static final int xstr = 20;
    	private static final int ystr = 40;
    	private JPanel	displayPanel;
    	private int currentDepth;
    	private double increment;
    	private int sleepTime;
    	private int limit;
    	private RunControl check;
    	Random rn= new Random();
        int randomNumber = rn.nextInt(8);
    	public Data(RunControl rc)
    		check = rc;
    	public void restart() { 
    		currentDepth = 0;  
    		increment =  1; 
    		sleepTime = 250; 
    		limit = height - inset;      
    	public void setLinkToPanel(JPanel aPanel) { displayPanel = aPanel; }
    	public void paint(Graphics g)
    		int waterLevel = currentDepth;
    		Color c = g.getColor();
    		g.fillRect(left, top, width, height);
    		g.fillRect(left+inset, top, width-2*inset, height-inset);
    		g.fillRect(left+inset, top+height-inset-currentDepth, width-2*inset, currentDepth);		
    		g.setColor (new Color(0,0,0));        //water level
    		g.drawString("Water Level =",20 ,250);
    		g.setColor (new Color(139,69,19));   //house color
            g.fillRect (200,20,400,430);
            g.setColor(new Color(190,190,190));   //doors and chimney  
            g.fillRect (345,380,110,70);
            g.setColor (new Color(186,134,11));   //door knobs
            g.fillOval (382,412,10,10);
            g.fillOval (407,412,10,10);
            g.setColor (new Color(186,134,11));   //windows outer frame effec
            g.fillRect (221,61,78,78);
            g.fillRect (221,261,78,78);
            g.fillRect (221,361,78,78);
            g.fillRect (221,161,78,78);
            g.fillRect (501,261,78,78);
            g.fillRect (501,361,78,78);
            g.fillRect (501,61,78,78);
            g.fillRect (501,161,78,78);
            g.setColor (new Color(175,238,238));   //windows
            g.fillRect (225,65,70,70);
            g.fillRect (225,165,70,70);
            g.fillRect (225,265,70,70);
            g.fillRect (225,365,70,70);
            g.fillRect (505,65,70,70);
            g.fillRect (505,165,70,70);
            g.fillRect (505,265,70,70);
            g.fillRect (505,365,70,70);
            g.setColor (new Color(139,69,19));   //door sections
            g.fillRect (398,380,2,70);
            g.setColor (new Color(186,134,11));   //inner frame effect
            g.fillRect (257,265,5,70);
            g.fillRect (257,365,5,70);
            g.fillRect (257,165,5,70);
            g.fillRect (257,65,5,70);
            g.fillRect (538,265,5,70);
            g.fillRect (538,365,5,70);
            g.fillRect (538,165,5,70);
            g.fillRect (538,65,5,70);
            g.fillRect (225,298,70,5);
            g.fillRect (225,398,70,5);
            g.fillRect (225,198,70,5);
            g.fillRect (225,98,70,5);
            g.fillRect (505,298,70,5);
            g.fillRect (505,398,70,5);
            g.fillRect (505,198,70,5);
            g.fillRect (505,98,70,5);
            g.setColor (new Color(240,248,255));   //water pipe
            g.fillRect (100,400,100,10);
    	public void run()
    		for(;;) {
    /*Claim (and temporarily at least Lock) the coordination object.
    If it isn't ok to run, then wait for the object to be changed.  While
    this thread is waiting, release the lock on the coordination object.
    This thread will resume after a "notify" is performed by some other thread.
    This thread should then check whether it is ok to continue.
    			synchronized(check) {
    				while(!check.runOK) { 
    					try { check.wait(); } catch(InterruptedException ie) { }
    			if(increment>0) {
    				// Tank is filling
    				if(currentDepth < limit) {
    					currentDepth += increment;	
    						currentDepth = limit;
    				else {
    					// Tank is full
    					limit = 0;
    					increment = - increment;
    			else {
    				// Tank is Emptying
    				if(currentDepth>0) {
    					currentDepth += increment;  // -ve increment makes it go down!
    					if(currentDepth<0) currentDepth = 0;
    				else {
    					// Tank is empty
    					limit = height - inset;
    					increment = - increment;
    			try { Thread.sleep(sleepTime); } catch(InterruptedException ie) {}
    class MyPanel extends JPanel {
    	Data fData;
    	public MyPanel(Data aData) {  
    		setPreferredSize(new Dimension(400,400));
    		fData = aData;
    	public void paint(Graphics g) { 
    class GUI1 extends JFrame implements ActionListener {
    	private static final String start = "Start";
    	private boolean running;
    	private MyPanel fPanel;
    	private JButton fStartButton;
    	private Data fData;
    	private Thread fWorkThread;
    	private RunControl fCheck;
    	public void actionPerformed(ActionEvent e)
    			running = true;
    			fCheck.runOK = true;
    			synchronized(fCheck) { fCheck.notify(); }
    	public GUI1()
    		fCheck = new RunControl();
    		fData = new Data(fCheck);
    		this.setLayout(new BorderLayout());
    		fPanel = new MyPanel(fData);
    		this.getContentPane().add(fPanel, BorderLayout.CENTER);
    		fStartButton = new JButton(start);
    		this.getContentPane().add(fStartButton, BorderLayout.SOUTH);
    		running = false;
    		fWorkThread = new Thread(fData);
    public class main extends Applet{
    	public static void main(String[] args)
    		GUI1 aGUI = new GUI1();

  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Washington, DC
    My Mood
    Thanked 636 Times in 540 Posts

    Default Re: water simulator

    Which part of this are you confused about?

    What's the very next, smallest thing you know you have to do?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Apr 2013
    Thanked 0 Times in 0 Posts

    Default Re: water simulator

    generate random requests to consume water from the tank below is what i am working on right now but i am not sure whether it is right way to do it or not.
    I am generating random number of requests and then generate an array that has the requested amount of water for each request.
    then i want to decrease the amount of water gradually that reflects the agents consumption. I hope you understand my points. if you run the previous code in ur PC maybe you will understand my point.
        Random rn = new Random();
        int ArraySize = rn.nextInt(8);   
        int requests [] = new int [ArraySize];

  4. #4
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    My Mood
    Thanked 263 Times in 232 Posts

    Default Re: water simulator

    Quote Originally Posted by watersimulator View Post
    i am not sure whether it is right way to do it or not.
    Wondering if it is the right way or not usually means you have little or no plan. Did you decide how to solve the problem before writing code?
    What step are you on in the solution? It looks ok (with the little shown), what is the next step?

Similar Threads

  1. smpp simulator
    By ridg18 in forum Java Theory & Questions
    Replies: 3
    Last Post: September 21st, 2012, 06:57 PM
  2. Developing a simulator
    By jack_nutt in forum AWT / Java Swing
    Replies: 1
    Last Post: September 5th, 2012, 07:06 AM
  3. 2D Fluid water simulation
    By JamEngulfer221 in forum Java Theory & Questions
    Replies: 0
    Last Post: December 11th, 2011, 07:27 AM
  4. JAVA simulator
    By YAS218 in forum Java Theory & Questions
    Replies: 8
    Last Post: July 20th, 2009, 09:57 AM