Java: Animation - PowerPoint PPT Presentation

1 / 15
About This Presentation
Title:

Java: Animation

Description:

Multiple threads let multiple methods execute simultaneously (multi-tasking) ... mythread = null; will cleanup thread object. Boolean runMyAnimation; ... – PowerPoint PPT presentation

Number of Views:907
Avg rating:3.0/5.0
Slides: 16
Provided by: chris78
Category:

less

Transcript and Presenter's Notes

Title: Java: Animation


1
JavaAnimation
  • Chris North
  • cs3724 HCI

2
Animation?
  • Changing graphics over time
  • Examples
  • cartoons
  • Clippy, agents/assistants
  • Hour glass
  • Save dohicky
  • Progress bar
  • Copy file
  • Amits pulsating plot

3
Purvi
  • Algorithm Animation

4
Java Graphics Review
  • Extend JPanel
  • Override paintComponent( ) method
  • public class MyPanel extends JPanel
  • public void paintComponent(Graphics g)
  • super.paintComponent(g)
  • Graphics2D g2 (Graphics2D)g
  • // my graphics here
  • g2.drawString("Hello World", 10, 10)

5
Ticker
6
Animating the ticker text
  • Keep repainting graphics using different x
  • public class MyPanel extends JPanel
  • int x
  • public void paintComponent(Graphics g)
  • super.paintComponent(g)
  • Graphics2D g2 (Graphics2D)g
  • // my graphics here
  • g2.drawString("Hello World", x, 10)

7
Animation Loop
  • Update x location (member variable)
  • Repaint
  • While(true) //infinite loop
  • x x 1
  • repaint() //calls paintComponent()
  • Thread.sleep(10) //slow down animation
  • Problem?

8
Multi-Threading
  • Loop prevents other parts of code from executing
  • User events pile up
  • Multiple threads let multiple methods execute
    simultaneously (multi-tasking)
  • Threads lightweight processes, shared memory

Main thread User interaction
Anim thread Update graphics
9
Java Threads
  • implement Runnable interface
  • Override run( ) method
  • Put animation loop here
  • new Thread(runnable)
  • Thread.start( )
  • Calls runnable.run( )in new thread

Runnable run( )
My Panel
Thread start( )
10
Simplifying
  • MyPanel implements Runnable

Runnable run( )
My Panel
My Panel Runnable run( )
Thread start( )
Thread start( )
11
Threaded Ticker
  • public class MyPanel extends JPanel implements
    Runnable
  • int x 100
  • Thread mythread
  • public MyPanel() // constructor
  • mythread new Thread(this)
  • mythread.start()
  • public void run()
  • while(true) // animation loop
  • x x 5
  • repaint()
  • Thread.sleep(100)
  • public void paintComponent(Graphics g)
  • super.paintComponent(g)
  • Graphics2D g2 (Graphics2D)g

or in button code that starts animation
12
Stopping a Thread
  • Stop animation loop with a flag
  • mythread null will cleanup thread object
  • Boolean runMyAnimation
  • While(runMyAnimation) //flag stops loop
  • x x 5
  • repaint() //calls paintComponent()
  • Thread.sleep(100) //slow down animation

13
Controlling Animation Speed
  • While(true)
  • x x 5 //animation step size
  • repaint()
  • Thread.sleep(100) //delay between steps

Milliseconds 1000 1 second
14
Animation Summary
  • In a secondary thread
  • Make changes to global state
  • Repaint
  • Sleep delay
  • Repeat
  • Use double buffering as needed

15
JBuilder Example
Write a Comment
User Comments (0)
About PowerShow.com