Objects don't appear

Hello, I’ve been working on this code and I can’t seem to find out what the problem is. You have Buttons at the bottom and depending on which one you press you can do different things. I am currently working on the Lawn and Flowers to be added by mouse clicks. But they just don’t appear :confused:
The Objects are in separate classes which extend from the same class called “GardenObject”. They both just hold the draw function for each Object.
Here are the important parts of my code:

ArrayList gardenObject;
public enum GameState {
LAWN, FLOWER, REMOVE, MOVING
}
GameState state;
void setup () {
size (700, 600);
gardenObject = new ArrayList();
this.state = GameState.FLOWER;
}
void draw () {
background(255);
for (int i = 0; i<gardenObject.size(); i++) {
gardenObject.get(i);
}
//Reihenfolge der GameStates
switch(state) {
case FLOWER:
drawGame();
break;
case LAWN:
drawGame();
break;
case REMOVE:
drawGame();
break;
case MOVING:
drawGame();
break;
}
}

void mouseClicked () {
if (this.state == GameState.FLOWER) {
gardenObject.add(new Flower(mouseX, mouseY));
}
if (this.state == GameState.LAWN) {
gardenObject.add(new Lawn(mouseX, mouseY));
}
}

Can you please show me this code?

Why is it the same for every case?

Why does the for loop close so early?

Nothing happens there?

this is the drawgame() function but it really just draws the buttons and stuff, I tried putting the mouseClicked loops in there but couldn’t figure it out

noFill();
rect (70, 500, 100, 50);
rect (220, 500, 100, 50);
rect (370, 500, 100, 50);
rect (520, 500, 100, 50);
fill(0);
textSize(20);
text(“FLOWER”, 90, 530);
text(“LAWN”, 240, 530);
text(“MOVE”, 390, 530);
text(“REMOVE”, 540, 530);
}

1 Like

Did you write this?
Or is it from your teacher to fill in the missing parts?

Also the classes are missing ? Class Flower, class Lawn

Is this the entire code you have for this?

Sorry

I misread your post

1 Like

Yes I wrote it all myself. I didn’t put the classes in here cause I thought they aren’t important for this part, cause they just have the draw function of the flower etc. And for Loop I didn’t quite know where to place it.
Here’s the full code:

ArrayList gardenObject;
public enum GameState {
LAWN, FLOWER, REMOVE, MOVING
}
GameState state;
void setup () {
size (700, 600);
gardenObject = new ArrayList();
this.state = GameState.FLOWER;
}
void draw () {
background(255);
for (int i = 0; i<gardenObject.size(); i++) {
gardenObject.get(i);
}
//Reihenfolge der GameStates
switch(state) {
case FLOWER:
drawGame();
break;
case LAWN:
drawGame();
break;
case REMOVE:
drawGame();
break;
case MOVING:
drawGame();
break;
}
}
void switchState (GameState newState) {
this.state = newState;
}
void drawGame () {
noFill();
rect (70, 500, 100, 50);
rect (220, 500, 100, 50);
rect (370, 500, 100, 50);
rect (520, 500, 100, 50);
fill(0);
textSize(20);
text(“FLOWER”, 90, 530);
text(“LAWN”, 240, 530);
text(“MOVE”, 390, 530);
text(“REMOVE”, 540, 530);
}
void mousePressed() {
switch(state) {
case FLOWER:
//Listening for mousePressed
if (mouseX >= 70 && mouseX <= 170 &&
mouseY >= 500 && mouseY <= 550) {//condition for mouse to be inside 1st button
switchState(GameState.FLOWER);//action for 1st button
}
case LAWN:
if (mouseX >= 220 && mouseX <= 320 &&
mouseY >= 500 && mouseY <= 550) {//condition for mouse to be inside 2nd button
switchState(GameState.LAWN);//action for 2nd button
}
case MOVING:
if (mouseX >= 370 && mouseX <= 470 &&
mouseY >= 500 && mouseY <= 550) {
switchState(GameState.MOVING);
}
case REMOVE:
if (mouseX >= 520 && mouseX <=620 &&
mouseY >= 500 && mouseY <= 550) {
switchState(GameState.REMOVE);
}
}
}
void mouseClicked () {
if (this.state == GameState.FLOWER) {
gardenObject.add(new Flower(mouseX, mouseY));
}
if (this.state == GameState.LAWN) {
gardenObject.add(new Lawn(mouseX, mouseY));
}
}

public class Flower extends GardenObject{
Flower(float thisx, float thisy) {
x = thisx;
y = thisy;
}
void draw () {
translate(mouseX - 50, mouseY - 50);
scale(0.2);
beginShape();
//stem
fill(44, 191, 83); //make it green
rect(240, 250, 20, 220); //make tall rectangle in center
//petal 1 (bottom left)
fill(random(250), random(250), random(250));
ellipse(175, 250, 150, 150);
//petal 2 (bottom right)
ellipse(325, 250, 150, 150);
//petal 4 (top left)
ellipse(175, 130, 150, 150);
//petal 5 (top right)
ellipse(325, 130, 150, 150);
//petal 3 (bottom)
fill(random(250), random(250), random(250));
ellipse(250, 275, 150, 150);
//petal 6 (top)
ellipse(250, 100, 150, 150);
//petal 7 (left)
ellipse(150, 180, 150, 150);
//petal 8 (right)
ellipse(350, 180, 150, 150);
//center of flower
fill(255, 218, 94); //fill in yellow
ellipse(250, 200, 80, 80); //make circle in the middle
endShape();
}
}

public class GardenObject {
float x;
float y;
void drawGardenObject () {
}
void clicked () {
}
}

public class Lawn extends GardenObject {
Lawn(float thisx, float thisy) {
x = thisx;
y = thisy;
}
void draw () {
fill(70, 200, 80);
rect(mouseX - 50,mouseY - 50,100,100);
}
}

I am not finished yet though, but as I said kinda stuck on how to make the different objects appear depending on which state your on. :slight_smile:

1 Like

Yeah

In my opinion the state are only there to have different mouse actions such as plant grass, flower etc.

You need a display function in the class and call it from the for loop

1 Like

Better

ArrayList<GardenObject> gardenObject;

1 Like

GardenObject gobj= gardenObject.get(i);
gobj.display();

inside the for loop

1 Like

actually have it like this but I think it got lost while formatting but thank you ill definitely try to add a display function. I did not think of that :slight_smile:

1 Like

Yeah the display function in the class is important

It uses the x,y data etc.

I just looked at your code and I think your method must ALWAYS be named
drawGardenObject and not “draw” or “display”.

Because it must be called the same way as it is called in class GardenObject

mouseX,mouseY inside the class

Also, since you want to draw multiple flowers independent of each other, don’t use mouseX,mouseY inside the class. Instead use x,y. Remember, you push mouseX,mouseY inside the class where it becomes x,y (and is stored as an information inside the object, so it’s now independent of the mouse. That’s what the constructor is for: passing data in the new object when you make it).

mousePressed()

I don’t think you want a switch() in mousePressed().
Just evaluate the buttons. You want every button be clickable every time, not only in a certain state. It works most times, but the switch is not needed. (it only works mostly because you forgot the break; after each case section. So it doesn’t work in state REMOVE.)

Flower

You could consider using pushMatrix and popMatrix around the flower drawing because the usage of scale and translate. pushMatrix and popMatrix isolate the part from the rest of the program.

Also consider storing the random colors initially. How?