Skip to content

Commit 81f3f05

Browse files
Added new example (#69)
* Added class Palette. Updated example. * New library files. New example. * Update documentation.
1 parent 76b0e9d commit 81f3f05

File tree

11 files changed

+189
-122
lines changed

11 files changed

+189
-122
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,5 @@ It is based on and inspired by a number of Processing library templates, includi
3939
I wish to thank the developers of these repositories, who generously provided
4040
guidance and time. This template has been developed in collaboration with
4141
[@enkatsu](https://github.com/enkatsu).
42+
43+
The example library was developed by Stig Møller Hansen ([@stixan](https://github.com/stixan)).

build.gradle.kts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,8 @@ dependencies {
9494
compileOnly(group = "org.processing", name = "core", version = "4.3.1")
9595

9696
// insert your external dependencies
97-
implementation(group = "org.apache.commons", name = "commons-math3", version = "3.6.1")
98-
// The provided example uses commons-math3. Replace or add as needed.
97+
// For example uncomment the following line to declare commons-math3 as a dependency.
98+
// implementation(group = "org.apache.commons", name = "commons-math3", version = "3.6.1")
9999

100100
// To add a dependency on a Processing library that is installed locally,
101101
// uncomment the line below, and replace <library folder> with the location of that library

docs/develop.md

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ Intellij and Visual Studio Code will then direct you to move your code into nest
1616
with your package name. For instance, if the package is `com.myDomain.myLibrary`, the code will
1717
be in the folder structure `src/main/java/com/myDomain/myLibrary/`.
1818

19+
If you would like to use Processing functionality in your library, you can do so by importing Processing core
20+
and referencing Processing objects from the PApplet instance. An example of how to do this is given in the
21+
example library in this template.
1922

2023
## Resolving dependencies
2124
In the `build.gradle.kts` file, there are a few places you should edit, to input your own values.
@@ -27,18 +30,13 @@ sections of the `build.gradle.kts` file are the `repositories` and `dependencies
2730
dependencies can be resolved from Maven. You can add additional repositories here if
2831
your dependencies are hosted elsewhere.
2932

30-
**Your dependencies should be listed within `dependencies`.** For example, the example library
31-
uses the `commons-math3` package from `org.apache.commons` as a dependency, which is resolved
32-
from your listed repositories. It is listed within `dependencies` with the following structure:
33+
**Your dependencies should be listed within `dependencies`.** For example, if the example library
34+
were to use the `commons-math3` package from `org.apache.commons` as a dependency, it would be listed within `dependencies` with the following structure:
3335

3436
```
3537
implementation(group = "org.apache.commons", name = "commons-math3", version = "3.6.1")
3638
```
3739

38-
This dependency, `commons-math3`, is only needed by the example library.
39-
You can delete this dependency for your own library.
40-
Add your own dependencies using the same structure.
41-
4240
NOTE: Dependencies added with `implementation` will be included in the release. Dependencies
4341
added with `compileOnly`, such as Processing core, are available for compilation, but won't
4442
be included in the release.

docs/example_sketch_output.jpg

-22.6 KB
Binary file not shown.

docs/example_sketch_output.png

297 KB
Loading

docs/index.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
The Processing Library Template is a template to help developers of Processing libraries to develop and release.
44
It can be found on Github at [https://github.com/processing/processing-library-template](https://github.com/processing/processing-library-template).
55

6-
![example sketch output](./example_sketch_output.jpg)
6+
![example sketch output](./example_sketch_output.png)
77

88
This documentation provides information on
99

@@ -36,3 +36,5 @@ It is based on and inspired by a number of Processing library templates, includi
3636
I wish to thank the developers of these repositories, who generously provided
3737
guidance and time. This template has been developed in collaboration with
3838
[@enkatsu](https://github.com/enkatsu).
39+
40+
The example library was developed by Stig Møller Hansen ([@stixan](https://github.com/stixan)).
Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,31 @@
11
import com.myDomain.myLibrary.*;
22

3-
RandomCircles circles;
4-
SayHello hello;
3+
Palette p;
4+
Grid g;
55

66
void setup() {
7-
size(400,400);
8-
smooth();
9-
10-
circles = new RandomCircles(this);
11-
hello = new SayHello(this);
12-
13-
PFont font = createFont("Arial", 40);
14-
textFont(font);
7+
size(1280, 720);
8+
p = new Palette(this);
9+
makeGrid();
1510
}
1611

1712
void draw() {
18-
background(0);
19-
noStroke();
20-
fill(255,0,0);
21-
circles.drawCircles();
22-
fill(255);
23-
text(hello.text(), 40, 200);
13+
background(16);
14+
g.show();
15+
displayText();
16+
}
17+
18+
void mouseClicked() {
19+
makeGrid();
2420
}
21+
22+
void makeGrid() {
23+
g = new Grid(this, p.getPalette());
24+
}
25+
26+
void displayText() {
27+
fill(240);
28+
textAlign(CENTER, CENTER);
29+
textSize(200);
30+
text("Hello Library", width / 2, height / 2);
31+
}
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
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+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.myDomain.myLibrary;
2+
3+
import processing.core.*;
4+
5+
/**
6+
* This is an example class within library myLibrary.
7+
* Make sure you rename this class as well as the name of the example
8+
* package 'com.myDomain.myLibrary' to your own library naming convention.
9+
*/
10+
11+
public class Palette {
12+
13+
// parent is a reference to the parent sketch, and to Processing commands
14+
PApplet parent;
15+
16+
/**
17+
* a Constructor, usually called in the setup() method in your sketch to
18+
* initialize and start the Library.
19+
*
20+
* @param theParent the parent PApplet
21+
*/
22+
public Palette(PApplet theParent) {
23+
this.parent = theParent;
24+
}
25+
26+
/**
27+
* getPalette returns a an array containing five colors defined as integers
28+
*/
29+
public int[] getPalette() {
30+
int numColors = 5;
31+
int[] palette = new int[numColors];
32+
this.parent.pushStyle();
33+
this.parent.colorMode(PConstants.HSB, 1.0F, 1.0F, 1.0F);
34+
int mix = this.parent.color(this.parent.random(1.0F), 1.0F, 1.0F);
35+
float h, s, b;
36+
for (int i = 0; i < palette.length; i++) {
37+
h = (this.parent.random(1.0F) + this.parent.hue(mix)) / 2;
38+
s = this.parent.random(0.8F, 1.0F);
39+
b = this.parent.random(1.0F) < 0.2F ? 0.2F : 1.0F;
40+
palette[i] = this.parent.color(h, s, b);
41+
}
42+
this.parent.popStyle();
43+
return palette;
44+
}
45+
46+
}

src/main/java/com/myDomain/myLibrary/RandomCircles.java

Lines changed: 0 additions & 60 deletions
This file was deleted.

src/main/java/com/myDomain/myLibrary/SayHello.java

Lines changed: 0 additions & 36 deletions
This file was deleted.

0 commit comments

Comments
 (0)