Home > Software design >  Cannot find a class or type named “Circle”
Cannot find a class or type named “Circle”

Time:04-14

I'm not sure why "Circle" isn't being recognized in my code. The error that shows up in Processing says "Cannot find a class or type named “Circle”" I am trying to take values that are taken from a sensor (through Arduino) & visualize them in Processing. From what I saw, I thought adding "import java.util.*" would fix the problem, but it didn't. I'm not sure if the term "Circle" is just too broad, & if I have to make it "MCircle" or "PCircle" or something like that.

Here is the Processing code:

import processing.serial.*;
import java.util.*;

Serial myPort; 
int val;    
int amt = 10;
Circle [] circles;

void setup() {
  size(500, 500);
  rectMode(CENTER);
  circles = new Circle[amt];
  for (int i=0; i<10; i  ) {
    circles[i] = new Circle();
  }


  String portName = Serial.list()[2];
  myPort = new Serial(this, portName, 9600);
  // println(Serial.list());
  //println(val);
}

void draw() {
  //if your port is available, get the val
  if ( myPort.available() > 2) {
    val = myPort.read();
  }

  background(0);
  for (int i = 0; i<10; i  ) {
    circles[i].display();
  }
  class Circle
  {
    float xPos;
    float yPos;

    Circle() {
      xPos = random(width);
      yPos = random(height);
    }
  }
}

void display() {
  // ellipse(random(width), random(height), val, val);
  ellipse(xPos, yPos, val, val);
}

CodePudding user response:

Circle is declared in scope of draw. You have to declare it in global scope:

void draw() {
    //if your port is available, get the val
    if ( myPort.available() > 2) {
        val = myPort.read();
    }

    background(0);
    for (int i = 0; i<10; i  ) {
        circles[i].display();
    }

    /* DELETE
    class Circle
    {
      float xPos;
      float yPos;

      Circle() {
        xPos = random(width);
        yPos = random(height);
      }
    }*/
}

// INSERT
class Circle {
    float xPos;
    float yPos;

    Circle() {
        xPos = random(width);
        yPos = random(height);
    }
}

CodePudding user response:

A few things:

  1. I would recommend putting the class declaration of Circle in its own file. One class to a file, generally. File named the same as the class, and import it into your processing class.
  2. Similar to #1, your processing code should probably be in a class.
  3. Good security practice is to avoid accessing xPos and yPos directly from outside the Circle class - it's better to use getter methods and make the class attributes private.
  • Related