Currently working on a Genetic Algorithm in Java where I'm using a JPanel to draw my population and my target. The technical code works (generate population, mutate, crossover, get genes, etc...) but I'm stuck with the code to draw all my population.
Currently each individuals have x, y, x velocity ,y velocity and an array of directions ["up", "up", "down", ...] as attributes.
Tried to do:
// Draw the destination square in Red, individuals in Black
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setColor(Color.red);
g2.fill(new Rectangle2D.Double(destinationX(),destinationY(), 10,10));
g2.setColor(Color.black);
// Draw each Individual in black
for (Individual ind : Population)
// read their genes one by one and repaint everytime to see the square move
}
But it's extremely messy, ugly and hard to debug.
How should I go from here? Should all individuals be JPanels that I'll add to the main JPanel? Do I need to use Threads? Any suggestion would be appreciated.