Skip to content

Commit 65bcfec

Browse files
committed
new hybrid coding example
1 parent 0524019 commit 65bcfec

File tree

3 files changed

+142
-0
lines changed

3 files changed

+142
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import py5
2+
3+
4+
def setup():
5+
py5.size(500, 500, py5.P2D)
6+
7+
8+
def draw():
9+
for _ in range(3):
10+
size = py5.random_int(50, 150)
11+
color = py5.color(py5.random_int(0, 255), py5.random_int(0, 255), py5.random_int(0, 255))
12+
# call Py5Utilities Java method to draw a happy face
13+
py5.utils.drawHappyFace(py5.random(py5.width), py5.random(py5.height), color, size)
14+
15+
16+
py5.run_sketch()
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>py5</groupId>
8+
<artifactId>py5-hybrid-programming-example3</artifactId>
9+
<version>0.1</version>
10+
11+
<name>py5-hybrid-programming-example3</name>
12+
<url>https://py5coding.org/</url>
13+
<properties>
14+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15+
<maven.compiler.source>17</maven.compiler.source>
16+
<maven.compiler.target>17</maven.compiler.target>
17+
<jarlocation>${env.CONDA_PREFIX}/lib/python3.10/site-packages/py5/jars</jarlocation>
18+
</properties>
19+
20+
<dependencies>
21+
<dependency>
22+
<groupId>py5</groupId>
23+
<artifactId>py5-processing4</artifactId>
24+
<version>0.9.0.dev0</version>
25+
<scope>system</scope>
26+
<systemPath>${jarlocation}/core.jar</systemPath>
27+
</dependency>
28+
<dependency>
29+
<groupId>py5</groupId>
30+
<artifactId>py5-jogl</artifactId>
31+
<version>0.9.0.dev0</version>
32+
<scope>system</scope>
33+
<systemPath>${jarlocation}/jogl-all.jar</systemPath>
34+
</dependency>
35+
<dependency>
36+
<groupId>py5</groupId>
37+
<artifactId>py5</artifactId>
38+
<version>0.9.0.dev0</version>
39+
<scope>system</scope>
40+
<systemPath>${jarlocation}/py5.jar</systemPath>
41+
</dependency>
42+
</dependencies>
43+
44+
<build>
45+
<plugins>
46+
<plugin>
47+
<groupId>org.apache.maven.plugins</groupId>
48+
<artifactId>maven-dependency-plugin</artifactId>
49+
<version>3.2.0</version>
50+
<executions>
51+
<execution>
52+
<id>copy</id>
53+
<phase>package</phase>
54+
<goals>
55+
<goal>copy</goal>
56+
</goals>
57+
</execution>
58+
</executions>
59+
<configuration>
60+
<artifactItems>
61+
<artifactItem>
62+
<groupId>py5</groupId>
63+
<artifactId>py5-hybrid-programming-example3</artifactId>
64+
<version>0.1</version>
65+
<type>jar</type>
66+
<overWrite>true</overWrite>
67+
<outputDirectory>${project.basedir}/../jars</outputDirectory>
68+
<destFileName>py5-hybrid-programming-example3.jar</destFileName>
69+
</artifactItem>
70+
</artifactItems>
71+
</configuration>
72+
</plugin>
73+
</plugins>
74+
</build>
75+
76+
</project>
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package py5utils;
2+
3+
import py5.core.Sketch;
4+
5+
public class Py5Utilities {
6+
7+
protected Sketch sketch;
8+
9+
public Py5Utilities(Sketch sketch) {
10+
// This constructor is called before the sketch starts running. DO NOT use
11+
// Processing methods here, as they may not work correctly.
12+
this.sketch = sketch;
13+
}
14+
15+
public void drawHappyFace(float x, float y, int color) {
16+
drawHappyFace(x, y, color, 100);
17+
}
18+
19+
public void drawHappyFace(float x, float y, int color, float size) {
20+
sketch.pushMatrix();
21+
sketch.pushStyle();
22+
23+
// set style
24+
sketch.noStroke();
25+
sketch.fill(color);
26+
27+
// position face
28+
sketch.translate(x, y);
29+
sketch.scale(size / 100.0f);
30+
sketch.translate(-50, -50);
31+
32+
// head
33+
sketch.ellipse(50, 50, 80, 80);
34+
35+
// eyes and nose
36+
sketch.fill(0);
37+
sketch.ellipse(35, 40, 15, 15);
38+
sketch.ellipse(65, 40, 15, 15);
39+
sketch.ellipse(50, 55, 10, 10);
40+
41+
// mouth
42+
sketch.stroke(0);
43+
sketch.noFill();
44+
sketch.strokeWeight(3);
45+
sketch.arc(50, 50, 50, 50, Sketch.radians(20), Sketch.radians(160));
46+
47+
sketch.popStyle();
48+
sketch.popMatrix();
49+
}
50+
}

0 commit comments

Comments
 (0)