My processing code is pretty bad, but it's supposed to add and subtract images from the screen, when you click on them. What isn't working now, is how Organism doesn't seem to work with it's subclasses, and how before I did this, the draw() method wouldn't update the "screen" when I take away members. Thanks for any help.
PImage shark;
PImage seaturtle;
PImage fish;
PImage shark1;
PImage b;
ArrayList<shark> SharkObjs = new ArrayList<shark>();
ArrayList<seaturtle> seaturtleObjs = new ArrayList<seaturtle>();
ArrayList<fish> fishObjs = new ArrayList<fish>();
ArrayList<phytoplankton> phyObjs = new ArrayList<phytoplankton>();
ArrayList<zooplankton> zooObjs = new ArrayList<zooplankton>();
void setup() {
size(800, 600);
b = loadImage("oceanBackGround.jpg");
b.resize(800, 600);
background(250);
fill(0, 127, 127);
text("Press [R] to reset the food web.");
seaturtleObjs.add(new seaturtle(100, 200));
fishObjs.add(new fish(400, 300));
phyObjs.add(new phytoplankton(500, 500));
zooObjs.add(new zooplankton(400, 500));
//populateSea();
}
void draw() {
background(b);
for ( shark s : SharkObjs ) {
s.render();
}
}
void mouseClicked() {
int MyX = mouseX;
int MyY = mouseY;
ArrayList<Organism> all = new ArrayList<Organism>();
all = SharkObjs + seaturtleObjs + fishObjs + phyObjs + zooObjs;
for ( Organism o : all) {
if (didIClick(o, MyX, MyY)) {
if ( o == shark ) {
System.out.println("holy shit this worked");
}
}
}
}
void sharkMethod(int b) {
int hitCounter = 0;
//System.out.println("There are this many sharks: " + SharkObjs.size());
if (mouseButton == LEFT || b == 0) {
//add members
for (int w = (SharkObjs.size() * 100) + 345, l = 35 , c = 0; c < 1 ; w+=100, c++) {
int x = SharkObjs.size();
while (w >= 700) {
int y = x;
l +=75;
w = (x-y) * 100 + 45;
x = 0;
}
SharkObjs.add(new shark(w, l));
turtleMethod(1);
}
}
else if (mouseButton == RIGHT || b == 1) {
ArrayList<shark> tempSharks = new ArrayList<shark>();
SharkObjs = tempSharks;
turtleMethod(0);
//System.out.println("There are this many sharks: " + SharkObjs.size());
}
}
////////////////////////
void turtleMethod(int b) {
int hitCounter = 0;
if (mouseButton == LEFT || b == 0) {
//add members
hitCounter++;
for (int w = 125, l = 210, counter = 0; counter< hitCounter ; w+=80, l+=70, counter++) {
seaturtleObjs.add(new seaturtle(w, l));
fishMethod(1);
}
}
else if (mouseButton == RIGHT || b == 1) {
for (int x = 0; x<3; x++ ) {
seaturtleObjs.remove(0);
fishMethod(0);
}
}
}
////////////////////////
void fishMethod(int b) {
int hitCounter = 0;
if (mouseButton == LEFT || b == 0) {
//add members
hitCounter++;
for (int w = 550, l = 310, counter = 0; counter< hitCounter ; w+=50, l+=50, counter++) {
fishObjs.add(new fish(w, l));
}
}
else if (mouseButton == RIGHT || b == 1) {
for (int x = 0; x < 4 ; x++) {
fishObjs.remove(x);
}
}
}
////////////////////////
void phyMethod(int b) {
int hitCounter = 0;
if (mouseButton == LEFT || b == 0) {
//add members
hitCounter++;
for (int w = 550, l = 310, counter = 0; counter< hitCounter ; w+=20, l+=20, counter++) {
phyObjs.add(new phytoplankton(w, l));
}
}
else if (mouseButton == RIGHT || b == 1) {
for (int x = 0; x < 4 ; x++) {
phyObjs.remove(x);
}
}
}
////////////////////////
void zooMethod(int b) {
int hitCounter = 0;
if (mouseButton == LEFT || b == 0) {
//add members
hitCounter++;
for (int w = 400, l = 550, counter = 0; counter< hitCounter ; w+=10, l+=10, counter++) {
zooObjs.add(new zooplankton(w, l));
}
}
else if (mouseButton == RIGHT || b == 1) {
for (int x = 0; x < 4 ; x++) {
zooObjs.remove(x);
}
}
}
////////////////////////
////////////////////////
////////////////////////
void mousePressed() {
int hitCounter = 0;
if (mouseButton == LEFT) {
//add members
hitCounter++;
for (int w = 325, l = 45, counter = 0; counter< hitCounter ; w+=5, l+=5, counter++) {
sharkMethod(0);
}
}
else if (mouseButton == RIGHT) {
sharkMethod(1);
}
}
/////////////////////////////////////////////////////////////////////////////////////////////
public class Checker {
int newX;
int newY;
boolean didIClick(Organism o, int mX, int mY) {
newX = o.getWidth() + o.getX();
newY = o.getLength() + o.getY();
if ((mX >= o.getWidth() && mX<= newX) && (mY >= o.getLength() && mY <= newY)) {
return true;
}
return false;
}
//////////////////////////////////////////////////////////////////////////////////////////////
class Organism {
int widthx;
int lengthx;
int locX;
int locY;
public int getWidth() {
return widthx;
}
public int getLength() {
return lengthx;
}
public int getX() {
return locX;
}
public int getY() {
return locY;
}
}
////////////////////////
class shark extends Organism {
int locX = 100;
int locY = 75;
public shark(int w, int l) {
widthx = w;
lengthx = l;
shark = loadImage("shark.gif");
shark.resize(locX, locY);
}
public String myPosition() {
String myPos = "my width is at "+ this.getWidth() + " my length is at " + this.getLength();
return myPos;
}
void render() {
image(shark, widthx, lengthx );
}
}
////////////////////////
class seaturtle extends Organism {
int widthx;
int lengthx;
int locX = 80;
int locY = 70;
public seaturtle(int w, int l) {
widthx = w;
lengthx = l;
seaturtle = loadImage("seaturtle.jpg");
seaturtle.resize(80, 70);
}
public seaturtle() {
widthx = 100;
lengthx = 200;
seaturtle = loadImage("seaturtle.jpg");
seaturtle.resize(80, 70);
}
void render() {
image(seaturtle, widthx, lengthx );
}
}
////////////////////////
class fish extends Organism {
int widthx;
int lengthx;
int locX = 50;
int locY = 50;
public fish(int w, int l) {
widthx = w;
lengthx = l;
fish = loadImage("fish.jpg");
fish.resize(50, 50);
}
public fish() {
widthx = 400;
lengthx = 300;
fish = loadImage("fish.jpg");
fish.resize(50, 50);
}
void render() {
image(fish, widthx, lengthx);
}
}
////////////////////////
class phytoplankton extends Organism {
int x;
int y;
int locX = 20;
int locY = 20;
public phytoplankton(int xCord, int yCord) {
x = xCord;
y = yCord;
}
public phytoplankton() {
x = 500;
y = 500;
}
void render() {
rect(x, y, locX, locY);
fill(0, 255, 5);
}
}
////////////////////////
class zooplankton extends Organism {
int x;
int y;
int locx = 10;
int locY = 10;
public zooplankton(int xCord, int yCord) {
x = xCord;
y = yCord;
}
public zooplankton() {
x = 400;
y = 550;
}
void render() {
ellipse(x, y, locX, locY);
fill(255, 0, 5);
}
}
////////////////////////
void keyPressed() {
if ( key == 'R' || key == 'r' ) {
for (int i = SharkObjs.size()-1 ; i>0 ; i-- ) {
SharkObjs.remove(i);
}
for (int i = seaturtleObjs.size()-1 ; i>0 ; i--) {
seaturtleObjs.remove(i);
}
for (int i = fishObjs.size()-1 ; i>0 ; i-- ) {
fishObjs.remove(i);
}
setup();
}
}}
PImage shark;
PImage seaturtle;
PImage fish;
PImage shark1;
PImage b;
ArrayList<shark> SharkObjs = new ArrayList<shark>();
ArrayList<seaturtle> seaturtleObjs = new ArrayList<seaturtle>();
ArrayList<fish> fishObjs = new ArrayList<fish>();
ArrayList<phytoplankton> phyObjs = new ArrayList<phytoplankton>();
ArrayList<zooplankton> zooObjs = new ArrayList<zooplankton>();
void setup() {
size(800, 600);
b = loadImage("oceanBackGround.jpg");
b.resize(800, 600);
background(250);
fill(0, 127, 127);
text("Press [R] to reset the food web.");
seaturtleObjs.add(new seaturtle(100, 200));
fishObjs.add(new fish(400, 300));
phyObjs.add(new phytoplankton(500, 500));
zooObjs.add(new zooplankton(400, 500));
//populateSea();
}
void draw() {
background(b);
for ( shark s : SharkObjs ) {
s.render();
}
}
void mouseClicked() {
int MyX = mouseX;
int MyY = mouseY;
ArrayList<Organism> all = new ArrayList<Organism>();
all = SharkObjs + seaturtleObjs + fishObjs + phyObjs + zooObjs;
for ( Organism o : all) {
if (didIClick(o, MyX, MyY)) {
if ( o == shark ) {
System.out.println("holy shit this worked");
}
}
}
}
void sharkMethod(int b) {
int hitCounter = 0;
//System.out.println("There are this many sharks: " + SharkObjs.size());
if (mouseButton == LEFT || b == 0) {
//add members
for (int w = (SharkObjs.size() * 100) + 345, l = 35 , c = 0; c < 1 ; w+=100, c++) {
int x = SharkObjs.size();
while (w >= 700) {
int y = x;
l +=75;
w = (x-y) * 100 + 45;
x = 0;
}
SharkObjs.add(new shark(w, l));
turtleMethod(1);
}
}
else if (mouseButton == RIGHT || b == 1) {
ArrayList<shark> tempSharks = new ArrayList<shark>();
SharkObjs = tempSharks;
turtleMethod(0);
//System.out.println("There are this many sharks: " + SharkObjs.size());
}
}
////////////////////////
void turtleMethod(int b) {
int hitCounter = 0;
if (mouseButton == LEFT || b == 0) {
//add members
hitCounter++;
for (int w = 125, l = 210, counter = 0; counter< hitCounter ; w+=80, l+=70, counter++) {
seaturtleObjs.add(new seaturtle(w, l));
fishMethod(1);
}
}
else if (mouseButton == RIGHT || b == 1) {
for (int x = 0; x<3; x++ ) {
seaturtleObjs.remove(0);
fishMethod(0);
}
}
}
////////////////////////
void fishMethod(int b) {
int hitCounter = 0;
if (mouseButton == LEFT || b == 0) {
//add members
hitCounter++;
for (int w = 550, l = 310, counter = 0; counter< hitCounter ; w+=50, l+=50, counter++) {
fishObjs.add(new fish(w, l));
}
}
else if (mouseButton == RIGHT || b == 1) {
for (int x = 0; x < 4 ; x++) {
fishObjs.remove(x);
}
}
}
////////////////////////
void phyMethod(int b) {
int hitCounter = 0;
if (mouseButton == LEFT || b == 0) {
//add members
hitCounter++;
for (int w = 550, l = 310, counter = 0; counter< hitCounter ; w+=20, l+=20, counter++) {
phyObjs.add(new phytoplankton(w, l));
}
}
else if (mouseButton == RIGHT || b == 1) {
for (int x = 0; x < 4 ; x++) {
phyObjs.remove(x);
}
}
}
////////////////////////
void zooMethod(int b) {
int hitCounter = 0;
if (mouseButton == LEFT || b == 0) {
//add members
hitCounter++;
for (int w = 400, l = 550, counter = 0; counter< hitCounter ; w+=10, l+=10, counter++) {
zooObjs.add(new zooplankton(w, l));
}
}
else if (mouseButton == RIGHT || b == 1) {
for (int x = 0; x < 4 ; x++) {
zooObjs.remove(x);
}
}
}
////////////////////////
////////////////////////
////////////////////////
void mousePressed() {
int hitCounter = 0;
if (mouseButton == LEFT) {
//add members
hitCounter++;
for (int w = 325, l = 45, counter = 0; counter< hitCounter ; w+=5, l+=5, counter++) {
sharkMethod(0);
}
}
else if (mouseButton == RIGHT) {
sharkMethod(1);
}
}
/////////////////////////////////////////////////////////////////////////////////////////////
public class Checker {
int newX;
int newY;
boolean didIClick(Organism o, int mX, int mY) {
newX = o.getWidth() + o.getX();
newY = o.getLength() + o.getY();
if ((mX >= o.getWidth() && mX<= newX) && (mY >= o.getLength() && mY <= newY)) {
return true;
}
return false;
}
//////////////////////////////////////////////////////////////////////////////////////////////
class Organism {
int widthx;
int lengthx;
int locX;
int locY;
public int getWidth() {
return widthx;
}
public int getLength() {
return lengthx;
}
public int getX() {
return locX;
}
public int getY() {
return locY;
}
}
////////////////////////
class shark extends Organism {
int locX = 100;
int locY = 75;
public shark(int w, int l) {
widthx = w;
lengthx = l;
shark = loadImage("shark.gif");
shark.resize(locX, locY);
}
public String myPosition() {
String myPos = "my width is at "+ this.getWidth() + " my length is at " + this.getLength();
return myPos;
}
void render() {
image(shark, widthx, lengthx );
}
}
////////////////////////
class seaturtle extends Organism {
int widthx;
int lengthx;
int locX = 80;
int locY = 70;
public seaturtle(int w, int l) {
widthx = w;
lengthx = l;
seaturtle = loadImage("seaturtle.jpg");
seaturtle.resize(80, 70);
}
public seaturtle() {
widthx = 100;
lengthx = 200;
seaturtle = loadImage("seaturtle.jpg");
seaturtle.resize(80, 70);
}
void render() {
image(seaturtle, widthx, lengthx );
}
}
////////////////////////
class fish extends Organism {
int widthx;
int lengthx;
int locX = 50;
int locY = 50;
public fish(int w, int l) {
widthx = w;
lengthx = l;
fish = loadImage("fish.jpg");
fish.resize(50, 50);
}
public fish() {
widthx = 400;
lengthx = 300;
fish = loadImage("fish.jpg");
fish.resize(50, 50);
}
void render() {
image(fish, widthx, lengthx);
}
}
////////////////////////
class phytoplankton extends Organism {
int x;
int y;
int locX = 20;
int locY = 20;
public phytoplankton(int xCord, int yCord) {
x = xCord;
y = yCord;
}
public phytoplankton() {
x = 500;
y = 500;
}
void render() {
rect(x, y, locX, locY);
fill(0, 255, 5);
}
}
////////////////////////
class zooplankton extends Organism {
int x;
int y;
int locx = 10;
int locY = 10;
public zooplankton(int xCord, int yCord) {
x = xCord;
y = yCord;
}
public zooplankton() {
x = 400;
y = 550;
}
void render() {
ellipse(x, y, locX, locY);
fill(255, 0, 5);
}
}
////////////////////////
void keyPressed() {
if ( key == 'R' || key == 'r' ) {
for (int i = SharkObjs.size()-1 ; i>0 ; i-- ) {
SharkObjs.remove(i);
}
for (int i = seaturtleObjs.size()-1 ; i>0 ; i--) {
seaturtleObjs.remove(i);
}
for (int i = fishObjs.size()-1 ; i>0 ; i-- ) {
fishObjs.remove(i);
}
setup();
}
}}
1