Ok so I have this program which creates a
NanOrg
object that acts as a moving ball that bounces off the walls and other NanOrgs
`public class NanOrg extends Ellipse2D.Float{
public int x_speed, y_speed;
private int d;
private int width = MainWindow.WIDTH;
private int height = MainWindow.HEIGHT;
private ArrayList<NanOrg> nanorgs;
Rectangle2D r = new Rectangle2D.Float(super.x, super.y, d, d);
public NanOrg(int diameter, ArrayList<NanOrg> nanorgs){
super((int) (Math.random() * (MainWindow.WIDTH - 20) + 1), (int) (Math.random() * (MainWindow.HEIGHT - 20) + 1), diameter, diameter);
this.d = diameter;
this.x_speed = (int) (Math.random() * 5 + 1);
this.y_speed = (int) (Math.random() * 5 + 1);
this.nanorgs = nanorgs;
}
public void move(){
for(NanOrg nanorg : nanorgs){
if(nanorg != this && nanorg.intersects(r)){
int tempx = x_speed;
int tempy = y_speed;
x_speed = nanorg.x_speed;
y_speed = nanorg.y_speed;
nanorg.x_speed = tempx;
nanorg.y_speed = tempy;
break;
}
}
if(super.x < 0){
super.x = 0;
x_speed = Math.abs(x_speed);
}
else if(super.x > width - d){
super.x = width - d;
x_speed = -Math.abs(x_speed);
}
if(super.y < 0){
super.y = 0;
y_speed = Math.abs(y_speed);
}
else if(super.y > height - d){
super.y = height - d;
y_speed = -Math.abs(y_speed);
}
super.x += x_speed;
super.y += y_speed;
}
}` and a
Oil
object which represents unmoving droplets of oil.
public class Oil extends Ellipse2D.Float {
private int width = MainWindow.WIDTH;
private int height = MainWindow.HEIGHT;
private int d;
private ArrayList<Oil> oils;
Rectangle2D rect = new Rectangle2D.Float(super.x, super.y, d, d);
public Oil(int diameter){
super((int) (Math.random() * (MainWindow.WIDTH - 20) + 1), (int) (Math.random() * (MainWindow.HEIGHT - 20) + 1), diameter, diameter);
this.d = diameter;
}
}
The NanOrgs are spawn at a random location and move in a random direction and speed. The NanOrg's are able to bounce of eachother, but I need help figuring out how to deal with the collision between the Oil and NanOrg, specifically so the NanOrg can "eat" the oil by removing it. I have two other Classes:
MainWindow and PaintSurface
The MainWindow Class creates the window as a JApplet.
`public class MainWindow extends JApplet{
public static final int WIDTH = 500;
public static final int HEIGHT = 500;
private PaintSurface canvas;
public void init(){
this.setSize(WIDTH, HEIGHT);
this.setName("NanOrg Program");
canvas = new PaintSurface();
this.add(canvas, BorderLayout.CENTER);
Timer t = new Timer(20, e -> {canvas.repaint();});
t.start();
}
}
The PaintSurface Class "draws" the Oil and NanOrgs
public class PaintSurface extends JComponent{
public ArrayList<NanOrg> nanorgs = new ArrayList<NanOrg>();
public ArrayList<Oil> oils = new ArrayList<Oil>();
public PaintSurface(){
for(int i = 0; i < 5; i++)
nanorgs.add(new NanOrg(10, nanorgs));
for(int o = 0; o < 30; o++)
oils.add(new Oil(10));
}
public void paint(Graphics g){
Graphics2D g2 = (Graphics2D) g;
g2.setColor(Color.MAGENTA);
for(NanOrg nanorg : nanorgs){
nanorg.move();
g2.fill(nanorg);
}
for(Oil oil : oils){
g2.setColor(Color.BLACK);
g2.fill(oil);
}
}
}`
That's my program, so just to recap: I have created it so the NanOrgs bounce off each other and the walls, but I need help on what I need to code for the NanOrg to "eat" the Oil(e.g remove it) and where to place the code that removes the Oil. I have tried
public void eatOil(){
for(Oil oil : oils){
if(oil.intersects(r)){
oils.remove(oil);
}
}
}
in the NanOrg Class and then called the method in my PaintSurface class, but when I ran it, I got the NullPointerException. Then I tried doing that same code in the Oil and PaintSurface Class, but I couldn't get either of the two classes to use "r" as the variable defined in NanOrg. So if you could just help me figure out what to do that would be great or tell me what to do if i'm doing something wrong. If you have questions to try and clarify ypur knowledge please ask and i'll answer to the best of my abilities.