The obvious error is that you declare
Object planet;You declare a generic object, and even if you can assign any object to it (as you do in the constructor), Java will only see (at compilation time) the generic object without particular method (other than those common to all objects, like toString()).
Now, somehow you got a good idea to make a generic object (Planet) and specialized one. But, even if using composition instead of inheritance is often recommended, in this case I think you should make your moon to derive from the Planet.
It should be something like (untested, that's just a rough approximation):
Code:class Moon extends Planet {
float theta;
float vel;
int orbitRad;
Moon(float theta_, float vel_, int rad_, int orbitRad_, color c_) {
super(width/2,height/2, rad_, c_);
theta = theta_;
vel = vel_;
orbitRad = orbitRad_;
}
void display(){
// These two lines belong more to move()
xpos += orbitRad * cos(theta);
ypos += orbitRad * sin(theta);
super.display();
}
void move(){
theta += vel;
}
}
Note the
extends which tell to reuse Planet's variables (fields) and functions (methods).
In one case (display()) I reuse Planet's method, in another case (move) I use replace it.
Looking more, perhaps they were different objects for you...
So instead, you should make a generic object common to both Planet and Moon: say, CelestialObject having most of Planet's content, except perhaps the move() method. You can even make the CelestialObject an abstract class, not defining move() but with a default display() method and a generic constructor.
I leave code above as it illustrates some ideas, but here is something more like what you wanted (still untested!):
Code:abstract class CelestialObject {
float xpos;
float ypos;
int rad;
color c;
CelestialObject(float xpos_, float ypos_, int rad_, color c_) {
xpos = xpos_;
ypos = ypos_;
rad = rad_;
c = c_;
}
void display(){
noStroke();
fill(c);
ellipse(xpos,ypos,rad,rad);
textFont (fontA, rad);
text ("x "+ int(xpos),xpos,ypos+(rad*2));
text ("y "+ int(ypos),xpos,ypos+(rad*2)+rad);
}
abstract void move();
}
class Planet extends CelestialObject {
float targetX;
float targetY;
Planet(float xpos_, float ypos_, int rad_, color c_) {
super(xpos_, ypos_, rad_, c_);
}
void move(){
targetX = mouseX;
float dx = targetX - xpos;
if(abs(dx) > 1) {
xpos += dx * easing;
}
targetY = mouseY;
float dy = targetY - ypos;
if(abs(dy) > 1) {
ypos += dy * easing;
}
}
}
class Moon extends CelestialObject {
float theta;
float vel;
int orbitRad;
Planet planet; // This moon orbits around this planet
Moon(float theta_, float vel_, int rad_, int orbitRad_, color c_) {
super(planet.xpos, planet.ypos, rad_, c_);
theta = theta_;
vel = vel_;
orbitRad = orbitRad_;
}
void move(){
xpos = orbitRad * cos(theta) + planet.xpos;
ypos = orbitRad * sin(theta) + planet.ypos;
theta += vel;
}
}