Animation in games - PowerPoint PPT Presentation

1 / 17
About This Presentation
Title:

Animation in games

Description:

The Model is the actual internal representation. The View (or a View) is a way of looking at or displaying the model ... g.drawString(50, 50, 'Hello, world! ... – PowerPoint PPT presentation

Number of Views:40
Avg rating:3.0/5.0
Slides: 18
Provided by: villa81
Category:

less

Transcript and Presenter's Notes

Title: Animation in games


1
Animation in games
2
MVC
  • MVC stands for Model-View-Controller
  • The Model is the actual internal representation
  • The View (or a View) is a way of looking at or
    displaying the model
  • The Controller provides for user input and
    modification
  • The Model should always be kept separate
  • The View and Controller can sometimes be combined

3
Applets and applications
  • An Applet is only part of a program
  • The browser, or appletviewer, is the other part
  • An Applet cannot access your files
  • It can't keep a high-scores page (except on the
    server)
  • An application can't appear on a web page
  • Painting and animation is the same for both
  • You can write an "appletcation" that works either
    as an Applet or as an application

4
The class Applet
  • class Applet extends Panel public void
    init ( ) public void start ( )
    public void stop ( ) public void destroy
    ( ) public void paint (Graphics g)
    . . .

5
Applet flow of control
6
Painting is easy
  • A Graphics is a painting area
  • If g is a Graphics, you can
  • g.setColor(Color.red)
  • g.fillRect(10, 20, 300, 200)
  • g.drawString(50, 50, "Hello, world!")
  • When you write an Applet, you get a Graphics as a
    parameter to paint() and update()
  • For an application, call component.getGraphics()

7
Animation is repeated painting
  • To animate something
  • Update the Model, so you know what the thing is
    currently doing
  • The View then paints it on the screen by calling
    repaint( ) -- not paint( )
  • Call Thread.sleep(ms) to wait until it's time to
    paint again

8
Animation with less flicker
  • To animate something
  • Update the Model, so you know what the thing is
    currently doing
  • The View then paints it on an offscreen Image
  • Then View copies the offscreen to the screen
  • Call Thread.sleep(ms) to wait until it's time to
    paint again

9
update( ), paint( ), and repaint( )
  • When you need to change the image on the screen,
    call repaint( )
  • repaint( ) tells Java to do it "as soon as you
    can"
  • Painting immediately isn't always possible
  • Java should eventually call update(Graphics g)
  • update clears ("erases") g, then calls paint(g)
  • Your paint does the actual painting
  • So write paint(Graphics g), but call repaint( )

10
Threads
  • A thread is a single flow of control
  • If you have animation and user input, they must
    be in separate threads
  • Your animation is an infinite loop update model,
    paint, sleep, update model, paint, sleep, ...
  • Nowhere in that infinite loop does it say "get
    user input"!
  • Solution put the animation in a new thread

11
Implementing a Thread
  • Every Thread must have a run( ) method
  • This tells it what to do
  • When run( ) finishes, the thread dies
  • Hence, run( ) should be an (almost) infinite loop
  • There are two ways to create a new thread
  • Extend Thread
  • Implement Runnable
  • Either way, you must supply a run( ) method

12
Extending Thread
  • class Animation extends Thread
  • Animation anim new Animation ( )
  • A newly created thread is ready to run, but not
    actually running yet
  • anim.start ( )
  • start( ) is a request to the scheduler to run the
    Thread--it may not happen right away
  • The thread should eventually start running

13
Implementing Runnable
  • class Animation implements Runnable
  • Animation anim new Animation ( )
  • We still need an actual Thread
  • Thread myThread new Thread(anim)
  • myThread.start( )
  • As always, start( ) is a request to the scheduler
    to run the Thread--it may not happen right away

14
Controlling the animation thread
  • With two or more threads, you never know what
    order things are going to happen in
  • This leads to some real headaches
  • In my examples I used two variables
  • alive -- to exit the animation when it become
    false
  • okToRun -- when this is false, the animation does
    nothing except sleep
  • These are initialized before the Thread is
    started
  • You should look at my example code!

15
run( )
public void run() while(alive)
if(okToRun) model.makeOneStep()
repaint() try
Thread.sleep(delay) catch(InterruptedExc
eption e)
16
Using MediaTracker for images
  • When you load an image from a file, Java only
    starts loading it
  • You need a MediaTracker to tell when it's done
  • The steps are
  • Create a MediaTracker
  • For each image,
  • Start loading the image
  • Tell your MediaTracker about the image
  • Tell the MediaTracker to wait for loading to
    finish

17
The End
Write a Comment
User Comments (0)
About PowerShow.com