Skip to content
Martin Prout edited this page Apr 8, 2014 · 9 revisions

Simple vanilla processing sketch:-

CircleTest.pde

Circle circle; // a variable declared here is available in setup and draw

void setup() {
  size(200, 200);
  circle = new Circle(100);
}

void draw() {
  background(0);
  circle.draw();
}

Circle.pde (class in separate tab in ide)

class Circle {
  float size; // a variable declared here is available in constructor and draw
  Circle(float sz) {
    size = sz;
  }
  
  void draw() {
    fill(200);
    ellipse(width / 2, height /2, size, size);
  }
}

When translated to java (by processing) prior to compiling

CircleTest.java

import processing.core.*; 
import processing.data.*; 
import processing.event.*; 
import processing.opengl.*; 

import java.util.HashMap; 
import java.util.ArrayList; 
import java.io.File; 
import java.io.BufferedReader; 
import java.io.PrintWriter; 
import java.io.InputStream; 
import java.io.OutputStream; 
import java.io.IOException; 

public class CircleTest extends PApplet {

Circle circle;

public void setup() {
  size(400, 400);
  circle = new Circle(100);
}

public void draw() {
  background(0);
  circle.draw();
}

class Circle { // sketch width and height accessible to this java inner class
  float size;
  Circle(float sz) {
    size = sz;
  }
  public void draw() {
    fill(200);
    ellipse(width / 2, height /2, sz, sz);
  }
}

  static public void main(String[] passedArgs) {
    String[] appletArgs = new String[] { "CircleTest" };
    if (passedArgs != null) {
      PApplet.main(concat(appletArgs, passedArgs));
    } else {
      PApplet.main(appletArgs);
    }
  }
}

circle_test.rb a bare ruby sketch, why write boiler-plate if don't need to?

attr_reader :circle # a convenience to create a getter in ruby

def setup
  size 200, 200
  @circle = Circle.new 100 # '@' prefix is required for assigment
end

def draw
  background 0
  circle.draw             # using circle getter here
end

class Circle # cannot access Sketch width and height without using $app
  attr_reader :size # a convenience to create a getter in ruby

  def initialize sz
    @size = sz
  end
  
  def draw
    fill 200
    ellipse $app.width / 2, $app.height / 2, size, size
  end
end

This bare sketch is wrapped for you by ruby-processing before sending to jruby, with the encapsulating Processing module doing all the require 'java' and library includes

class Sketch < Processing::App
  
  attr_reader :circle
  
  def setup
    size 200, 200
    @circle = Circle.new 100
  end
  
  def draw
    background 0
    circle.draw
  end
  
  class Circle 
    attr_reader :size
    def initialize sz
      @size = sz
    end
    
    def draw
      fill 200
      ellipse $app.width / 2, $app.height / 2, size, size
    end
  end
  
end