⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tail_1.java

📁 processing製作之互動鏈形圖像,圖像隨滑鼠移動
💻 JAVA
字号:
import processing.core.*; 
import processing.xml.*; 

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 Tail_1 extends PApplet {

/** * Tail 1: demonstration of how to make things follow each other * using OO programming. You can add almost anything to this  * queue if you write it right. *  * @author Myer Nore * @version 20081204  **///SECTION 1: INTERFACES ////////////////////////////interface Locatable{  public float getX();  public float getY();}interface Controllable extends Locatable{  public void setX( float x );  public void setY( float y );  /** provides the appearance of the object centered at 0,0 )**/  public void display();}// SECTION 2: CLASSES ////////////////////////////////////////abstract class Thing implements Locatable{  float x, y;  public float getX() {     return x;         }  public float getY() {     return y;         }  /** this displays the thing at its coordinates.    * notice how the appearance is totally separate     * from the display.  this enables us to make any appearance    * that we want, even an appearance that consists of many     * in different places in relationship to its actual location.    ***/  public void display()  {    pushMatrix();    pushStyle();    translate( x, y ); // here is where the position is set    appearance();    popStyle();    popMatrix();  }  /** this must draw the Thing at 0,0 **/  public abstract void appearance();}/** A Queue is a Thing that holds Things.  **/class Queue extends Thing implements Controllable{  float segmentLength;  ArrayList followers;  /** you must provide a Thing to start the queue. **/  Queue( Thing leader )  {    segmentLength = 15;    followers = new ArrayList();    // as wacky as it sounds, this object    // is going to be the first object in the collection    followers.add( leader );  }  public void addFollower( Controllable follower )  {    followers.add( follower );  }  public void appearance()  {    Controllable leader;    Controllable follower;    for( int i = 0; i < followers.size()-1; i++ )    {      leader = (Controllable) followers.get( i );      follower = (Controllable) followers.get( i+1 );      control( leader, follower );    }    for( int i = followers.size()-1; i > 0; i-- )    {      leader = (Controllable) followers.get( i );      leader.display();    }  }  /**   * This is the juicy part where the position of each follower   * is determined by the one directly in front.   **/  public void control( Controllable leader, Controllable follower )  {    // find the difference in x between two consecutive balls    float changeInX = leader.getX() - follower.getX();    // find the difference in y between two consecutive balls    float changeInY = leader.getY() - follower.getY();    // find the angle by using the inverse tangent    float angle = atan2( changeInY, changeInX );    // the new follower x coordinate is defined as a certain     // distance from the x coordinate of the leader    // cos( angle ) produces values from zero to one,     // so segmentLength scales them to a certain distance.    float newX = leader.getX() - cos(angle) * segmentLength;    float newY = leader.getY() - sin(angle) * segmentLength;    follower.setX( newX );    follower.setY( newY );  }  /** you can also control the leader by controlling the Queue.**/  public void setX( float x ){     Controllable leader = (Controllable) followers.get( 0 );    leader.setX( x );      }  /** you can also control the leader by controlling the Queue.**/  public void setY( float y ){        Controllable leader = (Controllable) followers.get( 0 );    leader.setY( y );        }}class Ball extends Thing implements Controllable{  float xSpeed, ySpeed;  Ball()  {    x = random(width);    y = random(height);    xSpeed = random(3)-1.5f;    ySpeed = random(3)-1.5f;  }  public void appearance()  {    fill( 0xff2827B7, 127 );    stroke( 0xff16156A, 127 );    ellipse( 0, 0, 20, 20 );  }  public void setX( float x ){     this.x = x;               }  public void setY( float y ){     this.y = y ;              }}// SECTION 3: PROCESSING CODE //////////////////////////////Ball leader;Queue queue;int numberOfFollowers;public void setup(){  size( 700, 400 );  smooth();    leader = new Ball();  queue = new Queue( leader );  numberOfFollowers = 50;    for( int i = 0; i < numberOfFollowers; i++ )  {    queue.addFollower( new Ball() );  }}public void draw(){  background( 255 );  queue.setX( mouseX ); // this sets the position of the leader  queue.setY( mouseY ); // you could also say leader.setX( mouseX )  queue.display();}  static public void main(String args[]) {
    PApplet.main(new String[] { "--bgcolor=#e0dfe3", "Tail_1" });
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -