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 3 of 3

Thread: Grass disappearing :(

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

    Default Grass disappearing :(

    For some reason when the "Grass" is drawn, it disappears immediately! I have no idea why this shouldn't work. It works perfectly when it's just the specified part of the code alone.

    This is the full code which doesn't work, the green lines disappear immediately:

    import java.applet.Applet;
    import java.awt.Color;
    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;
    	int GrassX = 5;
    	public void init()
    	{
    		this.addKeyListener(this);
    		resize(501, 502);
    	}
    	public void paint(Graphics g)
    	{
    		g.setColor(Color.green);
    		while(GrassX < 500)
    		{
    			g.drawLine(GrassX, 0, GrassX, 500);
    			GrassX = GrassX + 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.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){}
    }


    But alone the code does work:

    import java.applet.Applet;
    import java.awt.Color;
    import java.awt.Graphics;
     
    public class testing extends Applet{
    	public void init()
    	{
    		resize(501,501);
    	}
    	public void paint(Graphics g)
    	{
    		int GrassX = 5;
    		g.setColor(Color.green);
    		while(GrassX < 500)
    		{
    			g.drawLine(GrassX, 0, GrassX, 500);
    			GrassX = GrassX + 5;
    		}
    	}
    }


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Grass disappearing :(

    You've declared the GrassX variable in the class instance variables list. This means on each successive call to paint, the value will be whatever it was before, so in this case the first call would have GrassX be 0 and end with 500, then the next call would have GrassX already as 500, and not draw any grass.

  3. The Following User Says Thank You to helloworld922 For This Useful Post:

    The_Mexican (July 18th, 2010)

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

    Default Re: Grass disappearing :(

    Aha! Thanks!