Extending a class

I am trying to figure out how to extend the PShape class to include three additional attributes. I apologize if attributes is the wrong term. I need to be able to know 3 items about the cells in my project, the location, position and orientation. I know I could calculate these and I am currently doing it, but if I could store the information in the Shape (XML??) and be able to import/export them is part of the eventual project.

The relevant code is below, if anybody has any suggestions, it would be greatly appreciated:

//----------------------------------------------------------
// Extended PShape
class MyPShape extends PShape{

  MyPShape( ){
  PVector position;      // Initial Positioning in row, column, file
  PVector location;      // cell location in x,y,z
  PVector orientation;   // cell orientation vestor in x,y,z
  //  super( );          // no idea how this works
  }  

  public void setOrientation(PVector thisorientation) {
  // orientation = thisorientation;  
   }

  public void getOrientation(PVector thisorientation) {
  // thisorientation = orientation;  
   }

   public void getLocation(PVector thislocation) {
  // thislocation = location;  
   }

   public void getPostition(PVector thisposition) {
  // thispostion = position;  
   }
 }
//----------------------------------------------------------
// Custom Cube Celled Brain Class

class CubeCelledBrain {

  float cellwidth, cellheight, celldepth, cellsize, cs, gridsize;  // width, height, depth, size of cell
  int cells_per_side, half_cells_per_side;
  int face_number=0, n = 0, count=0;
  String  name;

  // Position & location vectors 
  PVector[] vertices = new PVector[9];
  PVector tempvec, up, down, left, right, backwards;

  PShape  brain, cell, tempcell, tempface, pickedcell;
  PShape[] face = new PShape[7];

  //*****************************************************************************************************
  CubeCelledBrain(int tempN, float tempS, float tempG) {  // CONSTRUCTOR
    cells_per_side = tempN; 
    cs = tempS;                   
    gridsize = tempG;


    // cube composed of 8 verticies
    vertices[0] = new PVector(0,0,0);
    vertices[1] = new PVector(-cs/2, -cs/2, cs/2);
    vertices[2] = new PVector(cs/2, -cs/2, cs/2);
    vertices[3] = new PVector(cs/2, cs/2, cs/2);
    vertices[4] = new PVector(-cs/2, cs/2, cs/2);
    vertices[5] = new PVector(cs/2, -cs/2, -cs/2); 
    vertices[6] = new PVector(-cs/2, -cs/2, -cs/2);
    vertices[7] = new PVector(-cs/2, cs/2, -cs/2);
    vertices[8] = new PVector(cs/2, cs/2, -cs/2);  
    up          = new PVector (0, 1, 0);

    // fv=faceverticies     mapping of the verticies to faces  
    int[][] fv ={{0,0,0,0},{1,2,3,4},{1,6,7,4},{2,5,8,3},{6,5,8,7},{1,6,5,2},{4,7,8,3}};

// Create the shape group
    brain = createShape(GROUP);
    for (int i = -cells_per_side/2; i < cells_per_side/2+1; i++) {
      for (int j = -cells_per_side/2; j < cells_per_side/2+1; j++) {
          for (int k = -cells_per_side/2; k < cells_per_side/2+1; k++) {         
            cell = createShape(GROUP);
               for (int fn = 0; fn < 7; fn++) {
               face[fn]=createShape();
               face[fn].beginShape();
                  face[fn].fill(quadBG[fn]);
                  face[fn].vertex(vertices[fv[fn][0]].x, vertices[fv[fn][0]].y, vertices[fv[fn][0]].z);
                  face[fn].vertex(vertices[fv[fn][1]].x, vertices[fv[fn][1]].y, vertices[fv[fn][1]].z);
                  face[fn].vertex(vertices[fv[fn][2]].x, vertices[fv[fn][2]].y, vertices[fv[fn][2]].z);
                  face[fn].vertex(vertices[fv[fn][3]].x, vertices[fv[fn][3]].y, vertices[fv[fn][3]].z);
                  face[fn].setName("Face " + fn);
                  face[fn].endShape();
                  cell.addChild(face[fn]);     }

            cell.translate(i*gridsize, j*gridsize, k*gridsize);
            name=("Cell "+i+" "+j+" "+k);
            cell.setName(name);
//          cell.setOrientation(up)
            brain.addChild(cell);
            }}}}
Tagged:

Answers

  • edited April 2014

    I apologize if attributes is the wrong term.

    It's the general OOP term. But officially Java calls them fields! :-B In Java, variables can be either field or local.
    Fields are declared within the enclosing curly braces of a class, in its 1st outer scope.
    Otherwise, for inner scopes, declared variables are local!

    And that's the problem in your class definition: position, location & orientation aren't fields,
    but local variables of the constructor's enclosing scope! :O

  • I'm not sure what your question is. What problem does your code have? You might consider posting an MCVE instead of all of that extra code.

  • Answer ✓

    I have taken the liberty of recoding your class. Hopefully this makes sense when read alongside @GoToLoop's explanation.

    //----------------------------------------------------------
    // Extended PShape
    class MyPShape extends PShape {
      // Fields or attributes of this class
      PVector position;      // Initial Positioning in row, column, file
      PVector location;      // cell location in x,y,z
      PVector orientation;   // cell orientation vestor in x,y,z
    
      MyPShape( ) {
        super( ); // Call the constructor PShape();
        // Create vectors for fields for safe use with detters
        position = new PVector();
        location = new PVector();
        orientation = new PVector();
      } 
    
      public void setOrientation(PVector thisorientation) {
        orientation = thisorientation;
      }
    
      public PVector getOrientation() {
        return orientation;
      }
    
      public void setLocation(PVector thislocation) {
        location = thislocation;
      }
    
      public PVector getLocation() {
        return location;
      }
    
      public void setPostition(PVector thisposition) {
        position = thisposition;
      }
    
      public PVector getPostition() {
        return position;
      }
    }
    
  • edited April 2014 Answer ✓

    My own take on it: :ar!

    // forum.processing.org/two/discussion/4577/extending-a-class
    
    final class MyPShape extends PShape {
      // Fields or attributes of this class:
      final PVector position    = new PVector();
      final PVector location    = new PVector();
      final PVector orientation = new PVector();
    
      // Methods of this class:
      MyPShape setPostition(PVector pos) {
        position.set(pos);
        return this;
      }
    
      PVector getPostition() {
        return position;
      }
    
      MyPShape setLocation(PVector loc) {
        location.set(loc);
        return this;
      }
    
      PVector getLocation() {
        return location;
      }
    
      MyPShape setOrientation(PVector orient) {
        orientation.set(orient);
        return this;
      }
    
      PVector getOrientation() {
        return orientation;
      }
    }
    
  • Thank you both for great help, it is appreciated. I will try working this into my main code and go from there.

Sign In or Register to comment.