Skip to content

Commit 02f21dd

Browse files
committed
Processing library examples
1 parent 0826d49 commit 02f21dd

File tree

4 files changed

+190
-0
lines changed

4 files changed

+190
-0
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#!/usr/bin/env jruby -v -W2
2+
# frozen_string_literal: true
3+
require 'propane'
4+
# DXF DXF Export
5+
# by Simon Greenwold.
6+
#
7+
# Press the 'R' key to export a DXF file.
8+
class DXFExport < Propane::App
9+
load_library :dxf
10+
include_package 'processing.dxf'
11+
12+
attr_reader :recording
13+
14+
def setup
15+
sketch_title 'DXF Export'
16+
noStroke
17+
sphereDetail(12)
18+
@recording = false
19+
end
20+
21+
def draw
22+
begin_raw(DXF, data_path('output.dxf')) if recording # Start recording to the file
23+
lights
24+
background(0)
25+
translate(width / 3, height / 3, -200)
26+
rotate_z(map1d(mouse_y, (0..height), (0..PI)))
27+
rotateY(map1d(mouse_x, (0..width), (0..HALF_PI)))
28+
(-2..2).step do |y|
29+
(-2..2).step do |x|
30+
(-2..2).step do |z|
31+
push_matrix
32+
translate(120 * x, 120 * y, -120 * z)
33+
sphere(30)
34+
pop_matrix
35+
end
36+
end
37+
end
38+
end_raw if recording
39+
@recording = false # Stop recording to the file
40+
end
41+
42+
def key_pressed
43+
return unless key == 'R' || key == 'r' # Press R to save the file
44+
@recording = true
45+
end
46+
47+
def settings
48+
size(400, 400, P3D)
49+
end
50+
end
51+
52+
DXFExport.new
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#!/usr/bin/env jruby -v -W2
2+
# frozen_string_literal: true
3+
require 'propane'
4+
# HTTP Client.
5+
#
6+
# Starts a network client that connects to a server on port 80,
7+
# sends an HTTP 1.0 GET request, and prints the results.
8+
# Note that this code is not necessary for simple HTTP GET request:
9+
# Simply calling load_strings("http://www.processing.org") would do
10+
# the same thing as (and more efficiently than) this example.
11+
# This example is for people who might want to do something more
12+
# complicated later.
13+
class HTTPClient < Propane::App
14+
load_library 'net'
15+
16+
attr_reader :client, :data
17+
18+
def setup
19+
sketch_title 'Http Client'
20+
background(50)
21+
fill(200)
22+
# Connect to server on port 80
23+
@client = Client.new(self, 'www.ucla.edu', 80)
24+
# Use the HTTP "GET" command to ask for a Web page
25+
client.write("GET / HTTP/1.0\r\n")
26+
client.write("\r\n")
27+
end
28+
29+
def draw
30+
return unless client.available > 0
31+
data = client.read_string # ...then grab it and print it
32+
puts data
33+
end
34+
35+
def settings
36+
size(200, 200)
37+
end
38+
end
39+
40+
HTTPClient.new
41+
# @todo replace with a pure ruby alternative
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#!/usr/bin/env jruby -v -W2
2+
# frozen_string_literal: true
3+
require 'propane'
4+
# Shared Drawing Canvas (Client)
5+
# by Alexander R. Galloway. Translated to JRubyArt by Martin Prout
6+
#
7+
# The Processing Client class is instantiated by specifying a remote address
8+
# and port number to which the socket connection should be made. Once the
9+
# connection is made, the client may read (or write) data to the server.
10+
# NB: Start the Shared Drawing Canvas (Server) program first
11+
class SimpleClient < Propane::App
12+
load_library :net
13+
14+
attr_reader :c
15+
16+
def settings
17+
size(450, 255)
18+
end
19+
20+
def setup
21+
sketch_title 'Client'
22+
background(204)
23+
stroke(0)
24+
frame_rate(5) # Slow it down a little
25+
# Replace with your server's IP and port
26+
@c = Client.new(self, '127.0.0.1', 12_345)
27+
end
28+
29+
def draw
30+
if mouse_pressed?
31+
# Draw our line
32+
stroke(255)
33+
line(pmouse_x, pmouse_y, mouse_x, mouse_y)
34+
# Send mouse coords to other person
35+
c.write(format("%d %d %d %d\n", pmouse_x, pmouse_y, mouse_x, mouse_y))
36+
end
37+
# Receive data from server
38+
return unless c.available > 0
39+
input = c.read_string
40+
# Split values into an array and convert to int
41+
data = input.split(' ').map(&:to_i)
42+
# Draw line using received coords
43+
stroke(0)
44+
line(*data)
45+
end
46+
end
47+
SimpleClient.new
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#!/usr/bin/env jruby -v -W2
2+
# frozen_string_literal: true
3+
require 'propane'
4+
# Shared Drawing Canvas (Server)
5+
# by Alexander R. Galloway. Translated to JRubyArt by Martin Prout
6+
#
7+
# A server that shares a drawing canvas between two computers.
8+
# In order to open a socket connection, a server must select a
9+
# port on which to listen for incoming clients and through which
10+
# to communicate. Once the socket is established, a client may
11+
# connect to the server and send or receive commands and data.
12+
# Get this program running and then start the Shared Drawing
13+
# Canvas (Client) program so see how they interact.
14+
class SketchServer < Propane::App
15+
load_library :net
16+
17+
attr_reader :s
18+
19+
def settings
20+
size(450, 255)
21+
end
22+
23+
def setup
24+
sketch_title 'Server'
25+
background(204)
26+
stroke(0)
27+
frame_rate(5) # Slow it down a little
28+
@s = Server.new(self, 12_345) # Start a simple server on a port
29+
end
30+
31+
def draw
32+
if mouse_pressed?
33+
# Draw our line
34+
stroke(255)
35+
line(pmouse_x, pmouse_y, mouse_x, mouse_y)
36+
# Send mouse coords to other person
37+
s.write(format("%d %d %d %d\n", pmouse_x, pmouse_y, mouse_x, mouse_y))
38+
end
39+
# Receive data from client
40+
c = s.available
41+
return if c.nil?
42+
input = c.read_string
43+
# Split values into an array and convert to int
44+
data = input.split(' ').map(&:to_f).to_java(:float)
45+
# Draw line using received coords
46+
line(*data)
47+
end
48+
end
49+
50+
SketchServer.new

0 commit comments

Comments
 (0)