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 › Constructor unedified
Page Index Toggle Pages: 1
Constructor unedified? (Read 1753 times)
Constructor unedified?
Apr 14th, 2010, 9:56am
 
Hi everyone Im a bit new to this so excuse me if this is stupid, but Im trying to write some code for my coursework, the idea at the moment is just to draw a yellow background with red squares that change the canvas size depending on how many squares you have also they should stay the same distance apart, but Im having a problem...
Heres my code so far;

class Square {}

int N = 8;

int W = 40 * (2 * N + 1),
   H = W/2;
color Red = color(255, 0, 0),
     Yellow = color(255, 255, 0);
color BG_C = Yellow;
int Side = 40;
int Cy = (H - Side)/2;
Square[] Sqs = new Square[N];
void setup()
   {
       size(W, H);
       background(BG_C);
       for(int i = 0; i < N; i++)
       {
          Square [] Sqs = new Square((2 * i + 1) * Side + Side / 2, Cy + Side / 2, Side);
           square.FillCol = Red;
           square.Draw();
       }

   }
   void draw()
   {
     
   }

Im getting a constructor undefined error on the line;

Square [] Sqs = new Square((2 * i + 1) * Side + Side / 2, Cy + Side / 2, Side);

If anyone can help me Id appreciate it! Also any tips wouldn't hurt! Thanks in advance!
Re: Constructor unedified?
Reply #1 - Apr 14th, 2010, 10:40am
 
The "constructor" is the line inside your class (which is currently just: Square{}) that describes how a Square object is defined -- the "setup()" of your square, in a way.  The line you're trying to pass to the constructor is:

new Square((2 * i + 1) * Side + Side / 2, Cy + Side / 2, Side);

so it looks like your Square is intended to receive 3 numbers (floats?)

class Square{
 float num1, num2, num3; //  <-- these are owned by the square
 Square( float input1, float input2, float input3){
   // ^ this is the constructor.
   // usually you'll want to assign the variables being passed to the constructor:
   num1 = input1; // etc.
 }
}

Re: multiple posts on the board, you can delete the others.
Re: Constructor unedified?
Reply #2 - Apr 15th, 2010, 1:14am
 
Hi thanks for the reply! Ive deleted the other posts, my laptop was having a funny 5 minutes I think. Ive put the code you suggested in and Ive gotten a bit further I now get a new error message! Heres the code now;

class Square{
float num1, num2, num3;
Square( float input1, float input2, float input3){
num1 = input1;
num2 = input2;
num3 = input3;
}
}

int N = 8;

int W = 40 * (2 * N + 1),
   H = W/2;
color Red = color(255, 0, 0),
     Yellow = color(255, 255, 0);
color BG_C = Yellow;
int Side = 40;
int Cy = (H - Side)/2;
Square[] Sqs = new Square[N];
void setup()
   {
       size(W, H);
       background(BG_C);
       for(int i = 0; i < N; i++)
       {
          Square [] Sqs = new Square((2 * i + 1) * Side + Side / 2, Cy + Side / 2, Side);
           square.FillCol = Red;
           square.Draw();
       }

   }
   void draw()
   {
     
   }


The new error code is saying that it "cannot convert from Squarecw1.Square to Squarecw1.Square[]"
Thanks again for the help!
Re: Constructor unedified?
Reply #3 - Apr 15th, 2010, 1:53am
 
sparky3189 wrote on Apr 15th, 2010, 1:14am:
Code:
Square [] Sqs = new Square((2 * i + 1) * Side + Side / 2, Cy + Side / 2, Side); 


Bad syntax. Don't re-declare global variables. And use:
Sqs[i] = new Square((2 * i + 1) * Side + Side / 2, Cy + Side / 2, Side);
Or, rather:
Code:
Square square = new Square((2 * i + 1) * Side + Side / 2, Cy + Side / 2, Side);
Sqs[i] = square;
square.FillCol = Red;
square.Draw();

Of course, you also have to define Square's FillCol and Draw().
And probably call Draw() in draw(), instead.
Re: Constructor unedified?
Reply #4 - Apr 15th, 2010, 2:26am
 
Sorry PhiLho, could you explain to me what you mean when you say that I have to "define Square's FillCol and Draw(). And probably call Draw() in draw(), instead"? Sorry if this is all tedious, Ive gotten myself a bit confused! Thanks for the help again though!
Re: Constructor unedified?
Reply #5 - Apr 15th, 2010, 4:41am
 
I suppose FillCol is a fill color, it must be a field (a class variable) defined in the Square class. Then you have to use this value with fill() in the Draw() routine.
You have also to define a Draw() method (a class function) so each Square can draw itself, depending on the parameters of the object. BTW, try and find better name for your parameters, numx is quite obscure, x, y, and size, for example, are better.
Re: Constructor unedified?
Reply #6 - Apr 17th, 2010, 7:38am
 
Hi again everyone. Ive moved on a bit with my coding but Im stuck again. Im now trying to get the letters from the "legend" on to the squares, Ive loaded a font but Im not sure where to go from there. Does anyone have any ideas? Heres the code so far;

String Legend = "Sparshott";
PFont Font;
int N = Legend.length();
int W = 40 * (2 * N + 1),
   H = W/2;
color Red = color(255, 0, 0),
     Yellow = color(255, 255, 0);
color BG_C = Yellow;
color SC = Red;
int Side = 40;
int Cy = (H - Side)/2;
Square[] Sqs = new Square[N];


void setup()
{
 Font = loadFont("BookmanOldStyle-Bold-24.vlw");
       textFont(Font);
 background(BG_C);
 int i;
 Square S;
 size(W, H);
   for(int j = 0; j < N; j++) {
     Square square = new Square(((3 + 4 * j) * Side)/2, H/2, Side);

           square.Draw();
         
  Sqs[j] = square;
 }
}

void draw()
{
 fill(SC);
 int i;
 Square S;
   for(i = 0; i < N; i++) {
     S = Sqs[i];
     S.Draw();
 }
}

The square class is in a separate tab heres the code for that just incase;

class Square {
 int X,
     Y;
 int letter;
 int Side;

 Square(int X, int Y, int Side) {
   this.X = X;
   this.Y = Y;
   this.Side = Side;
 }

 void Draw() {
   rectMode(CENTER);
   rect(X, Y, Side, Side);
   println(Legend.charAt(1));
 }
}


Thanks again for any help!
Re: Constructor unedified?
Reply #7 - Apr 17th, 2010, 9:53am
 
Hello thought Id just post to say Ive gotten a bit further, the problem now seems to be that I cant get the letters themselves to appear on the boxes, any help would be appreciated!

Code;

String Legend = "Cat";
PFont Font;
int Letter;
int N = Legend.length();
int W = 40 * (2 * N + 1),
   H = W/2;
color Red = color(255, 0, 0),
     Yellow = color(255, 255, 0);
color BG_C = Yellow;
color SC = Red;
int Side = 40;
int Cy = (H - Side)/2;
Square[] Sqs = new Square[N];


void setup()
{
 Font = loadFont("BookmanOldStyle-Bold-24.vlw");
       textFont(Font);
 background(BG_C);
 int i;
 Square S;
 size(W, H);
   for(int j = 0; j < N; j++) {
     Square square = new Square(((3 + 4 * j) * Side)/2, H/2, Side);

           square.Draw();
               text(Legend.charAt(Letter));
          Letter=Letter+1;
  Sqs[j] = square;
 }
}

void draw()
{
 fill(SC);
 int i;
 Square S;
   for(i = 0; i < N; i++) {
     S = Sqs[i];
     S.Draw();
 }
}

Next tabs code;

class Square {
 int X,
     Y;
 int Side;
 color FontCol;
 color BoxCol=Red;

 Square(int X, int Y, int Side) {
   this.X = X;
   this.Y = Y;
   this.Side = Side;
 }

 void Draw() {
   fill(BoxCol);
   rectMode(CENTER);
   rect(X, Y, Side, Side);
   fill(FontCol);
   textAlign(CENTER);
   text(Letter,X,Y);

 }
}

Just to be clear I want the letters C a t to appear on the individual boxes, and if I were to change the Legend from cat to Plane there would be more boxes with a letter on each, hope thats clear! Thank you for the help!
Re: Constructor unedified?
Reply #8 - Apr 17th, 2010, 10:33am
 
Each Square should know its Letter, ie. define this variable in the class itself.
Re: Constructor unedified?
Reply #9 - Apr 17th, 2010, 10:48am
 
Ive changed my code to;

String Legend = "Cat";
PFont Font;
int Letter;
int BoxLetter;
int N = Legend.length();
int W = 40 * (2 * N + 1),
   H = W/2;
color Red = color(255, 0, 0),
     Yellow = color(255, 255, 0);
color BG_C = Yellow;
color SC = Red;
int Side = 40;
int Cy = (H - Side)/2;
Square[] Sqs = new Square[N];


void setup()
{
 Font = loadFont("BookmanOldStyle-Bold-24.vlw");
       textFont(Font);
 background(BG_C);
 int i;
 Square S;
 size(W, H);
   for(int j = 0; j < N; j++) {
     Square square = new Square(((3 + 4 * j) * Side)/2, H/2, Side);

           square.Draw();
           
  Sqs[j] = square;
 }
}

void draw()
{
 fill(SC);
 int i;
 Square S;
   for(i = 0; i < N; i++) {
     S = Sqs[i];
     S.Draw();
 }
}



class Square {
 int X,
     Y;
 int Side;
 color FontCol;
 color BoxCol=Red;

 Square(int X, int Y, int Side) {
   this.X = X;
   this.Y = Y;
   this.Side = Side;
   BoxLetter=(Legend.charAt(Letter));
          Letter=Letter+1;
 }

 void Draw() {
   fill(BoxCol);
   rectMode(CENTER);
   rect(X, Y, Side, Side);
   fill(FontCol);
   textAlign(CENTER);
   text(BoxLetter,X,Y);

 }
}

But Im still getting the same problem :/

Re: Constructor unedified?
Reply #10 - Apr 17th, 2010, 1:34pm
 
BoxLetter must be defined in Square, like Side and FontCol are.
Re: Constructor unedified?
Reply #11 - Apr 17th, 2010, 1:37pm
 
Quote:
String Legend = "Awesome!"; //"Cat";
PFont Font;
int Letter;
//int BoxLetter;
int N = Legend.length();
int W = 40 * (2 * N + 1),
H = W/2;
color Red = color(255, 0, 0),
Yellow = color(255, 255, 0);
color BG_C = Yellow;
color SC = Red;
int Side = 40;
int Cy = (H - Side)/2;
Square[] Sqs = new Square[N];


void setup()
{
  Font = createFont("Ariel", 24 ); //loadFont("BookmanOldStyle-Bold-24.vlw");
  textFont(Font);
  background(BG_C);
  int i;
  Square S;
  size(W, H);
  for(int j = 0; j < N; j++) {
    Square square = new Square(((3 + 4 * j) * Side)/2, H/2, Side);

    square.Draw();

    Sqs[j] = square;
  }
}

void draw()
{
  fill(SC);
  int i;
  Square S;
  for(i = 0; i < N; i++) {
    S = Sqs[i];
    S.Draw();
  }
}



class Square {
  int X,
  Y;
  int Side;
  color FontCol;
  color BoxCol=Red;
  char myLetter;
  Square(int X, int Y, int Side) {
    this.X = X;
    this.Y = Y;
    this.Side = Side;

    myLetter = (Legend.charAt(Letter));
    Letter=Letter+1;
  }

  void Draw() {
    fill(BoxCol);
    rectMode(CENTER);
    rect(X, Y, Side, Side);
    fill(FontCol);
    textAlign(CENTER);
    text(myLetter,X,Y);

  }
}



No problems here.
I changed your call to loadFont to a call to createFont. You can change this back.
The problem was that you were only using one variable to track the current letter, and after setting up all the squares, that single variable was equal to the last letter.
What I did was add a new character to each square class, so each square remembers it's own letter.
Re: Constructor unedified?
Reply #12 - Apr 20th, 2010, 1:45pm
 
Thats brilliant thanks! Im now needing some help with 1) getting the squares to move in a random direction along the X axes 2)getting the them to bounce off one another and the edges of the window. Could someone please help me with these problems? Thank you all so much for your help so far btw!  Smiley
Re: Constructor unedified?
Reply #13 - Apr 22nd, 2010, 3:28am
 
Ok so Ive gotten everything to work and am now on the last part of my work, so I was wondering; what is the easiest way to transform my squares into circles once the mouse is click or after a delay?
Thanks again for any help!
Page Index Toggle Pages: 1