Animation Method in Paint Method?
Author |
Message |
ProgrammingNoob
|
Posted: Sun Apr 20, 2014 12:40 pm Post subject: Animation Method in Paint Method? |
|
|
Why doesn't this work? I created a method for an animation loop of two pictures called animation(),
and i want to put that in the paint method. How do i do this?
import java.applet.*;
import java.awt.*;
public class Slot_Machine extends Applet
{
Image back;
int coin=10;
// Place instance variables here
public void init ()
{
back= getImage(getDocumentBase(),"Images/Background/Slot Machine.png");
// Place the body of the initialization method here
} // init method
public void animation(Graphics g)
{
Image Title1,Title2;
Title1= getImage(getDocumentBase(),"Images/Title Animation/Title 1.png");
Title2= getImage(getDocumentBase(),"Images/Title Animation/Title 2.png");
while(true){
g.drawImage(Title2,200,0,this);
{ try { Thread.currentThread().sleep(2000); }
catch ( Exception e ) { } }
g.drawImage(Title1,200,0,this);
{ try { Thread.currentThread().sleep(2000); }
catch ( Exception e ) { } }
}//end while(true) loop
}//end animation
public void paint (Graphics g)
{
g.drawImage(back,0,0,this);
animation(); //FROM THE METHOD ABOVE, WHY DOESNT THIS WORK? HOW DO I FIX THIS?
String coins = String.valueOf(coin);
g.setColor(Color.white);
g.setFont(new Font("Impact",Font.PLAIN,30));
g.drawString(coins,405,350);
// Place the body of the drawing method here
} // paint method
} |
|
|
|
|
|
Sponsor Sponsor
|
|
|
Zren
|
Posted: Sun Apr 20, 2014 5:33 pm Post subject: Re: Animation Method in Paint Method? |
|
|
ProgrammingNoob @ Sun Apr 20, 2014 12:40 pm wrote: Why doesn't this work? I created a method for an animation loop of two pictures called animation(),
and i want to put that in the paint method. How do i do this?
Java: |
import java.applet.*;
import java.awt.*;
public class Slot_Machine extends Applet
{
Image back;
int coin= 10;
// Place instance variables here
public void init ()
{
back= getImage (getDocumentBase (), "Images/Background/Slot Machine.png");
// Place the body of the initialization method here
} // init method
public void animation (Graphics g )
{
Image Title1,Title2;
Title1= getImage (getDocumentBase (), "Images/Title Animation/Title 1.png");
Title2= getImage (getDocumentBase (), "Images/Title Animation/Title 2.png");
while(true){
g. drawImage(Title2, 200, 0, this);
{ try { Thread. currentThread(). sleep(2000); }
catch ( Exception e ) { } }
g. drawImage(Title1, 200, 0, this);
{ try { Thread. currentThread(). sleep(2000); }
catch ( Exception e ) { } }
}//end while(true) loop
}//end animation
public void paint (Graphics g )
{
g. drawImage(back, 0, 0, this);
animation (); //FROM THE METHOD ABOVE, WHY DOESNT THIS WORK? HOW DO I FIX THIS?
String coins = String. valueOf(coin );
g. setColor(Color. white);
g. setFont(new Font("Impact", Font. PLAIN, 30));
g. drawString(coins, 405, 350);
// Place the body of the drawing method here
} // paint method
} |
Please wrap your code in either of the following in order to preserve whitespace (indentation) and to highlight the syntax.
code: |
[syntax="java"] ... code ... [/syntax]
[code] ... code ... [/code ]
|
Basically you're trying to do an animation loop inside an animation loop. The paint() method is used to draw ONE frame of the animation, not the entire animation. By calling Thread.sleep(), you are also blocking the whole program for 2 second. This means you're preventing the program from reaching code for ANYTHING else. Eg: Input, which would mean you're preventing the user from closing the program prematurely.
The reason why your code isn't working is (probably) because the screen doesn't update with what you've drawn until after the paint() method completes.
Instead of blocking the program, try drawing the same picture over and over until it's been 2 seconds since the program started. How would you do that? Simple: t = t2 - t1.
Java: |
long startTime = System. getCurrentTimeMillis();
while (true) {
long currentTime = System. getCurrentTimeMillis();
long timeSinceStart = currentTime - startTime;
if (timeSinceStart >= 2000)
break; // It's been 2 seconds since the program started, so let's do something!
}
|
Now Java's GUI has it's own event loop. So we to work with it.
You can apply this into your program like so:
code: |
class SlotMachine:
void init():
this.startTime = System.getCurrentTimeMillis()
void paint(Graphics g):
currentTime = System.getCurrentTimeMillis()
t = currentTime - this.startTime
if (0 <= t <= 2000):
// Draw Title #1
else if (2000 < t <= 4000):
// Draw Title #2
else if (4000 < t):
// Exit program
|
|
|
|
|
|
|
|
|