Java animation
Download
1 / 15

Java: Animation - PowerPoint PPT Presentation


  • 273 Views
  • Updated On :

Java: Animation Chris North cs3724: HCI Animation? Changing graphics over time Examples: cartoons Clippy, agents/assistants Hour glass Save dohicky Progress bar Copy file Amit’s pulsating plot Purvi Algorithm Animation Java Graphics Review Extend JPanel

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Java: Animation' - ostinmannual


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
Java animation l.jpg

Java:Animation

Chris North

cs3724: HCI


Animation l.jpg
Animation?

  • Changing graphics over time

  • Examples:

    • cartoons

    • Clippy, agents/assistants

    • Hour glass

    • Save dohicky

    • Progress bar

    • Copy file

    • Amit’s pulsating plot


Purvi l.jpg
Purvi

  • Algorithm Animation


Java graphics review l.jpg
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);

    }

    }



Animating the ticker text l.jpg
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);

    }

    }


Animation loop l.jpg
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?


Multi threading l.jpg
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


Java threads l.jpg
Java Threads

  • implement Runnable interface

  • Override run( ) method

    • Put animation loop here

  • new Thread(runnable)

  • Thread.start( )

    • Calls runnable.run( )in new thread

  • My

    Panel

    Runnable

    run( )

    Thread

    start( )


    Simplifying l.jpg
    Simplifying

    • MyPanel implements Runnable

    My

    Panel

    Runnable

    run( )

    My

    Panel

    Runnable

    run( )

    Thread

    start( )

    Thread

    start( )


    Threaded ticker l.jpg
    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;

    g2.drawString("Hello World", x, 10); // my graphics

    }

    }

    or in button code

    that starts animation


    Stopping a thread l.jpg
    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

      }


    Controlling animation speed l.jpg
    Controlling Animation Speed

    While(true){

    x = x – 5; //animation step size

    repaint();

    Thread.sleep(100); //delay between steps

    }

    Milliseconds

    1000 = 1 second


    Animation summary l.jpg
    Animation Summary

    • In a secondary thread:

    • Make changes to global state

    • Repaint

    • Sleep delay

    • Repeat

    • Use double buffering as needed



    ad