15
Java: Animation Chris North cs3724: HCI

Java: Animation Chris North cs3724: HCI. Animation? Changing graphics over time Examples: cartoons Clippy, agents/assistants Hour glass Save dohicky Progress

Embed Size (px)

Citation preview

Page 1: Java: Animation Chris North cs3724: HCI. Animation? Changing graphics over time Examples: cartoons Clippy, agents/assistants Hour glass Save dohicky Progress

Java:Animation

Chris North

cs3724: HCI

Page 2: Java: Animation Chris North cs3724: HCI. Animation? Changing graphics over time Examples: cartoons Clippy, agents/assistants Hour glass Save dohicky Progress

Animation?

• Changing graphics over time

• Examples:• cartoons

• Clippy, agents/assistants

• Hour glass

• Save dohicky

• Progress bar

• Copy file

• Amit’s pulsating plot

Page 3: Java: Animation Chris North cs3724: HCI. Animation? Changing graphics over time Examples: cartoons Clippy, agents/assistants Hour glass Save dohicky Progress

Purvi

• Algorithm Animation

Page 4: Java: Animation Chris North cs3724: HCI. Animation? Changing graphics over time Examples: cartoons Clippy, agents/assistants Hour glass Save dohicky Progress

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);

}

}

Page 5: Java: Animation Chris North cs3724: HCI. Animation? Changing graphics over time Examples: cartoons Clippy, agents/assistants Hour glass Save dohicky Progress

Ticker

Page 6: Java: Animation Chris North cs3724: HCI. Animation? Changing graphics over time Examples: cartoons Clippy, agents/assistants Hour glass Save dohicky Progress

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);

}

}

Page 7: Java: Animation Chris North cs3724: HCI. Animation? Changing graphics over time Examples: cartoons Clippy, agents/assistants Hour glass Save dohicky Progress

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?

Page 8: Java: Animation Chris North cs3724: HCI. Animation? Changing graphics over time Examples: cartoons Clippy, agents/assistants Hour glass Save dohicky Progress

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

Page 9: Java: Animation Chris North cs3724: HCI. Animation? Changing graphics over time Examples: cartoons Clippy, agents/assistants Hour glass Save dohicky Progress

Java Threads

• implement Runnable interface

• Override run( ) method• Put animation loop here

• new Thread(runnable)

• Thread.start( )• Calls runnable.run( )

in new thread

Runnablerun( )

Threadstart( )

MyPanel

Page 10: Java: Animation Chris North cs3724: HCI. Animation? Changing graphics over time Examples: cartoons Clippy, agents/assistants Hour glass Save dohicky Progress

Simplifying

• MyPanel implements Runnable

Runnablerun( )

Threadstart( )

MyPanel

Threadstart( )

MyPanel

Runnable

run( )

Page 11: Java: Animation Chris North cs3724: HCI. Animation? Changing graphics over time Examples: cartoons Clippy, agents/assistants Hour glass Save dohicky Progress

Threaded Tickerpublic class MyPanel extends JPanel implements Runnable {

int x = 100;Thread mythread;

public MyPanel(){ // constructormythread = new Thread(this);mythread.start();

}public void run(){

while(true){ // animation loopx = 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

Page 12: Java: Animation Chris North cs3724: HCI. Animation? Changing graphics over time Examples: cartoons Clippy, agents/assistants Hour glass Save dohicky Progress

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

}

Page 13: Java: Animation Chris North cs3724: HCI. Animation? Changing graphics over time Examples: cartoons Clippy, agents/assistants Hour glass Save dohicky Progress

Controlling Animation Speed

While(true){

x = x – 5; //animation step size

repaint();

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

}

Milliseconds1000 = 1 second

Page 14: Java: Animation Chris North cs3724: HCI. Animation? Changing graphics over time Examples: cartoons Clippy, agents/assistants Hour glass Save dohicky Progress

Animation Summary

• In a secondary thread:

1. Make changes to global state

2. Repaint

3. Sleep delay

4. Repeat

• Use double buffering as needed

Page 15: Java: Animation Chris North cs3724: HCI. Animation? Changing graphics over time Examples: cartoons Clippy, agents/assistants Hour glass Save dohicky Progress

JBuilder Example