(Old Post) I'm making a program that will simulate viruses attacking cells, then reproducing. The problem is, I made an entity class, then 3 classes that extend it: FoodEntity, CellEntity, and VirusEntity. When I try to use my addEntity(entity) method, it only add the first one, and doesn't show the others. I added some debugging lines, and it tells me that everything is all set up and being sent to the draw method, and that everything occupies different x and y coordinates, but nothing is being draw except the first thing. I added a link to a zip file containing all of the source code, and it will compile.
Thanks.
Edit: Working on a smaller program that replicates the issue. Feel free to look at the main one, though.
Edit2: Here it is:
Code:import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;
import java.util.ArrayList;
import java.util.Random;
import javax.swing.JFrame;
public class Main {
public static JFrame win=new JFrame();
public static BufferStrategy strategy;
private static ArrayList entities=new ArrayList();
private static Random rnd=new Random();
public static void main(String[] args){
initWin();
initEntities();
loop();
}
public static void loop(){
strategy=win.getBufferStrategy();
Graphics g=strategy.getDrawGraphics();
g.setColor(Color.white);
g.fillRect(0, 0, 550, 450);
for(int a=0;a<entities.size();a++){
Entity e=(Entity)entities.get(a);
e.logic();
e.move();
e.draw(g);
}
g.dispose();
strategy.show();
try {Thread.sleep(10);} catch (InterruptedException e) {}
}
public static void initWin(){
win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
win.setSize(550,450);
win.setLocationRelativeTo(null);
win.setResizable(false);
win.setIgnoreRepaint(true);
win.setVisible(true);
win.createBufferStrategy(2);
strategy=win.getBufferStrategy();
}
public static void initEntities(){
for(int a=0;a<5;a++){
Entity e=new CircleEntity(rnd.nextInt(500)+20, rnd.nextInt(400)+10);
entities.add(e);
}
}
}
//Next Class:
import java.awt.Color;
import java.awt.Graphics;
public abstract class Entity {
protected double x,y,xm,ym;
protected Color color;
public Entity(double x, double y){
this.x=x;
this.y=y;
}
public void draw(Graphics g){
}
public void move(){
x+=xm;
y+=ym;
}
public void logic(){
}
}
// Last one:
import java.awt.Graphics;
import java.awt.Color;
import java.util.Random;
public class CircleEntity extends Entity{
private Random rnd=new Random();
public CircleEntity(double x, double y) {
super(x, y);
}
public void draw(Graphics g){
g.setColor(Color.black);
g.fillOval((int)x, (int)y, 20, 20);
g.dispose();
}
public void logic(){
xm=rnd.nextInt(3)-1;
ym=rnd.nextInt(3)-1;
}
}
As you can see, the circles don't move, and even though I added 5, there is only one. Any help would be appreciated.
P.S. I kept the original post+files just in case somebody preferred that (which I doubt, but you never know).
P.P.S. The other classes are different files, everything isn't one file(there's 3 individual .java files).
(Old files) https://dl.dropbox.com/u/68488237/Program.zip

