Writing Processing in Kotlin

Thank you @cansik for posting your code! Very useful to learn this new approach :slight_smile:

Question: I’m trying to inherit from PGraphics3D but apparently when I call LCircle.draw() the PGraphics has not been initialized (NPE at clear()). Any ideas?

package com.hamoid

import processing.core.PApplet
import processing.core.PConstants

class Basic : PApplet() {
    lateinit var layer : LCircle
    companion object Factory {
        fun run() {
            val art = Basic()
            //art.setSize(700, 500)
            art.runSketch()
        }
    }

    override fun settings() {
        size(700, 500, PConstants.P3D)
    }

    override fun setup() {
        background(70.0f, 20.0f, 200.0f)
        layer = LCircle(this, 500, 500)
    }

    override fun draw() {
        line(0.0f, 0.0f, width * 1.0f, height * 1.0f);
        layer.draw()
    }
}

fun main() {
    Basic.run()
}

This is an example “hello world” layer:

package com.hamoid

import processing.core.PApplet
import processing.opengl.PGraphics3D

class LCircle(p5: PApplet, w: Int, h: Int) : PGraphics3D() {
    init {
        setParent(p5)
        setPrimary(false)
        setSize(w, h)
    }

    fun draw() {
        clear()
        fill(255)
        circle(width* 0.5f, height * 0.5f, 100.0f)
    }
}

Update: the thing that was missing was a beginDraw() / endDraw() inside fun draw(). The clean syntax (without the myPG. prefix) made me forget I was inside a PGraphics.