1

this is a very simple question which is confusing me quite a bit since I have done OOP in Java before and never had this issue. Basically when I go to call the class to create the object in question it's just not happening I have no Idea why this won't work, I assuming it might be misuising a bit of processing simplified java.

final color RED = color(255,0,0);
final color BLUE = color(0,0,255);

motorbike bike1;
motorbike bike2;

class motorbike
{
int x = 5;
//members
int y;
int speed=2;
int size=30;
color colour;

void render()
{
  float wheelHeight = size/3;
  fill(colour);
  triangle(x,y,x+size,y,x+size/2,y-size/2);
  //built-in triangle routine
  drawWheel(x,y,wheelHeight);
  drawWheel(x+size,y,wheelHeight);
}

void drawWheel( int x, int y,float size)
{
  float inner = size*2/3;
  fill(0);
  ellipse(x,y,size,size);
  fill(255);
  ellipse(x,y,inner,inner);
}

void move() 
{
  speed= (int)random(5.0);
  //a random step [0..5]
  x=x+speed;
}
void update()  {
  move();
  render();
}

motorbike(int y,color col){
  //constructor
  this.y=y;
  this.speed= (int)random(5.0);
  this.colour=col;
}


//endof class description
}

void setup()
{
  size(500,100);
  bike1 = new motorbike(RED,30);
  bike2 = new motorbike(BLUE,60);
  print("Come on");
}

void draw()
{
  background(125);
}

Sorry for the really dumb question but this should be calling I don't know why it's not drawing the object.

  • can you share full declaration? I need to test and send you feedback and solution – oetoni Nov 09 '17 at 15:06
  • The above is the full code It might look off because it's in processing. – user3469829 Nov 09 '17 at 15:10
  • 1
    Your `draw()` method does nothing but set the background. What do you expect to happen? – John Coleman Nov 09 '17 at 16:40
  • You're never calling any of the functions in your class. In the future, please try to use standard naming conventions (classes and constructors start with upper-case letters, methods and variables start with lower-case letters) and post a [mcve] instead of your whole program. – Kevin Workman Nov 09 '17 at 20:18

1 Answers1

2

Your constructor has the parameters (int, color).

motorbike(int y,color col)

You are passing the parameters in the wrong order (color, int)

bike1 = new motorbike(RED,30);

You also have to call your functions from your recently created object bike1.

bike1.render();

As you are working with Processing, these methods need to be invoked in draw() and perhaps render() once in setup()

Alex Blasco
  • 793
  • 11
  • 22
  • 1
    Yeah I just switched them and it's still not rendering anything. – user3469829 Nov 09 '17 at 14:53
  • Did you call your functions with the object bike1 for example? – Alex Blasco Nov 09 '17 at 14:59
  • Yes it's literally the same as the above code but the variables are the right way around. – user3469829 Nov 09 '17 at 15:04
  • Check my answer again, or check this [example](https://stackoverflow.com/questions/23901420/calling-methods-in-java-hello-world-example) – Alex Blasco Nov 09 '17 at 15:11
  • I understand your answer and I have applied it while calling the object it is still not working. There is more wrong here then me just mixing up the color and int parameters. – user3469829 Nov 09 '17 at 15:20
  • 1
    In *Processing* (which is related to but distinct from Java) it simply isn't true that you need a main method. – John Coleman Nov 09 '17 at 16:36
  • 1
    @AlexBlasco No problem. Processing is a fun language with a gentle learning curve if you already know Java. The comparison isn't exact, but in Processing the function `draw()` is sort of like `main()`. Your remark that OP needs to call the functions is spot-on. You could say a bit more: these methods need to be invoked in `draw()` (and perhaps `render()` once in `setup()`. – John Coleman Nov 09 '17 at 16:53