Created
April 22, 2009 03:58
-
-
Save phred/99571 to your computer and use it in GitHub Desktop.
Processing+jython color cube
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment