Creating an object from a class


I would like to use the mousePressed function to create new objects from a class at the location of the mouse. But the class itself seems complicated (I’m working on an example I found). When I type:

void mousePressed(){
 Atom w = new Atom();

I get the message ‘‘The constructor HC.Atom() is undefined’’

Here’s the class:

class Atom 
  public PVector position,velocity,acceleration;
  public int type;
  public Atom(int t){
    position = new PVector(random(width),random(height));
    velocity = new PVector(random(-3,3),random(-3,3));
    acceleration = new PVector();
    type = t;
  public void drawAtom()
    switch(type) {
      case WATER: fill(color(0)); break; //Water color
      case HYDROPHOBIC: fill(color(255)); break; //oil color
      default: fill(color(255)); break;
    ellipse(position.x,position.y,max(4,R),max(4,R)); // change shape

The constructor requires one parameter

You have to pass a number to it inside ()

1 Like

…and that constructor is here:

So you either need to create an additional constructor public Atom() { which takes no arguments or you need to call like thiis: Atom w = new Atom(10);

1 Like