Results 1 to 13 of 13
  1. #1
    sahildave1991 Guest

    Default Help me with this program.....plsssssssss

    i am writing this code. I want to do the following :
    1/ when i click for the first time the image 2.png(which is a squirrel) appears on the screen and move.
    2/ before clicking the background should be there.

    MY PROBLEM :: when i click the background + squirrel appears but the squirrel does not move. if i remove the if statement or turn it to true the program goes well. but i dont understand, that when i click the if statement is always true because i assign value 1 to click in mouseClicked method, but still the squirrel doesnt move.
    And is there a way to bring the background beforehand only.

    HOPE YOU UNDERSTOOD MY PROBLEM>>>>:confused:



    Java Code:
    package MyScreen;
    
    import java.applet.Applet;
    import java.awt.Graphics;
    import java.awt.Image;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import java.awt.event.MouseMotionListener;
    
    public class MoveByMouse extends Applet implements MouseListener,MouseMotionListener,Runnable
    {
    	
    	int posX=0,posY=10;
    	int a,b,click=0,i=0;
    	Image sq,bg;
    	Thread t;
    	public void init()
    	{
    	addMouseListener(this);
    	addMouseMotionListener(this);
    	sq = getImage(getDocumentBase(),"22.PNG");
    	bg = getImage(getDocumentBase(),"STAGE I - Morning Glory 2.JPG");
    	}
    	
    	public void mouseClicked(MouseEvent e)
    	{
    		
    		posX=e.getX();
    		posY=e.getY();
    		click=1;
    		repaint();
    	}
    
    	public void mouseEntered(MouseEvent e) {}
    	public void mouseExited(MouseEvent e) {}
    	public void mousePressed(MouseEvent e) {}
    	public void mouseReleased(MouseEvent e) {}
    	public void mouseDragged(MouseEvent e) {}
    	public void mouseMoved(MouseEvent e) {}
    
    
    	public void paint(Graphics g)
    	{
    		
    		setSize(1024, 668);
    		g.drawImage(bg,0,0,null);
    		g.drawImage(sq,950-(10*(i*i)), 620-(50*i),null);
    	}
    	
    	public void start()
    	{
    		if(click==1)
    		{
    			try		
    			{
    				t= new Thread(this);
    				t.start();
    			}
    			catch(Exception e){}
    		}
    	}
    	
    	public void run()
    	{
    		for(;i<30;)
    		{
    			try
    			{
    				repaint();
    				Thread.sleep(200);
    				i++;
    			}
    			catch(Exception e){}
    		}
    	}
    
    	
    	
    	
    
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Please do not post your question in more than one forum. It fragments the discussion making it difficult for folks to know what has been already been discussed already. Duplicate post has been locked.

  3. #3
    berkeleybross's Avatar
    berkeleybross is offline Senior Member
    Join Date
    Feb 2010
    Location
    England
    Posts
    187
    Rep Power
    5

    Default

    You don't want to put the if(click) in the start method.
    You should put it in the paint method - At the moment you are painting nothing *until* the user clicks.
    If you put the if(click) around the bit painting the squirrel only, then it should appear when the user clicks.

    Why are you using a (badly constucted) for loop in the run method?
    Surely it would be better to use
    Java Code:
    while (true) {
        repaint();
        Thread.sleep(200);
    }
    This will make it repaint forever - which is probably what you want.

    Berkeleybross

  4. #4
    sahildave1991 Guest

    Default

    @berkelyboss

    if i do what u said...the squirrel doesn't appear but it is still moving invisibly...when i click, it is at a diff place rather than the starting point/....
    i want that whenever i click the squirrl moves frm the starting point/

  5. #5
    berkeleybross's Avatar
    berkeleybross is offline Senior Member
    Join Date
    Feb 2010
    Location
    England
    Posts
    187
    Rep Power
    5

    Default

    "it is still moving invisbly" How do you know??? Not very invisble... :P

    What do you define as "starting point"?

    BerkelEybRoss :P

  6. #6
    sahildave1991 Guest

    Default

    i hav my starting point at right bottom corner of the window....

    when i launch the applet....the background appears....when i click then the squirrel appears....BUT!!!

    it appears at a diff position everytime....
    for eg...if i had not used the if(click) statement...the squirrel would hav been visible and would hav been moving towards left...
    say after 1 sec it is at 500,500
    and after 2 sec it is at 400,500


    NOW !
    if i use if (click) statement and click after 1 sec after executing the program, the squirrel automatically appears at 500,500 and not at the bottom right corner
    similarly if i click after 2 sec after executing the program it appears at 400,500

    hope u understood my 'invisible' thingy....

  7. #7
    berkeleybross's Avatar
    berkeleybross is offline Senior Member
    Join Date
    Feb 2010
    Location
    England
    Posts
    187
    Rep Power
    5

    Default

    Im not sure if im getting you...

    You want the squirrel to wait in the bottom corner until you click?
    When you click the squirrel should be shown and then start to move?

    if thats the case, it can all be done in the repaint method.

    In the repaint method:
    - paint background
    - if mouse has been clicked
    - paint squirrel
    - move squirrel (inc i?)

    And remove the i++; from the for loop.

    Hope this helps, if I've got the wrong end of the stick please try to clarify further :)

    Berkeleybross

  8. #8
    sahildave1991 Guest

    Default

    i want a squirrel to wait at the bottom of the screen till i click....

  9. #9
    berkeleybross's Avatar
    berkeleybross is offline Senior Member
    Join Date
    Feb 2010
    Location
    England
    Posts
    187
    Rep Power
    5

    Default

    if thats the case, it can all be done in the repaint method.

    In the repaint method:
    - paint background
    - if mouse has been clicked
    - paint squirrel
    - move squirrel (inc i?)

    And remove the i++; from the for loop.
    Try that then, it *should* work.

    At the moment your for loop is moving the squirrel regardless of whether you clicked or not - hence its moving "invisibly". To fix this you should only move when you have clicked - which is why i suggest you put the paint and move methods inside the if statement.

    Come back if it doesnt work or you need help implementing it.

    Berkeleybross

  10. #10
    sahildave1991 Guest

    Default

    i removed the i++ from for loop and did this in paint method
    Java Code:
    public void paint(Graphics g)
    	{
    		
    		setSize(1024, 668);
    		g.drawImage(bg,0,0,null);
    		if(click==1)
    		{
    			for(;i<15;)
    			{	
    				g.drawImage(bg,0,0,null);
    				g.drawImage(sq,950-(10*(i*i)), 620-(50*i),null);
    				i++;
    				try
    				{
    					Thread.sleep(200);
    				}
    				catch(Exception e){}
    			}
    		}
    	}

    but the background doesnt appear from the starting,,,,,i want that the background image bg and the squirrel at the bottom from the starting i.e. when i run the program.

    but when i run...first a blank white screen appear then when i click the bg with the squirrel appears and it moves...

  11. #11
    sahildave1991 Guest

    Default

    hey its diff....
    i'll tell u step by step ::
    1. first i run the program, applet appears, white screen.
    2.
    a. if i maximize the applet, the background appears with no squirrel
    b. if i click it, background+squirrel moving appears

    so is there a way so that my background appears when i start applet.
    or a way that applet starts maximized ???

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,883
    Rep Power
    25

    Default

    have you tried debugging your code by adding println() statements to show you where the code executes and when and how variables change?
    For example: what is the value of click when start() is called?

  13. #13
    berkeleybross's Avatar
    berkeleybross is offline Senior Member
    Join Date
    Feb 2010
    Location
    England
    Posts
    187
    Rep Power
    5

    Default

    You shouldnt call thread.sleep inside the paint method.

    The run method should look like this:
    Java Code:
    public void run() {
        while (true) {
            try {
                repaint();
                Thread.sleep(200);
            } catch(Exception e){}
        }
    }
    This will make it constantly repaint every 200milliseconds, regardless of a rabbit or not. This should mean the background will appear.

    You need to adjust the paint method to remove the sleep call.

    Another note, why are you using for(;i<15; )?
    A simple if statement would do the trick: if (i<15) { //do something}

    Im afraid i cant help you with the maximizing problem, not sure whats going on there :| I've never made an applet.

    Berkeleybross
    Last edited by berkeleybross; 06-29-2010 at 08:29 PM. Reason: typo :|

Similar Threads

  1. Help me with this program.....plsssssssss
    By sahildave1991 in forum Threads and Synchronization
    Replies: 1
    Last Post: 06-29-2010, 06:20 AM
  2. changing my program to array working program
    By Chewart in forum New To Java
    Replies: 39
    Last Post: 11-18-2009, 07:53 PM
  3. Execute A program from a Program!
    By Moncleared in forum Advanced Java
    Replies: 2
    Last Post: 02-22-2009, 05:17 PM
  4. Replies: 0
    Last Post: 04-04-2008, 03:40 PM
  5. Replies: 0
    Last Post: 10-04-2007, 10:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •