We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › new to OOP, what am I doing wrong
Page Index Toggle Pages: 1
new to OOP, what am I doing wrong? (Read 931 times)
new to OOP, what am I doing wrong?
Oct 18th, 2009, 9:31am
 

Hello, I´m kinda trying to create a class here, based on an beutiful example found on the forum, that features a glowing point that reminds me of a star, I wanted to use it to create many of them, but the code just doesn´t work, once I got the message "class not found", then the object just wasn´t creating, and I can´t see what´s wrong. Could please someone  be so kind to help me?

void draw(){
...
for (int s = width/8; s < width; s++){
     for(int t = height/4; t < height; t++){
       star = new Stars(color(random(0,255), random(0,255), random(0,255)), 2, 4, 2, s, t, true);
       star.display();
     }
}
...
class Stars {
   color c;
   float rMin;
   float rMax;
   float r;
   float Xpos;
   float Ypos;
   boolean grow;
 
   // The Constructor
   Stars(color tempC, float tempRMin, float tempRMax, float tempR,  float tempXpos, float tempYpos, boolean tempGrow) {
     c = tempC;
     rMin = tempRMin;
     rMax = tempRMax;
     r = tempR;
     Xpos = tempXpos;
     Ypos = tempYpos;
     grow = tempGrow;
   }
 
   void display() {
     background(0);
     stroke(c);
     fill(c);
     r = rMin;
     ellipse(Xpos, Ypos, r, r);
     noFill();
     for (int i = 1; i < 20; i++)
      {
        stroke(red(c), green(c), blue(c), 255/i);
        strokeWeight(2);
        ellipse(Xpos, Ypos, r+i, r+i);
      }
     if ( grow )
      {
        r += 8/frameRate;
        if ( r > rMax ) grow = false;
      }
     else
      {
        r -= 8/frameRate;
        if ( r < rMin ) grow = true;
      }
   }
 }

During the last two weeks I'm exploring Processing and I've started reading Ira Greenberg's book,which is fantastic, and seeing all these marvelous works here makes me regret I'm so noob! Cry
Re: new to OOP, what am I doing wrong?
Reply #1 - Oct 18th, 2009, 9:34am
 
Btw, here the original code,that I found, I never intended to claim it or something, my vision is that since it's an open community, new members are free to use whatever they find for educational purposes,

please correct me if I'm wrong


color c = color(255, 124, 0);
float rMin = 2;
float rMax = 4;
float r = rMin;
boolean grow = true;

void setup()
{
 size(200,200);
 frameRate(30);
 ellipseMode(CENTER_RADIUS);
 smooth();
 r = rMin;
}
void draw()
{
background(0);
stroke(c);
fill(c);
ellipse(height/2, width/2, r, r);
noFill();
for (int i = 1; i < 20; i++)
{
  stroke(red(c), green(c), blue(c), 255/i);
  strokeWeight(2);
  ellipse(height/2, width/2, r+i, r+i);
}
if ( grow )
{
  r += 8/frameRate;
  if ( r > rMax ) grow = false;
}
else
{
  r -= 8/frameRate;
  if ( r < rMin ) grow = true;
}
}
Re: new to OOP, what am I doing wrong?
Reply #2 - Oct 18th, 2009, 9:40am
 
is this your whole code? could you paste all you have done, presetup and setup... cause in this example, you just dont create the objects
Re: new to OOP, what am I doing wrong?
Reply #3 - Oct 18th, 2009, 9:53am
 

I thought I did it here:
void draw(){
...
for (int s = width/8; s < width; s++){
    for(int t = height/4; t < height; t++){
      star = new Stars(color(random(0,255), random(0,255), random(0,255)), 2, 4, 2, s, t, true);
      star.display();
    }
}

Shocked Huh
Re: new to OOP, what am I doing wrong?
Reply #4 - Oct 18th, 2009, 10:18am
 
Well, if that's in draw(), you're creating them *each frame* ... you might want to create them in setup, with one loop, and display() them in draw, with another loop.  And, you appear to only have one Stars object (named "star") which gets re-created many times...you can use an array[] of stars to get multiples.
Re: new to OOP, what am I doing wrong?
Reply #5 - Oct 18th, 2009, 10:51am
 
Thanks for the hint, I always get confused wuth this looping Angry

Here's a my disastrous sketch, eventually it will be followed bu a text explaining what it's all about, but right now it;s just a sequence of different things:


 PFont font;
 Stars[] stars;
 int i, k;
 float x, h, n = 2,m;
 float y;
 float ySpeed = .98;
 float xSpeed = 1.005;
 float j;
 float speedX, speedY;
 float adjustl;
 float accelerationX = .04, accelerationY = .05;
 boolean step1 = false;
 boolean step2 = false;
 boolean step3 = false;
 boolean step4 = false;
 boolean step5 = false;

  //sin parameters
 float px, py;
 float angle;
 float radius = 50;
 float frequency = 2;

 void setup(){
   size(500, 300);
   background(0);
   frameRate(30);
   x = width/2;
   y = height/2;
   smooth();
   font = loadFont("Arial-Black-16.vlw");
   stars = new Stars[3];
 }

 void draw(){
   if (i<=width/2+5){
   move(100, 0, 0, width/2, height/2, width, height/2, "X", width-20, height/2-5,
   width-10, height/2-5, width-10, height/2+5, width, height/2,
   i+250, height/2);
   speedX += accelerationX;
   i+=speedX;
 }
   else {
   step1 = true;
 }
 if(step1)
 {  
   if (j<height){
   move(0, 100, 0, width/2, height/2, 0, height,
   "Y", 1, height-10,1, height-1, 10, height-10, 20, height-10, height-j,350-2*(height-j)/3);
   speedY += accelerationY;
   j+=speedY;
 }
   else {
     step2 = true;
   }

 }
 
 if(step2)
 {
   if (x>=width/2&&x<width) {    
     move(0, 0, 100, width/2, height/2, width/2, 0,
    "Z", width/2-20, 20, width/2, 1, width/2-5, 5, width/2+5, 5, x, y);
     x *= xSpeed;
     ySpeed += accelerationY;
     y -= ySpeed;  
   }
   else {      
     step3 = true;
   }
 }
 
if (step3){
  drawClock();
  m++;
 
  if(m==200){
    background(0);
    step4 = true;
  }
 }
 
 if (step4){
   for (int s = width/8; s < width; s++){
     for(int t = height/4; t < height; t++){
       
       for (int p = 0; p< stars.length;p++){stars[p] = new Stars(color(random(0,255), random(0,255), random(0,255)), 2, 4, 2, s, t, true);
       stars[p].display();}
     }
   }
 }
}


I suppose the problem is somewhere here, I need to show the clock for some time, hide it and  draw a few random glowing points. But right now it just hangs after showing the clock.

 void move(int ellipsecolor1, int ellipsecolor2, int ellipsecolor3, int x1_line, int x2_line, int y1_line,
 int y2_line, String name, int textpos_x, int textpos_y,  int x1_triangle, int y1_triagle, int x2_triangle, int y2_triangle, int x3_triangle, int y3_triangle, float x_ellipse, float y_ellipse)
{
 fill(0, 50);
 rect(0,0, width, height);
 stroke(ellipsecolor1, ellipsecolor2, ellipsecolor3, 50);
 strokeWeight(2);
 line(x1_line, x2_line, y1_line, y2_line);
 fill(ellipsecolor1, ellipsecolor2, ellipsecolor3);
 textFont(font);
 text(name, textpos_x, textpos_y);
 triangle(x1_triangle, y1_triagle, x2_triangle, y2_triangle, x3_triangle, y3_triangle);
 stroke(ellipsecolor1, ellipsecolor2, ellipsecolor3);
 ellipse(x_ellipse, y_ellipse, 10, 10);
}

 void drawStaticSin(float n){
   // draw static curve - y = sin(x)
   for (int i = 0; i< width; i++){
     py = sin(radians(angle))*(n*radius);
     stroke(255, 80);
     point(i, py);
     angle -= frequency;
 }
}
 void drawClock(){
   background(0);
   noFill();
   noStroke();
   // Angles for sin() and cos() start at 3 o'clock;
   // subtract HALF_PI to make them start at the top
   ellipse(3*width/4, height/4, 20, 20);
   float s = map(second(), 0, 60, 0, TWO_PI);
   float m = map(minute() + norm(second(), 0, 60), 0, 60, 0, TWO_PI);
   float h = map(hour() + norm(minute(), 0, 60), 0, 24, 0, TWO_PI * 2);
   stroke(255);
   strokeWeight(1);
   line(100, 100, cos(s) * 72 + 100, sin(s) * 72 + 100);
   strokeWeight(2);
   line(100, 100, cos(m) * 60 + 100, sin(m) * 60 + 100);
   strokeWeight(4);
   line(100, 100, cos(h) * 50 + 100, sin(h) * 50 + 100);
   
   // Draw the minute ticks
   strokeWeight(2);
   for (int a = 0; a < 360; a+=6) {
     float x = 100 + ( cos(radians(a)) * 72 );
     float y = 100 + ( sin(radians(a)) * 72 );
     point(x, y);
   }
 }
 
 class Stars {
   color c;
   float rMin;
   float rMax;
   float r;
   float Xpos;
   float Ypos;
   boolean grow;
 
   // The Constructor
   Stars(color tempC, float tempRMin, float tempRMax, float tempR,  float tempXpos, float tempYpos, boolean tempGrow) {
     c = tempC;
     rMin = tempRMin;
     rMax = tempRMax;
     r = tempR;
     Xpos = tempXpos;
     Ypos = tempYpos;
     grow = tempGrow;
   }
 
   void display() {
     background(0);
     stroke(c);
     fill(c);
     r = rMin;
     ellipse(Xpos, Ypos, r, r);
     noFill();
     for (int i = 1; i < 20; i++)
      {
        stroke(red(c), green(c), blue(c), 255/i);
        strokeWeight(2);
        ellipse(Xpos, Ypos, r+i, r+i);
      }
     if ( grow )
      {
        r += 8/frameRate;
        if ( r > rMax ) grow = false;
      }
     else
      {
        r -= 8/frameRate;
        if ( r < rMin ) grow = true;
      }
   }
 }
Re: new to OOP, what am I doing wrong?
Reply #6 - Oct 18th, 2009, 10:55am
 
This is also a part of the same picture, I wanted to make a vibrating string, but instead I accidentally created this:

float angle = 0;
 int n, j;
 float amplitude = 40;
 float x = 0, y = 0;
 float xSpeed = 0.5;
 float frequency = 3.0;
 /*float damping = .994;*/

 void setup(){
   size(400, 400);
   background(0);
   stroke(255);
   strokeWeight(2);
   smooth();
 }

 void draw(){
   background(0);
   amplitude = amplitude*pow(-1,n);
   n++;
   for (int a = 0; a< width; a++){
     for (int i = 0; i< width; i++){
       y = 75 + sin(radians(angle))*(amplitude*(a/2));
       point(i, y);
       angle -= frequency;
       frequency+=.000001;
     }
 }
}

What do I do with an amplitude to make the sine reduce, creating the desires effect? It's like when you're rotating, say, a rope or a cord, and it spines
Re: new to OOP, what am I doing wrong?
Reply #7 - Oct 18th, 2009, 4:42pm
 
Well...I think you are on the road to good things.  The mistakes are getting more awesome.

...
Page Index Toggle Pages: 1