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.


>> REGISTER NOW TO START POSTING


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

Results 1 to 8 of 8

Thread: Continued from Grass problem

  1. #1
    Member
    Join Date
    Feb 2010
    Posts
    81
    Thanks
    18
    Thanked 1 Time in 1 Post

    Default Continued from Grass problem

    Ok, so my "disappearing grass" problem was solved but now I've developed the program more and have run into another problem . For some reason when a certain if statement is completed, the program simply reverts back to the state it was in before the if statement for no apparent reason.

    This is the full code:

    import java.applet.Applet;
    import java.awt.Color;
    import java.awt.Font;
    import java.awt.Graphics;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
     
    public class Drawing4 extends Applet implements KeyListener{
    	int PlayerX = 0;
    	int PlayerY = 0;
    	public void init()
    	{
    		this.addKeyListener(this);
    		resize(501, 502);
    	}
    	public void paint(Graphics g)
    	{
    		int GrassX = 5;
    		int GrassY = 5;
    		int TransX = 200;
    		int TransY = 200;
    		int TransX2 = 50;
    		int TransY2 = 50;
    		int BoardsX = 5;
    		int BoardsY = 5;
    		int GrassHere = 0;
    		g.setColor(Color.green);
    		while(GrassX < 500)
    		{
    			g.drawLine(GrassX, 0, GrassX, 500);
    			GrassX = GrassX + 5;
    		}
    		while(GrassY < 500)
    		{
    			g.drawLine(0, GrassY, 500, GrassY);
    			GrassY = GrassY + 5;
    		}
    		g.setColor(Color.black);
    		g.drawLine(0,0,500,0);
    		g.drawLine(500,500,500,0);
    		g.drawLine(500,500,0,500);
    		g.drawLine(0,500,0,0);
    		g.fillRect(225, 0, 50, 200);
    		g.setColor(Color.white);
    		g.fillRect(240, 180, 20, 20);
    		g.setColor(Color.red);
        	g.setFont(new Font(g.getFont().getName(),Font.PLAIN,25));
        	g.drawString("B",242,20);
        	g.drawString("L",242,40);
        	g.drawString("D",242,60);
        	g.drawString("G",242,80);
        	g.drawString("1",242,100);
    		g.setColor(Color.red);
    		g.fillRect(PlayerX, PlayerY, 10, 10);
                   if(PlayerX == 240 && PlayerY == 180 || PlayerX == 240 && PlayerY == 190 || PlayerX == 250 && PlayerY == 180 || PlayerX == 250 && PlayerY == 190)
    		{
    			g.setColor(Color.white);
    				try{
    					while(TransX > -1)
    					{
    						Thread.sleep(75);
    						g.fillRect(TransX, TransY, TransX2, TransY2);
    						TransX = TransX - 10;
    						TransY = TransY - 10;
    						TransX2 = TransX2 + 25;
    						TransY2 = TransY2 + 25;
    					}
    				}catch(Exception e){};
    			g.setColor(Color.white);
    			g.fillRect(0, 0, 500, 500);
    			g.setColor(Color.black);
    			g.drawLine(0,0,500,0);
    			g.drawLine(500,500,500,0);
    			g.drawLine(500,500,0,500);
    			g.drawLine(0,500,0,0);
    			g.setColor(new Color( 192, 128, 64));
    			while(BoardsX < 500)
    			{
    				g.drawLine(BoardsX, 0, BoardsX, 500);
    				BoardsX = BoardsX + 5;
    			}
    			while(BoardsY < 500)
    			{
    				g.drawLine(0, BoardsY, 500, BoardsY);
    				BoardsY = BoardsY + 5;
    			}
    			g.setColor(Color.red);
    			g.fillRect(PlayerX, PlayerY, 10, 10);
    		}
    	}
    	public void keyPressed(KeyEvent arg0){}
    	public void keyReleased(KeyEvent e) 
    	{
    		char key = e.getKeyChar();
    		if(key == 'w')
    		{
    			PlayerY = PlayerY - 10;
    		}
    		if(key == 'a')
    		{
    			PlayerX = PlayerX - 10;
    		}
    		if(key == 's')
    		{
    			PlayerY = PlayerY + 10;
    		}
    		if(key == 'd')
    		{
    			PlayerX = PlayerX + 10;
    		}
    		repaint();
    	}	
    	public void keyTyped(KeyEvent arg0){}
    }

    This is the portion with the if statement:

    public void paint(Graphics g)
    	{
    		int GrassX = 5;
    		int GrassY = 5;
    		int TransX = 200;
    		int TransY = 200;
    		int TransX2 = 50;
    		int TransY2 = 50;
    		int BoardsX = 5;
    		int BoardsY = 5;
    		int GrassHere = 0;
    		g.setColor(Color.green);
    		while(GrassX < 500)
    		{
    			g.drawLine(GrassX, 0, GrassX, 500);
    			GrassX = GrassX + 5;
    		}
    		while(GrassY < 500)
    		{
    			g.drawLine(0, GrassY, 500, GrassY);
    			GrassY = GrassY + 5;
    		}
    		g.setColor(Color.black);
    		g.drawLine(0,0,500,0);
    		g.drawLine(500,500,500,0);
    		g.drawLine(500,500,0,500);
    		g.drawLine(0,500,0,0);
    		g.fillRect(225, 0, 50, 200);
    		g.setColor(Color.white);
    		g.fillRect(240, 180, 20, 20);
    		g.setColor(Color.red);
        	g.setFont(new Font(g.getFont().getName(),Font.PLAIN,25));
        	g.drawString("B",242,20);
        	g.drawString("L",242,40);
        	g.drawString("D",242,60);
        	g.drawString("G",242,80);
        	g.drawString("1",242,100);
    		g.setColor(Color.red);
    		g.fillRect(PlayerX, PlayerY, 10, 10);
    		if(PlayerX == 240 && PlayerY == 180 || PlayerX == 240 && PlayerY == 190 || PlayerX == 250 && PlayerY == 180 || PlayerX == 250 && PlayerY == 190)
    		{
    			g.setColor(Color.white);
    				try{
    					while(TransX > -1)
    					{
    						Thread.sleep(75);
    						g.fillRect(TransX, TransY, TransX2, TransY2);
    						TransX = TransX - 10;
    						TransY = TransY - 10;
    						TransX2 = TransX2 + 25;
    						TransY2 = TransY2 + 25;
    					}
    				}catch(Exception e){};
    			g.setColor(Color.white);
    			g.fillRect(0, 0, 500, 500);
    			g.setColor(Color.black);
    			g.drawLine(0,0,500,0);
    			g.drawLine(500,500,500,0);
    			g.drawLine(500,500,0,500);
    			g.drawLine(0,500,0,0);
    			g.setColor(new Color( 192, 128, 64));
    			while(BoardsX < 500)
    			{
    				g.drawLine(BoardsX, 0, BoardsX, 500);
    				BoardsX = BoardsX + 5;
    			}
    			while(BoardsY < 500)
    			{
    				g.drawLine(0, BoardsY, 500, BoardsY);
    				BoardsY = BoardsY + 5;
    			}
    			g.setColor(Color.red);
    			g.fillRect(PlayerX, PlayerY, 10, 10);
                           [B]//And then right here if you move it simply reverts back to what it was when it started[/B]
    		}
    	}

    My project for my class is due in 3 days!


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,104
    Thanks
    52
    Thanked 2,285 Times in 2,256 Posts

    Default Re: Continued from Grass problem

    right here if you move it simply reverts back
    Can you explain what "reverts back" means?
    Do you mean there are a group of variables whose values are lost when the code exits a scope?
    Sounds like you are using local variables with the same names as variables in a larger scope.
    Try renaming the local variables to be different from the more global ones.
    Or if you don't want local variables, don't define them locally. use the more global ones.

  3. #3
    Member
    Join Date
    Feb 2010
    Posts
    81
    Thanks
    18
    Thanked 1 Time in 1 Post

    Default Re: Continued from Grass problem

    By "reverts back" I mean that the green lines which are drawn at the beginning are drawn again, after the if statement completes. Which is strange because the if statement is clearing the screen and then drawing something new and yet as soon as it completes the whole scene which I draw before the if statement is drawn again. Almost all the variables I'm using (at least all the ones I'm sure would affect the if statement) are declared locally.

    If you run my program yourself you'll see exactly what I'm talking about.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,104
    Thanks
    52
    Thanked 2,285 Times in 2,256 Posts

    Default Re: Continued from Grass problem

    Did you read the rest of my post about scope?
    What variables lose there values ("revert back")? What values should they keep NOT to revert back?

    Have you tried debugging your program by adding println() statements at all the places the variables in question are changed?

  5. #5
    Member
    Join Date
    Feb 2010
    Posts
    81
    Thanks
    18
    Thanked 1 Time in 1 Post

    Default Re: Continued from Grass problem

    I may be a beginner but I can say with 99% certainty that the problem has nothing to do with variables resetting themselves or variables at all. My problem is that, referring to the example:

    //Part 1
    //If Statement
    //Part 2

    Part 1 completes, and when the if statement is triggered, the if statement completes. Part 2 then completes. Then, for some reason, Part 1 starts and completes again.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,104
    Thanks
    52
    Thanked 2,285 Times in 2,256 Posts

    Default Re: Continued from Grass problem

    Add some println() statements at all the parts you mention to show when they are executed.

    For example:
    /*
    By "reverts back" I mean that the green lines which are drawn at the beginning are drawn again, 
    after the if statement completes. Which is strange because the if statement is clearing the screen
    and then drawing something new and yet as soon as it completes the whole scene which I draw before
    the if statement is drawn again. Almost all the variables I'm using (at least all the ones I'm sure 
    would affect the if statement) are declared locally. 
     
      <applet code=Drawing4Applet width=600 height=600>
      </applet>
    */
     
    import java.applet.Applet;
    import java.awt.Color;
    import java.awt.Font;
    import java.awt.Graphics;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
     
    public class Drawing4Applet extends Applet implements KeyListener{
    	int PlayerX = 250;  // start here vs the corner
    	int PlayerY = 200;
     
    	public void init()	{
    		this.addKeyListener(this);
    		resize(501, 502);
    	}
     
    	public void paint(Graphics g) 	{
          System.out.println(">>>Entered paint at " + System.currentTimeMillis());
    		int GrassX = 5;
    		int GrassY = 5;
    		int TransX = 200;
    		int TransY = 200;
    		int TransX2 = 50;
    		int TransY2 = 50;
    		int BoardsX = 5;
    		int BoardsY = 5;
    		int GrassHere = 0;
    		g.setColor(Color.green);
     
    		while(GrassX < 500)
    		{
    			g.drawLine(GrassX, 0, GrassX, 500);
    			GrassX = GrassX + 5;
    		}
    		while(GrassY < 500)
    		{
    			g.drawLine(0, GrassY, 500, GrassY);
    			GrassY = GrassY + 5;
    		}
    		g.setColor(Color.black);
    		g.drawLine(0,0,500,0);
    		g.drawLine(500,500,500,0);
    		g.drawLine(500,500,0,500);
    		g.drawLine(0,500,0,0);
    		g.fillRect(225, 0, 50, 200);
    		g.setColor(Color.white);
    		g.fillRect(240, 180, 20, 20);
    		g.setColor(Color.red);
        	g.setFont(new Font(g.getFont().getName(),Font.PLAIN,25));
        	g.drawString("B",242,20);
        	g.drawString("L",242,40);
        	g.drawString("D",242,60);
        	g.drawString("G",242,80);
        	g.drawString("1",242,100);
    		g.setColor(Color.red);
    		g.fillRect(PlayerX, PlayerY, 10, 10);
     
          if(PlayerX == 240 && PlayerY == 180 || PlayerX == 240 && PlayerY == 190 
                 || PlayerX == 250 && PlayerY == 180 || PlayerX == 250 && PlayerY == 190) 		{
             System.out.println("in if x=" + PlayerX + ", y=" + PlayerY  + " at " + System.currentTimeMillis());
    			g.setColor(Color.white);
    			try{
    				while(TransX > -1) 				{
    					Thread.sleep(75);
    					g.fillRect(TransX, TransY, TransX2, TransY2);
    					TransX = TransX - 10;
    					TransY = TransY - 10;
    					TransX2 = TransX2 + 25;
    					TransY2 = TransY2 + 25;
    				}
    			}catch(Exception e){};
    			g.setColor(Color.white);
    			g.fillRect(0, 0, 500, 500);
    			g.setColor(Color.black);
    			g.drawLine(0,0,500,0);
    			g.drawLine(500,500,500,0);
    			g.drawLine(500,500,0,500);
    			g.drawLine(0,500,0,0);
    			g.setColor(new Color( 192, 128, 64));
    			while(BoardsX < 500) 			{
    				g.drawLine(BoardsX, 0, BoardsX, 500);
    				BoardsX = BoardsX + 5;
    			}
    			while(BoardsY < 500)			{
    				g.drawLine(0, BoardsY, 500, BoardsY);
    				BoardsY = BoardsY + 5;
    			}
    			g.setColor(Color.red);
    			g.fillRect(PlayerX, PlayerY, 10, 10);
          System.out.println(" >>Exiting if at " + System.currentTimeMillis());
    		} // end if()
          System.out.println("after end of if at " + System.currentTimeMillis());
    	}
     
    	public void keyPressed(KeyEvent arg0){}
    	public void keyReleased(KeyEvent e)	{
          System.out.println("keyReleased at " + System.currentTimeMillis());
    		char key = e.getKeyChar();
    		if(key == 'w')
    		{
    			PlayerY = PlayerY - 10;
    		}
    		if(key == 'a')
    		{
    			PlayerX = PlayerX - 10;
    		}
    		if(key == 's')
    		{
    			PlayerY = PlayerY + 10;
    		}
    		if(key == 'd')
    		{
    			PlayerX = PlayerX + 10;
    		}
    		repaint();
    	}	
    	public void keyTyped(KeyEvent arg0){}
    }
    Last edited by Norm; July 19th, 2010 at 05:18 PM.

  7. The Following User Says Thank You to Norm For This Useful Post:

    The_Mexican (July 19th, 2010)

  8. #7
    Member
    Join Date
    Feb 2010
    Posts
    81
    Thanks
    18
    Thanked 1 Time in 1 Post

    Default Re: Continued from Grass problem

    Wow, ok! Thanks to your println statements I now know the problem fully I think, however I don't know how to fix it.

    The problem is that whenever I call the method repaint (which I do every time I press either w,a,s or d), it starts the entire paint over again from the beginning. I did not realize it did this. This makes perfect sense since whenever I move it restarts from the beginning of paint!

    Now I just don't have any idea how to fix it . Do you have any idea how?

  9. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,104
    Thanks
    52
    Thanked 2,285 Times in 2,256 Posts

    Default Re: Continued from Grass problem

    Sounds like a place for a flag like a boolean. Use it's value to control when you want to do something.

Similar Threads

  1. [SOLVED] Grass disappearing :(
    By The_Mexican in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 18th, 2010, 09:09 PM
  2. Application Continued (problem)
    By Riston in forum AWT / Java Swing
    Replies: 6
    Last Post: November 26th, 2009, 02:33 PM