Recently I've been looking at Povray, pyprocessing, and cfdg (version 3.0) as tools for creating digital images. I have branched two separate blogs where I mainly explore jruby + processing and processing.py

Tuesday, 11 November 2008

Creating and using libraries

Its pretty easy to create your own java libraries and include them into your processing application. Continuing with my theme of φ the golden ratio I have created a library class with a static variable PHI. However the main purpose the class is to return a Dimension instance such that the ratio of width to height can be defined(eg as φ, π or sqrt(2)). To prevent the ratio collapsing for small values, I have set a minimum size of 10 for either width or height. Here is my java code.

   1:
2:/**
3: * The idea for this class is that it can return either
4: * width and height, or a Dimension instance, where the ratio
5: * of the two sides is special number eg phi, pi or root(2)
6: * The minimum size has been set to 10 pixels
7: * @author Martin Prout
8: */
9:public class SpecialDimension {
10:
11: public final int MINSIZE = 10;
12: // static constant
13: public static final double PHI = (1 + Math.sqrt(5)) / 2;
14: public java.awt.Dimension special;
15: /**
16: * A version where the width is set as a proportion of height
17: * using the ratio value typically PHI or PI or root 2
18: * Arbitary minimum value 10 x 10. Since ratio is typically an
19: * irrational number rounding is required
20: * @param height int base
21: * @param ratio double multiplier
22: */
23: public SpecialDimension(int height, double ratio) {
24: if (height < MINSIZE)
25: { height = MINSIZE;}
26: int width = (int)Math.ceil (ratio*height);
27: if (width < MINSIZE){ // then ratio < 1
28: width = MINSIZE;
29: height = (int)Math.ceil(MINSIZE/ratio);
30: }
31: special = new java.awt.Dimension (width, height);}
32: /**
33: *
34: * @return special width int
35: */
36: public int getWidth(){return special.width;}
37: /**
38: *
39: * @return special height int
40: */
41: public int getHeight(){
42: return special.height;
43: }
44: /**
45: *
46: * @return special Dimension
47: */
48: public java.awt.Dimension getSize(){
49: return special;
50: }
51:
52:}


To use this class as library for a processing application, compile it, archive it as jar file and place it in the 'code' folder.
Heres my processing code:

   1:SpecialDimension special = new SpecialDimension(400, SpecialDimension.PHI);
2:
3:final java.awt.Dimension WSIZE = special.getSize();
4:
5:void setup() {
6: size(WSIZE.width, WSIZE.height);
7: background(03366);
8: noLoop();
9:}


Displays OK for me as a blue window, default aspect ratio is landscape, a refinement would add a switch to the library class to create a portrait look (as per Mondrian).

No comments:

Post a Comment

Followers

Blog Archive

About Me

My photo
Pembrokeshire, United Kingdom
I have developed JRubyArt and propane new versions of ruby-processing for JRuby-9.1.5.0 and processing-3.2.2