@@ -0,0 +1,39 @@
|
||||
/**
|
||||
* Animated Sprite (Shifty + Teddy)
|
||||
* by James Patterson.
|
||||
*
|
||||
* Press the mouse button to change animations.
|
||||
* Demonstrates loading, displaying, and animating GIF images.
|
||||
* It would be easy to write a program to display
|
||||
* animated GIFs, but would not allow as much control over
|
||||
* the display sequence and rate of display.
|
||||
*/
|
||||
|
||||
Animation animation1, animation2;
|
||||
float xpos, ypos;
|
||||
float drag = 30.0;
|
||||
|
||||
void setup() {
|
||||
size(200, 200);
|
||||
background(255, 204, 0);
|
||||
frameRate(24);
|
||||
animation1 = new Animation("PT_Shifty_", 38);
|
||||
animation2 = new Animation("PT_Teddy_", 60);
|
||||
}
|
||||
|
||||
void draw() {
|
||||
float difx = mouseX - xpos;
|
||||
if (abs(difx) > 1.0) {
|
||||
xpos = xpos + difx/drag;
|
||||
xpos = constrain(xpos, 0, width);
|
||||
}
|
||||
|
||||
// Display the sprite at the position xpos, ypos
|
||||
if (mousePressed) {
|
||||
background(153, 153, 0);
|
||||
animation1.display(xpos-animation1.getWidth()/2, ypos);
|
||||
} else {
|
||||
background(255, 204, 0);
|
||||
animation2.display(xpos-animation1.getWidth()/2, ypos);
|
||||
}
|
||||
}
|
||||
27
java/examples/Topics/Animation/AnimatedSprite/Animation.pde
Normal file
@@ -0,0 +1,27 @@
|
||||
// Class for animating a sequence of GIFs
|
||||
|
||||
class Animation {
|
||||
PImage[] images;
|
||||
int imageCount;
|
||||
int frame;
|
||||
|
||||
Animation(String imagePrefix, int count) {
|
||||
imageCount = count;
|
||||
images = new PImage[imageCount];
|
||||
|
||||
for (int i = 0; i < imageCount; i++) {
|
||||
// Use nf() to number format 'i' into four digits
|
||||
String filename = imagePrefix + nf(i, 4) + ".gif";
|
||||
images[i] = loadImage(filename);
|
||||
}
|
||||
}
|
||||
|
||||
void display(float xpos, float ypos) {
|
||||
frame = (frame+1) % imageCount;
|
||||
image(images[frame], xpos, ypos);
|
||||
}
|
||||
|
||||
int getWidth() {
|
||||
return images[0].width;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
import processing.core.*;
|
||||
|
||||
import java.applet.*;
|
||||
import java.awt.*;
|
||||
import java.awt.image.*;
|
||||
import java.awt.event.*;
|
||||
import java.io.*;
|
||||
import java.net.*;
|
||||
import java.text.*;
|
||||
import java.util.*;
|
||||
import java.util.zip.*;
|
||||
import java.util.regex.*;
|
||||
|
||||
public class AnimatedSprite extends PApplet {
|
||||
|
||||
/**
|
||||
* Animated Sprite (Shifty + Teddy)
|
||||
* by James Patterson.
|
||||
*
|
||||
* Press the mouse button to change animations.
|
||||
* Demonstrates loading, displaying, and animating GIF images.
|
||||
* It would be easy to write a program to display
|
||||
* animated GIFs, but would not allow as much control over
|
||||
* the display sequence and rate of display.
|
||||
*/
|
||||
|
||||
Animation animation1, animation2;
|
||||
float xpos, ypos;
|
||||
float drag = 30.0f;
|
||||
|
||||
public void setup() {
|
||||
size(200, 200);
|
||||
background(255, 204, 0);
|
||||
frameRate(24);
|
||||
animation1 = new Animation("PT_Shifty_", 38);
|
||||
animation2 = new Animation("PT_Teddy_", 60);
|
||||
}
|
||||
|
||||
public void draw() {
|
||||
float difx = mouseX - xpos;
|
||||
if (abs(difx) > 1.0f) {
|
||||
xpos = xpos + difx/drag;
|
||||
xpos = constrain(xpos, 0, width);
|
||||
}
|
||||
|
||||
// Display the sprite at the position xpos, ypos
|
||||
if (mousePressed) {
|
||||
background(153, 153, 0);
|
||||
animation1.display(xpos-animation1.getWidth()/2, ypos);
|
||||
} else {
|
||||
background(255, 204, 0);
|
||||
animation2.display(xpos-animation1.getWidth()/2, ypos);
|
||||
}
|
||||
}
|
||||
// Class for animating a sequence of GIFs
|
||||
|
||||
class Animation {
|
||||
PImage[] images;
|
||||
int imageCount;
|
||||
int frame;
|
||||
|
||||
Animation(String imagePrefix, int count) {
|
||||
imageCount = count;
|
||||
images = new PImage[imageCount];
|
||||
|
||||
for (int i = 0; i < imageCount; i++) {
|
||||
// Use nf() to number format 'i' into four digits
|
||||
String filename = imagePrefix + nf(i, 4) + ".gif";
|
||||
images[i] = loadImage(filename);
|
||||
}
|
||||
}
|
||||
|
||||
public void display(float xpos, float ypos) {
|
||||
frame = (frame+1) % imageCount;
|
||||
image(images[frame], xpos, ypos);
|
||||
}
|
||||
|
||||
public int getWidth() {
|
||||
return images[0].width;
|
||||
}
|
||||
}
|
||||
|
||||
static public void main(String args[]) {
|
||||
PApplet.main(new String[] { "AnimatedSprite" });
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
/**
|
||||
* Animated Sprite (Shifty + Teddy)
|
||||
* by James Patterson.
|
||||
*
|
||||
* Press the mouse button to change animations.
|
||||
* Demonstrates loading, displaying, and animating GIF images.
|
||||
* It would be easy to write a program to display
|
||||
* animated GIFs, but would not allow as much control over
|
||||
* the display sequence and rate of display.
|
||||
*/
|
||||
|
||||
Animation animation1, animation2;
|
||||
float xpos, ypos;
|
||||
float drag = 30.0;
|
||||
|
||||
void setup() {
|
||||
size(200, 200);
|
||||
background(255, 204, 0);
|
||||
frameRate(24);
|
||||
animation1 = new Animation("PT_Shifty_", 38);
|
||||
animation2 = new Animation("PT_Teddy_", 60);
|
||||
}
|
||||
|
||||
void draw() {
|
||||
float difx = mouseX - xpos;
|
||||
if (abs(difx) > 1.0) {
|
||||
xpos = xpos + difx/drag;
|
||||
xpos = constrain(xpos, 0, width);
|
||||
}
|
||||
|
||||
// Display the sprite at the position xpos, ypos
|
||||
if (mousePressed) {
|
||||
background(153, 153, 0);
|
||||
animation1.display(xpos-animation1.getWidth()/2, ypos);
|
||||
} else {
|
||||
background(255, 204, 0);
|
||||
animation2.display(xpos-animation1.getWidth()/2, ypos);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
// Class for animating a sequence of GIFs
|
||||
|
||||
class Animation {
|
||||
PImage[] images;
|
||||
int imageCount;
|
||||
int frame;
|
||||
|
||||
Animation(String imagePrefix, int count) {
|
||||
imageCount = count;
|
||||
images = new PImage[imageCount];
|
||||
|
||||
for (int i = 0; i < imageCount; i++) {
|
||||
// Use nf() to number format 'i' into four digits
|
||||
String filename = imagePrefix + nf(i, 4) + ".gif";
|
||||
images[i] = loadImage(filename);
|
||||
}
|
||||
}
|
||||
|
||||
void display(float xpos, float ypos) {
|
||||
frame = (frame+1) % imageCount;
|
||||
image(images[frame], xpos, ypos);
|
||||
}
|
||||
|
||||
int getWidth() {
|
||||
return images[0].width;
|
||||
}
|
||||
}
|
||||
BIN
java/examples/Topics/Animation/AnimatedSprite/applet/loading.gif
Normal file
|
After Width: | Height: | Size: 2.2 KiB |
|
After Width: | Height: | Size: 314 B |
|
After Width: | Height: | Size: 314 B |
|
After Width: | Height: | Size: 318 B |
|
After Width: | Height: | Size: 328 B |
|
After Width: | Height: | Size: 343 B |
|
After Width: | Height: | Size: 379 B |
|
After Width: | Height: | Size: 420 B |
|
After Width: | Height: | Size: 501 B |
|
After Width: | Height: | Size: 615 B |
|
After Width: | Height: | Size: 733 B |
|
After Width: | Height: | Size: 806 B |
|
After Width: | Height: | Size: 842 B |
|
After Width: | Height: | Size: 877 B |
|
After Width: | Height: | Size: 877 B |
|
After Width: | Height: | Size: 951 B |
|
After Width: | Height: | Size: 934 B |
|
After Width: | Height: | Size: 917 B |
|
After Width: | Height: | Size: 907 B |
|
After Width: | Height: | Size: 907 B |
|
After Width: | Height: | Size: 892 B |
|
After Width: | Height: | Size: 854 B |
|
After Width: | Height: | Size: 841 B |
|
After Width: | Height: | Size: 886 B |
|
After Width: | Height: | Size: 805 B |
|
After Width: | Height: | Size: 733 B |
|
After Width: | Height: | Size: 667 B |
|
After Width: | Height: | Size: 596 B |
|
After Width: | Height: | Size: 539 B |
|
After Width: | Height: | Size: 503 B |
|
After Width: | Height: | Size: 451 B |
|
After Width: | Height: | Size: 392 B |
|
After Width: | Height: | Size: 351 B |
|
After Width: | Height: | Size: 333 B |
|
After Width: | Height: | Size: 323 B |
|
After Width: | Height: | Size: 316 B |
|
After Width: | Height: | Size: 316 B |
|
After Width: | Height: | Size: 315 B |
|
After Width: | Height: | Size: 312 B |
|
After Width: | Height: | Size: 305 B |
|
After Width: | Height: | Size: 309 B |
|
After Width: | Height: | Size: 310 B |
|
After Width: | Height: | Size: 310 B |
|
After Width: | Height: | Size: 315 B |
|
After Width: | Height: | Size: 329 B |
|
After Width: | Height: | Size: 339 B |
|
After Width: | Height: | Size: 354 B |
|
After Width: | Height: | Size: 390 B |
|
After Width: | Height: | Size: 415 B |
|
After Width: | Height: | Size: 451 B |
|
After Width: | Height: | Size: 482 B |
|
After Width: | Height: | Size: 546 B |
|
After Width: | Height: | Size: 566 B |
|
After Width: | Height: | Size: 607 B |
|
After Width: | Height: | Size: 692 B |
|
After Width: | Height: | Size: 778 B |
|
After Width: | Height: | Size: 853 B |
|
After Width: | Height: | Size: 914 B |
|
After Width: | Height: | Size: 936 B |
|
After Width: | Height: | Size: 968 B |
|
After Width: | Height: | Size: 971 B |
|
After Width: | Height: | Size: 987 B |
|
After Width: | Height: | Size: 1013 B |
|
After Width: | Height: | Size: 1014 B |
|
After Width: | Height: | Size: 1002 B |
|
After Width: | Height: | Size: 976 B |
|
After Width: | Height: | Size: 984 B |
|
After Width: | Height: | Size: 936 B |
|
After Width: | Height: | Size: 891 B |
|
After Width: | Height: | Size: 826 B |
|
After Width: | Height: | Size: 717 B |
|
After Width: | Height: | Size: 625 B |
|
After Width: | Height: | Size: 543 B |
|
After Width: | Height: | Size: 486 B |
|
After Width: | Height: | Size: 468 B |
|
After Width: | Height: | Size: 453 B |
|
After Width: | Height: | Size: 440 B |
|
After Width: | Height: | Size: 428 B |
|
After Width: | Height: | Size: 410 B |
|
After Width: | Height: | Size: 387 B |
|
After Width: | Height: | Size: 351 B |
|
After Width: | Height: | Size: 329 B |
|
After Width: | Height: | Size: 322 B |
|
After Width: | Height: | Size: 318 B |
|
After Width: | Height: | Size: 315 B |
|
After Width: | Height: | Size: 310 B |
|
After Width: | Height: | Size: 311 B |
|
After Width: | Height: | Size: 311 B |
|
After Width: | Height: | Size: 312 B |
|
After Width: | Height: | Size: 312 B |
|
After Width: | Height: | Size: 311 B |
|
After Width: | Height: | Size: 308 B |
|
After Width: | Height: | Size: 306 B |
|
After Width: | Height: | Size: 303 B |
|
After Width: | Height: | Size: 303 B |