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

    Angry 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,316
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. changing my program to array working program
    By Chewart in forum New To Java
    Replies: 39
    Last Post: 11-18-2009, 07:53 PM
  2. Execute A program from a Program!
    By Moncleared in forum Advanced Java
    Replies: 2
    Last Post: 02-22-2009, 05:17 PM
  3. Executing a program within a program
    By gibsonrocker800 in forum New To Java
    Replies: 5
    Last Post: 05-12-2008, 09:24 AM
  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
  •