public
Last active

Processing+jython color cube

  • Download Gist
sketch.py
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
"""
jython version of http://hipstersinc.com/blog/2007/5/16/jruby_processingorg_howto/
Spinning color cube controled by mouse motion.
 
1. Install Jython
2. Add Processing's core.jar to CLASSPATH: export CLASSPATH=/Applications/Processing.app/Contents/Resources/Java/core.jar
3. ~/jython2.5b3/bin/jython sketch.py
"""
 
 
from processing.core import PApplet
from javax.swing import JFrame
 
class Sketch(PApplet):
def setup(self):
self.size(300, 300, PApplet.P3D)
self.noStroke()
self.colorMode(PApplet.RGB, 1)
 
self.xmag = 0
self.ymag = 0
self.new_xmag = 0
self.new_ymag = 0
 
 
def draw(self):
self.background(0.5, 0.5, 0.45)
 
self.pushMatrix()
 
self.translate(self.width / 2, self.height / 2, -30)
 
self.new_xmag = float(self.mouseX) / self.width * PApplet.TWO_PI
self.new_ymag = float(self.mouseY) / self.height * PApplet.TWO_PI
 
 
diff_x = self.xmag - self.new_xmag
 
if abs(diff_x) > 0.01:
self.xmag -= diff_x / 4.0
 
diff_y = self.ymag - self.new_ymag
if abs(diff_y) > 0.01:
self.ymag -= diff_y/4.0
 
self.rotateX(-self.ymag)
self.rotateY(-self.xmag)
 
self.scale(50)
self.beginShape(self.QUADS)
 
self.fill(0, 1, 1); self.vertex(-1, 1, 1)
self.fill(1, 1, 1); self.vertex( 1, 1, 1)
self.fill(1, 0, 1); self.vertex( 1, -1, 1)
self.fill(0, 0, 1); self.vertex(-1, -1, 1)
 
self.fill(1, 1, 1); self.vertex( 1, 1, 1)
self.fill(1, 1, 0); self.vertex( 1, 1, -1)
self.fill(1, 0, 0); self.vertex( 1, -1, -1)
self.fill(1, 0, 1); self.vertex( 1, -1, 1)
 
self.fill(1, 1, 0); self.vertex( 1, 1, -1)
self.fill(0, 1, 0); self.vertex(-1, 1, -1)
self.fill(0, 0, 0); self.vertex(-1, -1, -1)
self.fill(1, 0, 0); self.vertex( 1, -1, -1)
 
self.fill(0, 1, 0); self.vertex(-1, 1, -1)
self.fill(0, 1, 1); self.vertex(-1, 1, 1)
self.fill(0, 0, 1); self.vertex(-1, -1, 1)
self.fill(0, 0, 0); self.vertex(-1, -1, -1)
 
self.fill(0, 1, 0); self.vertex(-1, 1, -1)
self.fill(1, 1, 0); self.vertex( 1, 1, -1)
self.fill(1, 1, 1); self.vertex( 1, 1, 1)
self.fill(0, 1, 1); self.vertex(-1, 1, 1)
 
self.fill(0, 0, 0); self.vertex(-1, -1, -1)
self.fill(1, 0, 0); self.vertex( 1, -1, -1)
self.fill(1, 0, 1); self.vertex( 1, -1, 1)
self.fill(0, 0, 1); self.vertex(-1, -1, 1)
 
self.endShape()
self.popMatrix()
 
 
if __name__ == "__main__":
sketch = Sketch()
 
frame = JFrame()
frame.defaultCloseOperation = JFrame.EXIT_ON_CLOSE
frame.contentPane.add(sketch)
sketch.init()
 
frame.pack()
frame.visible = True

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.