2015年10月5日 星期一

02160473賴緯漢_2015互動技術概論 week4

今天WEEK4的練習
播出gif檔

1.程式範例




























程式碼:
/**
 * Sequential
 * by James Paterson.  
 * 
 * Displaying a sequence of images creates the illusion of motion. 
 * Twelve images are loaded and each is displayed individually in a loop. 
 */

// @pjs preload must be used to preload media if the program is 
// running with Processing.js
/* @pjs preload="PT_anim0000.gif, PT_anim0001.gif, PT_anim0002.gif, PT_anim0003.gif,
PT_anim0004.gif, PT_anim0005.gif, PT_anim0006.gif, PT_anim0007.gif, PT_anim0008.gif,
PT_anim0009.gif, PT_anim0010.gif, PT_anim0011.gif"; */ 

int numFrames = 12;  // The number of frames in the animation
int currentFrame = 0;
PImage[] images = new PImage[numFrames];
    
void setup() {
  size(640, 360);
  frameRate(24);
  
  images[0]  = loadImage("PT_anim0000.gif");
  images[1]  = loadImage("PT_anim0001.gif"); 
  images[2]  = loadImage("PT_anim0002.gif");
  images[3]  = loadImage("PT_anim0003.gif"); 
  images[4]  = loadImage("PT_anim0004.gif");
  images[5]  = loadImage("PT_anim0005.gif"); 
  images[6]  = loadImage("PT_anim0006.gif");
  images[7]  = loadImage("PT_anim0007.gif"); 
  images[8]  = loadImage("PT_anim0008.gif");
  images[9]  = loadImage("PT_anim0009.gif"); 
  images[10] = loadImage("PT_anim0010.gif");
  images[11] = loadImage("PT_anim0011.gif"); //一張一張播放
  
  // If you don't want to load each image separately
  // and you know how many frames you have, you
  // can create the filenames as the program runs.
  // The nf() command does number formatting, which will
  // ensure that the number is (in this case) 4 digits.
  //for (int i = 0; i < numFrames; i++) {
  //  String imageName = "PT_anim" + nf(i, 4) + ".gif";
  //  images[i] = loadImage(imageName);
  //}


void draw() { 
  background(0);
  currentFrame = (currentFrame+1) % numFrames;  // Use % to cycle through frames
  int offset = 0;
  for (int x = -100; x < width; x += images[0].width) { 
    image(images[(currentFrame+offset) % numFrames], x, -20);
    offset+=2;
    image(images[(currentFrame+offset) % numFrames], x, height/2);
    offset+=2;
  }

}

2.不過第一種方法有點麻煩(一張一張播放loop)
   所以使用可以直接撥放gif檔的方法














程式碼:
import gifAnimation.*;
PImage [] imgKPgif;
Gif myKP;
//PImage img;
void setup()
{
  size(400,400);
  //img=loadImage("KP.gif");
  myKP = new Gif(this,"KP.gif");
  myKP.loop();//連續播放
}
void draw()
{
  image(myKP,0,0);

}

3.點滑鼠才播放gif檔














程式碼:
import gifAnimation.*;
PImage [] imgKPgif;
Gif myKP;
//PImage img;
void setup()
{
  size(400,400);
  //img=loadImage("KP.gif");
  myKP = new Gif(this,"KP.gif");
  //myKP.loop();//loop拿掉
}

void draw()
{
  image(myKP,0,0);
}

void mousePressed()
{
  myKP.play();//按滑鼠播放
  myKP.ignoreRepeat();//不要重複

}

4.按下鍵盤按鍵即可播放gif(ex:按鍵f,按鍵j)














程式碼:
import gifAnimation.*;
PImage [] imgKPgif;
Gif myKP,myKP2;
//PImage img;
void setup()
{
  size(800,400);
  //img=loadImage("KP.gif");
  myKP = new Gif(this,"KP.gif");
  myKP2 = new Gif(this,"KP.gif");
  //myKP.loop();
}

void draw()
{
  image(myKP,0,0);
  image(myKP2,400,0);
}

void mousePressed()
{
  if(mouseButton==LEFT) {myKP.play(); myKP.ignoreRepeat();}
  if(mouseButton==RIGHT) {myKP2.play(); myKP2.ignoreRepeat();}
}
void keyPressed()
{
  if(key=='f') {myKP.play(); myKP.ignoreRepeat();}//按下f鍵則播放
  if(key=='j') {myKP2.play(); myKP2.ignoreRepeat();}//按下j鍵則播放

}

5.播放音檔範例














程式碼:
/**
  * This sketch demonstrates how to play a file with Minim using an AudioPlayer. <br />
  * It's also a good example of how to draw the waveform of the audio.
  * <p>
  * For more information about Minim and additional features, 
  * visit http://code.compartmental.net/minim/
  */

import ddf.minim.*;

Minim minim;
AudioPlayer player;

void setup()
{
  size(512, 200, P3D);
  
  // we pass this to Minim so that it can load files from the data directory
  minim = new Minim(this);
  
  // loadFile will look in all the same places as loadImage does.
  // this means you can find files that are in the data folder and the 
  // sketch folder. you can also pass an absolute path, or a URL.
  player = minim.loadFile("marcus_kellis_theme.mp3");
  
  // play the file from start to finish.
  // if you want to play the file again, 
  // you need to call rewind() first.
  player.play();
}

void draw()
{
  background(0);
  stroke(255);
  
  // draw the waveforms
  // the values returned by left.get() and right.get() will be between -1 and 1,
  // so we need to scale them up to see the waveform
  // note that if the file is MONO, left.get() and right.get() will return the same value
  for(int i = 0; i < player.bufferSize() - 1; i++)
  {
    float x1 = map( i, 0, player.bufferSize(), 0, width );
    float x2 = map( i+1, 0, player.bufferSize(), 0, width );
    line( x1, 50 + player.left.get(i)*50, x2, 50 + player.left.get(i+1)*50 );
    line( x1, 150 + player.right.get(i)*50, x2, 150 + player.right.get(i+1)*50 );
  }

}

7.輸出音檔














程式碼:
import ddf.minim.*;
import gifAnimation.*;
PImage [] imgKPgif;
Gif myKP,myKP2;
Minim minim;
AudioPlayer player;
//PImage img;
void setup()
{
  size(800,400);
  frameRate(100);
  minim = new Minim(this);
  player = minim.loadFile("Michael Calfan - Mercy (Original Mix).mp3");
  player.play();
  //img=loadImage("KP.gif");
  myKP = new Gif(this,"KP.gif");
  myKP2 = new Gif(this,"KP.gif");
  //myKP.loop();
}

void draw()
{
  image(myKP,0,0);
  image(myKP2,400,0);
}

void mousePressed()
{
  if(mouseButton==LEFT) {myKP.jump(0); myKP.play();}
  if(mouseButton==RIGHT) {myKP2.jump(0); myKP2.play();}
}
/*void keyPressed()
{
  if(key=='f') {myKP.play(); myKP.ignoreRepeat();}
  if(key=='j') {myKP2.play(); myKP2.ignoreRepeat();}

}*/

沒有留言:

張貼留言