I am trying to add a ‘ball’ entity with my mouse coordinates within my mouse class, however whenever I try to make a non-static reference in order to ‘add’ the ball it won’t increase the entity size (not adding on the screen).
Mouse.java:
public void mousePressed(MouseEvent e) {
s.addEntity(new Ball(e.getX(), e.getY(), 15, 15));
}
Ball.java:
import java.awt.Color;
import java.awt.Graphics;
public class Ball extends Entity{
private int x, y, width, height;
private boolean removed;
public Ball(int x, int y, int width, int height){
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
public void render(Graphics g){
if(!removed){
g.setColor(Color.WHITE);
g.drawRect(x, y, width, height);
}
}
public void remove(){
removed = true;
}
}
Screen:
import java.awt.Color;
import java.awt.Graphics;
import java.util.*;
public class Screen {
public List<Entity> entities = new ArrayList<Entity>();
public void render(Graphics g){
System.out.println(entities.size());
for(int i = 0; i < entities.size(); i++){
entities.get(i).render(g);
}
}
public void addEntity(Entity e){
System.out.println("ADDED" + e);
entities.add(e);
}
}
Making entities static works, but there must be another way around this?