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

Saturday, 22 November 2008

Exploring Classes and Attributes in Processing

For my chunk I will be trying to consolidate the readers understanding of a class, by using a class for my Hybrid Shape. To explore the issues involved with processing and classes I have created a SimpleShape class (it's a square), defined using vertex (coordinates) within the beginShape() endShape() environment. The idea is my shape is symmetrical with a default center at (0,0) using cartesian coordinates. However the user of SimpleShape class can provide alternative coordinates to position the shape anywhere on the window (even off the window should they wish). The class also has a color attribute that can be set, the default color is white, with no transparency.
Now this is where I can see problems arising since processing uses 'color' as keyword, so anyone thinking too much in the java mould is apt to get it wrong, and the neat idea of classes having a capitalised first letter goes out the window. Using classes clearly can have their place within processing, even in my example its easy to see how the class acts as a factory and many instances can easily be created once the class has been built, it just seems to go against the grain a bit of the simplicity of the processing environment.
NB: the SimpleShape has a default size of 20 x 20, this can be scaled by entering a scale factor, there are no limits, perhaps I should have created some?
Here I chose to put class and the applet into different files this is how it looks on your ide:-



When the program is run this what you get:-



Here is the class code:-

   1:class SimpleShape {  
2: float xp, yp;
3: float myScale;
4: color c;
5: SimpleShape(){
6: xp = 0.0f;
7: yp = 0.0f;
8: myScale = 1.0f;
9: c = color(255);
10: }
11:
12: SimpleShape(float xpos, float ypos){
13: xp = xpos;
14: yp = ypos;
15: myScale = 1.0f;
16: c = color(255);
17: }
18:
19: void setScale(float myScale){
20: this.myScale =myScale;
21: }
22:
23: void setColor(int r, int g, int b){
24: setColor(r, g, b, 100);
25: }
26:
27: void setColor(int r, int g, int b, int trans){
28: this.c = color(r, g, b, trans);
29: }
30:
31: void drawSimpleShape(){
32: fill(c);
33: beginShape();
34: vertex((-10 * myScale) + xp, (-10 * myScale) + yp);
35: vertex((-10 * myScale) + xp, (10 * myScale) + yp);
36: vertex((-10 * myScale) + xp, (10 * myScale) + yp);
37: vertex((10 * myScale) + xp, (10 * myScale) + yp);
38: vertex((10 * myScale) + xp, (10 * myScale) + yp);
39: vertex((10 * myScale) + xp, (-10 * myScale) + yp);
40: vertex((10 * myScale) + xp, (-10 * myScale) + yp);
41: vertex((10 * myScale) + xp, (-10 * myScale) + yp);
42: vertex((-10 * myScale) + xp, (-10 * myScale) + yp);
43: endShape(CLOSE);
44: }
45:}


Here is the applet code:-
   1:void setup() {
2: size(600, 600);
3: SimpleShape shape = new SimpleShape(width/2, height/2);
4: shape.setScale(8);
5: shape.setColor(0, 255, 255);
6: shape.drawSimpleShape();
7: SimpleShape shape2 = new SimpleShape(width/3, height/3);
8: shape2.setColor(255,0,0, 100);
9: shape2.setScale(13);
10: shape2.drawSimpleShape();
11:}

2 comments:

  1. I've since read up a bit more on the 'color' type, and its representation in processing. Essentially it is stored as int value, albeit with a very strange interface. Whatever it is, there is potential for confusion.

    ReplyDelete
  2. Even more confusing, red, blue, green and scale are all keywords. However it also shows what a good idea it is to present your code using 'syntax highlighting'.

    ReplyDelete

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