how to create a dynamic icon which always shows what is going on in the frame currently?
in
Programming Questions
•
7 months ago
I read the following article "
Fun with the frame object" from
http://wiki.processing.org/w/Fun_with_the_frame_object
My question is how to create a dynamic icon by retrieving the image of the current frame while the animation is going on?
i.e. what is shown in the icon is always a scaled version of what is showing in the current frame.
Thanks in advance!
For example, in the code below, can I get rid of the "draw icon" section (highlighted) which actually just repeats what has been done to the main frame but in the PGraphics icon object instead.
Code modified slightly from Generative Design code # M_4_1_01:
My question is how to create a dynamic icon by retrieving the image of the current frame while the animation is going on?
i.e. what is shown in the icon is always a scaled version of what is showing in the current frame.
Thanks in advance!
For example, in the code below, can I get rid of the "draw icon" section (highlighted) which actually just repeats what has been done to the main frame but in the PGraphics icon object instead.
Code modified slightly from Generative Design code # M_4_1_01:
- // M_4_1_01.pde
- // Node.pde
- //
- // Generative Gestaltung, ISBN: 978-3-87439-759-9
- // First Edition, Hermann Schmidt, Mainz, 2009
- // Hartmut Bohnacker, Benedikt Gross, Julia Laub, Claudius Lazzeroni
- // Copyright 2009 Hartmut Bohnacker, Benedikt Gross, Julia Laub, Claudius Lazzeroni
- //
- // http://www.generative-gestaltung.de
- //
- // Licensed under the Apache License, Version 2.0 (the "License");
- // you may not use this file except in compliance with the License.
- // You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
- // Unless required by applicable law or agreed to in writing, software
- // distributed under the License is distributed on an "AS IS" BASIS,
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- // See the License for the specific language governing permissions and
- // limitations under the License.
- /**
- * some moving nodes
- *
- * KEYS
- * r : re-initialize velocity vectors of the nodes
- * s : save png
- */
- int nodeCount = 50;
- PGraphics icon;
- PImage imgFrame;
- // myNodes array
- Node[] myNodes = new Node[nodeCount];
- // image output
- boolean saveOneFrame = false;
- void setup() {
- size(600,600);
- frame.setResizable(true);
- icon = createGraphics(30,30);
- colorMode(RGB, 255, 255, 255, 100);
- smooth();
- background(255);
- noStroke();
- fill(0);
- // setup myNodes
- for (int i = 0; i < nodeCount; i++) {
- myNodes[i] = new Node(random(width), random(height));
- myNodes[i].velocity.x = random(-3, 3);
- myNodes[i].velocity.y = random(-3, 3);
- myNodes[i].damping = 0.01;
- myNodes[i].c = color(random(255),random(255),random(255));
- }
- }
- void draw() {
- // background(255); // no tail
- fill(0, 5); // the smaller the alpha value, the longer the tail
- rect(0, 0, width, height);
- for (int i = 0; i < myNodes.length; i++) {
- myNodes[i].update();
- // draw node
- fill(myNodes[i].c, 100);
- ellipse(myNodes[i].x, myNodes[i].y, 5, 5);
- }
- // draw icon _____________________________________________________
- icon.beginDraw();
- icon.fill(0, 10); // the smaller the alpha value, the longer the tail
- icon.rect(0, 0, width, height);
- for (int i = 0; i < myNodes.length; i++) {
- myNodes[i].update();
- // draw node
- icon.fill(myNodes[i].c, 100);
- icon.ellipse(myNodes[i].x/20, myNodes[i].y/20, 3, 3);
- }
- icon.endDraw();
- frame.setIconImage(icon.image);
- // image output _______________________________________________________
- if(saveOneFrame == true) {
- saveFrame("_M_4_1_01_"+timestamp()+".png");
- saveOneFrame = false;
- }
- }
- void keyPressed(){
- if(key=='r' || key=='R') {
- for (int i = 0; i < nodeCount; i++) {
- myNodes[i].velocity.x = random(-5, 5);
- myNodes[i].velocity.y = random(-5, 5);
- }
- }
- if(key=='s' || key=='S') {
- saveOneFrame = true;
- }
- }
- String timestamp() {
- return String.format("%1$ty%1$tm%1$td_%1$tH%1$tM%1$tS", Calendar.getInstance());
- }
- class Node extends PVector {
- // velocity
- PVector velocity = new PVector();
- // minimum and maximum posiions
- float minX=5, minY=5, maxX=width-5, maxY=height-5;
- // damping of the velocity (0 = no damping, 1 = full damping)
- float damping = 0.1;
- // color
- color c;
- Node(float theX, float theY) {
- x = theX;
- y = theY;
- }
- // ------ calculate new position of the node ------
- void update() {
- maxX = width-5; // update maxX using the width of the current window in case it is changed
- maxY = height-5; // update maxY using the height of the current window in case it is changed
- x += velocity.x;
- y += velocity.y;
- if (x < minX) {
- x = minX - (x - minX);
- velocity.x = -velocity.x;
- }
- if (x > maxX) {
- x = maxX - (x - maxX);
- velocity.x = -velocity.x;
- }
- if (y < minY) {
- y = minY - (y - minY);
- velocity.y = -velocity.y;
- }
- if (y > maxY) {
- y = maxY - (y - maxY);
- velocity.y = -velocity.y;
- }
- velocity.x *= (1-damping);
- velocity.y *= (1-damping);
- }
- }
1