|
| 1 | +package com.myDomain.myLibrary; |
| 2 | + |
| 3 | +import processing.core.PApplet; |
| 4 | +import processing.core.PConstants; |
| 5 | + |
| 6 | +import java.util.ArrayList; |
| 7 | + |
| 8 | +/** |
| 9 | + * This is an example class within library myLibrary. |
| 10 | + * Make sure you rename this class as well as the name of the example |
| 11 | + * package 'com.myDomain.myLibrary' to your own library naming convention. |
| 12 | + */ |
| 13 | + |
| 14 | +public class Grid { |
| 15 | + |
| 16 | + // parent is a reference to the parent sketch, and to Processing commands |
| 17 | + PApplet parent; |
| 18 | + |
| 19 | + ArrayList<Dot> dots; |
| 20 | + |
| 21 | + /** |
| 22 | + * a Constructor, usually called in the setup() method in your sketch to |
| 23 | + * initialize and start the Library. |
| 24 | + * |
| 25 | + * @param theParent the parent PApplet |
| 26 | + * @param palette an array of color values from the Palette class |
| 27 | + */ |
| 28 | + public Grid(PApplet theParent, int[] palette) { |
| 29 | + |
| 30 | + // parent is a reference to the parent sketch, and to Processing commands |
| 31 | + parent = theParent; |
| 32 | + |
| 33 | + dots = new ArrayList<Dot>(); |
| 34 | + |
| 35 | + int gridSizeY = (int) parent.random(3, 16); |
| 36 | + int gridSizeX = (int) (gridSizeY * ((float) parent.width / parent.height)); |
| 37 | + float cellSizeY = parent.height / (float) gridSizeY; |
| 38 | + float cellSizeX = parent.width / (float) gridSizeX; |
| 39 | + float dotSpeed = parent.random(0.008F, 0.064F); |
| 40 | + |
| 41 | + for (int y = 0; y < gridSizeY; y++) { |
| 42 | + for (int x = 0; x < gridSizeX; x++) { |
| 43 | + float xpos = x * cellSizeX + cellSizeX / 2; |
| 44 | + float ypos = y * cellSizeY + cellSizeY / 2; |
| 45 | + int dotColor = palette[(int) parent.random(palette.length)]; |
| 46 | + dots.add(new Dot(xpos, ypos, PApplet.min(cellSizeX, cellSizeY), dotSpeed, dotColor)); |
| 47 | + } |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * show both updates and displays the grid by calling the |
| 53 | + * update and display methods of the inline Dot class (see below) |
| 54 | + */ |
| 55 | + public void show() { |
| 56 | + for (Dot d : dots) { |
| 57 | + d.update(); |
| 58 | + d.display(); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + // Dot is an inline class of the Grid class. |
| 63 | + private class Dot { |
| 64 | + |
| 65 | + float xpos, ypos; |
| 66 | + float diameterOriginal, diameterCurrent; |
| 67 | + float pulseValue, pulseSpeed; |
| 68 | + int colorFill; |
| 69 | + |
| 70 | + /** |
| 71 | + * a Constructor called from the constructor of the parent Grid class. |
| 72 | + * |
| 73 | + * @param x horisontal position of the dot |
| 74 | + * @param y vertical position of the dot |
| 75 | + * @param d diameter of the dot |
| 76 | + * @param s pulse speed of the dot |
| 77 | + * @param c color of the dot |
| 78 | + */ |
| 79 | + public Dot(float x, float y, float d, float s, int c) { |
| 80 | + xpos = x; |
| 81 | + ypos = y; |
| 82 | + diameterOriginal = d; |
| 83 | + diameterCurrent = diameterOriginal; |
| 84 | + pulseValue = parent.random(PConstants.TWO_PI); |
| 85 | + pulseSpeed = parent.random(s * 0.5F, s * 2.0F); |
| 86 | + colorFill = c; |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * display draws a dot |
| 91 | + */ |
| 92 | + public void display() { |
| 93 | + parent.pushStyle(); |
| 94 | + parent.noStroke(); |
| 95 | + parent.circle(xpos, ypos, diameterCurrent); |
| 96 | + parent.popStyle(); |
| 97 | + parent.fill(colorFill); |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * update updates the dots current diameter based on its pulse value and original diameter |
| 102 | + */ |
| 103 | + public void update() { |
| 104 | + pulseValue += pulseSpeed; |
| 105 | + diameterCurrent = PApplet.map(PApplet.sin(pulseValue), -1, 1, diameterOriginal * 0.1F, diameterOriginal * 0.9F); |
| 106 | + } |
| 107 | + } |
| 108 | +} |
0 commit comments