import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
import java.util.List;
/**
* Write a description of class MyWorld here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Ocean extends World
{
private int counter=39;
private int counter2=650;
private int counter3=0;
private int ident=0;
private Oxygen[] oxygen=new Oxygen[50];
/**
* Constructor for objects of class MyWorld.
*
*/
public Ocean()
{
// Create a new world with 600x400 cells with a cell size of 1x1 pixels.
super(640, 600, 1,false);
getBackground().setColor(new Color(0,186,255));
getBackground().fillRect(0,(getHeight()/3)+13,getWidth(),getHeight());
getBackground().setColor(new Color(133,228,224));
getBackground().fillRect(0,0,getWidth(),(getHeight()/3)-12);
setPaintOrder(Dolphin.class);
drawBackgroundPics();
setup();
}
/**
* The main method.
*/
public void act()
{
oxygen();
addWave();
addCloud();
}
/**
* Set up the beginning actors.
*/
private void setup()
{
addObject(new Cloud(),150,120);
addObject(new Cloud(),500,55);
for(int i=40;i<getWidth();i+=80)
{
addObject(new Wave(-2),i,getHeight()/3);
}
for(int i=0;i<50;i++)
{
addObject(oxygen[i],501+(i*2),566);
}
}
/**
* Add a wave object in certain increments.
*/
private void addWave()
{
counter++;
if(counter==40)
{
counter=0;
addObject(new Wave(-2),getWidth()+40,getHeight()/3);
}
}
/**
* Add a cloud object in certain increments.
*/
private void addCloud()
{
counter2+=5;
if(counter2==700)
{
addObject(new Cloud(),850,120);
counter2=0;
}
if(counter2==350)addObject(new Cloud(),850,55);
}
/**
* Draw backgound pictures.
*/
private void drawBackgroundPics()
{
for(int i=0;i<665;i+=166)
{
getBackground().drawImage(new GreenfootImage("Mud.png"),i,getHeight()-84);
}
getBackground().drawImage(new GreenfootImage("rock2.png"),75,getHeight()-100);
getBackground().drawImage(new GreenfootImage("grass.png"),475,getHeight()-115);
getBackground().setColor(new Color(255,255,28));
getBackground().fillOval(-50,-50,100,100);
getBackground().setColor(Color.black);
getBackground().drawRect(500,550,101,31);
}
private void oxygen()
{
// counter3++;
int num=49;
List<Oxygen>oxygen=getObjects(Oxygen.class);
Oxygen ox;
for(int i=0;i<oxygen.size();i++)
{
if(oxygen.get(i).getIdentity()==num)
{
removeObject(oxygen.get(i));
//break ;
}
num--;
}
/*if(counter3==2)
{
for(Oxygen o2:oxygen)
{
if(o2.getIdentity()==ident)
{
removeObject(o2);
ident--;
break;
}
}
counter3=0;
}*/
}
}import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Oxygen here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Oxygen extends Actor
{
private int identifier;
public Oxygen(int num)
{
setImage("Oxygen-bar.png");
identifier=num;
}
public Oxygen()
{
setImage("Oxygen-bar.png");
}
/**
* Act - do whatever the Oxygen wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
// Add your action code here.
}
public int getIdentity()
{
return identifier;
}
}java.lang.NullPointerException at greenfoot.World.addObject(World.java:396) at Ocean.setup(Ocean.java:57) at Ocean.<init>(Ocean.java:31) at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:423) at greenfoot.core.Simulation.newInstance(Simulation.java:617) at greenfoot.platforms.ide.WorldHandlerDelegateIDE$3.run(WorldHandlerDelegateIDE.java:425) at greenfoot.core.Simulation.runQueuedTasks(Simulation.java:502) at greenfoot.core.Simulation.maybePause(Simulation.java:305) at greenfoot.core.Simulation.runContent(Simulation.java:218) at greenfoot.core.Simulation.run(Simulation.java:211)

