Results 1 to 13 of 13
- 06-29-2010, 04:17 AM #1sahildave1991 Guest
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){} } } }
-
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.
- 06-29-2010, 05:39 AM #3
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
This will make it repaint forever - which is probably what you want.Java Code:while (true) { repaint(); Thread.sleep(200); }
Berkeleybross
- 06-29-2010, 06:07 AM #4sahildave1991 Guest
@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/
- 06-29-2010, 06:12 AM #5
"it is still moving invisbly" How do you know??? Not very invisble... :P
What do you define as "starting point"?
BerkelEybRoss :P
- 06-29-2010, 06:17 AM #6sahildave1991 Guest
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....
- 06-29-2010, 06:24 AM #7
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
- 06-29-2010, 06:33 AM #8sahildave1991 Guest
i want a squirrel to wait at the bottom of the screen till i click....
- 06-29-2010, 06:38 AM #9
Try that then, it *should* work.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.
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
- 06-29-2010, 08:12 AM #10sahildave1991 Guest
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...
- 06-29-2010, 08:45 AM #11sahildave1991 Guest
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 ???
- 06-29-2010, 01:34 PM #12
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?
- 06-29-2010, 07:24 PM #13
You shouldnt call thread.sleep inside the paint method.
The run method should look like this:
This will make it constantly repaint every 200milliseconds, regardless of a rabbit or not. This should mean the background will appear.Java Code:public void run() { while (true) { try { repaint(); Thread.sleep(200); } catch(Exception e){} } }
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.
BerkeleybrossLast edited by berkeleybross; 06-29-2010 at 07:29 PM. Reason: typo :|
Similar Threads
-
Help me with this program.....plsssssssss
By sahildave1991 in forum Threads and SynchronizationReplies: 1Last Post: 06-29-2010, 05:20 AM -
changing my program to array working program
By Chewart in forum New To JavaReplies: 39Last Post: 11-18-2009, 06:53 PM -
Execute A program from a Program!
By Moncleared in forum Advanced JavaReplies: 2Last Post: 02-22-2009, 04:17 PM -
How to execute an External Program through Java program
By Java Tip in forum java.ioReplies: 0Last Post: 04-04-2008, 02:40 PM -
How to execute an External Program through Java program
By JavaBean in forum Java TipReplies: 0Last Post: 10-04-2007, 09:33 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks