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 › How do I make a static class
Page Index Toggle Pages: 1
How do I make a static class? (Read 1855 times)
How do I make a static class?
Jun 6th, 2010, 8:39pm
 
Hi, I'm trying to make a static class that contains code for drawing shapes but I can't quite figure out how to get a static class working. Here's what I have in the main tab:

Quote:
void setup() {
  smooth();
  noStroke();
}

void draw() {
  background(0);
  fill(255);
  Shapes.pentagon(10, 20, 10);
}



and here's the attempted static class:

Quote:
static class Shapes{

  static void pentagon(int x, int y, float radius){
    beginShape();
    for (int deg = 0; deg < 360; deg += 72) {
      float xPos = -sin(radians(deg)) * radius;
      float yPos = -cos(radians(deg)) * radius;  
      vertex(x + xPos , y + yPos);
    }
    endShape(CLOSE);
  }
}



I think I'm just tired. I'll give it another shot tomorrah
Re: How do I make a static class?
Reply #1 - Jun 7th, 2010, 12:21am
 
you will have trouble because your sketch is actually an inner class - it gets wrapped during the processing, er, pre-processing

(export a minimal app and look at the generated java file)

public class ProjectName extends PApplet {

public void setup() {
...
}

public void draw() {
...
}

 class YourStaticClass {
 }

}
Re: How do I make a static class?
Reply #2 - Jun 7th, 2010, 1:18am
 
Actually, you don't really need static classes, you can just put the static variables and static functions at the top level (ie., somehow, at "global" level, but actually these are fields and methods of the class created from your sketch).

Technically, you can make static classes by putting them in .java files, but then you loose easy access to PApplet functions (like beginShape or sin).
Re: How do I make a static class?
Reply #3 - Jun 7th, 2010, 10:40am
 
Ah ok cool thank you. I'll do this instead:

Quote:
float x, y;
float radius;
int shapeID;
Shape shapeX;

void setup() {
  smooth();
  noStroke();
  x = width/2;
  y = height/2;
  radius = 10;
  shapeID = 1;
}

void draw() {
  background(0);
  fill(255);
  shapeX = new Shape(x, y, radius, shapeID);
  shapeX.drawShape();
}




Quote:
class Shape{
  float x, y;
  float radius;
  int shapeID;

  Shape(float x, float y, float radius, int shapeID){
    this.x = x;
    this.y = y;
    this.radius = radius;
    this.shapeID = shapeID;
  }

  void drawShape() {
    if (shapeID == 1) {
      beginShape();
      for (int deg = 0; deg < 360; deg += 72) {
        float xPos = -sin(radians(deg)) * radius;
        float yPos = -cos(radians(deg)) * radius;  
        vertex(x + xPos , y + yPos);
      }
      endShape(CLOSE);
    }
  }
}

Page Index Toggle Pages: 1