1
0
mirror of https://github.com/fazo96/AIrium.git synced 2025-01-10 09:34:20 +01:00

Fixed brain renderer and implemented creature list in GUI

This commit is contained in:
Enrico Fasoli 2015-07-06 17:40:18 +02:00
parent 124087d381
commit f60469bbdf
7 changed files with 334 additions and 233 deletions

View File

@ -96,20 +96,20 @@ public class Game extends ApplicationAdapter {
} }
} catch (ConcurrentModificationException ex) { } catch (ConcurrentModificationException ex) {
} }
renderer.setColor(0.3f, 0.3f, 0.3f, 1);
// draw borders
renderer.rect(0, 0, world.getWidth(), world.getHeight());
if (world.getSelectedCreature() != null) { if (world.getSelectedCreature() != null) {
// There is a selection // There is a selection
Creature c = world.getSelectedCreature(); Creature c = world.getSelectedCreature();
renderer.setColor(1, 1, 1, 1); renderer.setColor(1, 1, 1, 1);
// Draw selection rectangle // Draw selection rectangle
renderer.rect(c.getX() - c.getSize() / 2, c.getY() - c.getSize() / 2, c.getX() + c.getSize() / 2, c.getY() + c.getSize() / 2); renderer.rect(c.getX() - c.getSize() / 2, c.getY() - c.getSize() / 2, c.getSize(), c.getSize());
// Draw brain // Draw brain
overlayRenderer.begin(); overlayRenderer.begin();
c.getBrain().render(overlayRenderer); c.getBrain().render(overlayRenderer);
overlayRenderer.end(); overlayRenderer.end();
} }
renderer.setColor(0.3f, 0.3f, 0.3f, 1);
// draw borders
renderer.rect(0, 0, world.getWidth(), world.getHeight());
renderer.end(); renderer.end();
} }

View File

@ -0,0 +1,17 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.mygdx.game;
/**
*
* @author Fazo
*/
public interface Listener {
public static int FPS_CHANGED = 0, CREATURE_LIST_CHANGED = 1;
public void on(int event);
}

View File

@ -6,6 +6,7 @@
package logic; package logic;
import com.mygdx.game.Game; import com.mygdx.game.Game;
import com.mygdx.game.Listener;
import com.mygdx.game.Log; import com.mygdx.game.Log;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Comparator; import java.util.Comparator;
@ -30,7 +31,7 @@ public class World implements Runnable {
private final ArrayList<Creature> graveyard; private final ArrayList<Creature> graveyard;
private final ArrayList<Vegetable> plants; private final ArrayList<Vegetable> plants;
private final ArrayList<Vegetable> deadPlants; private final ArrayList<Vegetable> deadPlants;
private final ArrayList<FpsListener> fpsListeners; private final ArrayList<Listener> listeners;
public World(int width, int height) { public World(int width, int height) {
this.width = width; this.width = width;
@ -43,7 +44,7 @@ public class World implements Runnable {
plants = new ArrayList(); plants = new ArrayList();
deadPlants = new ArrayList(); deadPlants = new ArrayList();
graveyard = new ArrayList(); graveyard = new ArrayList();
fpsListeners = new ArrayList(); listeners = new ArrayList();
selected = null; selected = null;
newGen(true); newGen(true);
} }
@ -62,9 +63,7 @@ public class World implements Runnable {
if (now.getTime() - timekeeper.getTime() > 1000) { if (now.getTime() - timekeeper.getTime() > 1000) {
fps = frames; fps = frames;
frames = 0; frames = 0;
for (FpsListener f : fpsListeners) { fire(Listener.FPS_CHANGED);
f.fpsChanged(fps);
}
timekeeper = new Date(); timekeeper = new Date();
} }
if (fpsLimit > 0) { if (fpsLimit > 0) {
@ -106,7 +105,9 @@ public class World implements Runnable {
elements.removeAll(deadPlants); elements.removeAll(deadPlants);
plants.removeAll(deadPlants); plants.removeAll(deadPlants);
deadPlants.clear(); deadPlants.clear();
creatures.removeAll(graveyard); if (creatures.removeAll(graveyard)) {
fire(Listener.CREATURE_LIST_CHANGED);
}
if (creatures.isEmpty()) { if (creatures.isEmpty()) {
// All dead, next gen // All dead, next gen
newGen(false); newGen(false);
@ -176,6 +177,7 @@ public class World implements Runnable {
ne.getBrain().mutate(0.05f); // mutate children ne.getBrain().mutate(0.05f); // mutate children
} }
graveyard.clear(); graveyard.clear();
fire(Listener.CREATURE_LIST_CHANGED);
generation++; generation++;
} }
} }
@ -218,11 +220,6 @@ public class World implements Runnable {
} }
} }
public interface FpsListener {
public abstract void fpsChanged(int newValue);
}
public void selectCreatureAt(int x, int y) { public void selectCreatureAt(int x, int y) {
selected = null; // Clear selection selected = null; // Clear selection
try { try {
@ -236,6 +233,12 @@ public class World implements Runnable {
} }
} }
public void fire(int eventCode) {
for (Listener f : listeners) {
f.on(eventCode);
}
}
private void spawnVegetable() { private void spawnVegetable() {
spawn(false, null); spawn(false, null);
} }
@ -260,8 +263,8 @@ public class World implements Runnable {
return generation; return generation;
} }
public void addFpsListener(FpsListener f) { public void addListener(Listener f) {
fpsListeners.add(f); listeners.add(f);
} }
public void add(Element e) { public void add(Element e) {

View File

@ -88,6 +88,7 @@ public class Brain {
* @param s the ShapeRenderer to use for the drawing * @param s the ShapeRenderer to use for the drawing
*/ */
public void render(ShapeRenderer s) { public void render(ShapeRenderer s) {
int sepX = 100, sepY = 50, offset = 100;
s.set(ShapeRenderer.ShapeType.Filled); s.set(ShapeRenderer.ShapeType.Filled);
int neuronHeight = 0; int neuronHeight = 0;
for (Neuron[] ns : neurons) { for (Neuron[] ns : neurons) {
@ -95,22 +96,20 @@ public class Brain {
neuronHeight = ns.length; neuronHeight = ns.length;
} }
} }
s.rect(0, 0, neurons.length * 50, neuronHeight * 30);
for (int i = 0; i < neurons.length; i++) { for (int i = 0; i < neurons.length; i++) {
//s.set(ShapeRenderer.ShapeType.Line); //s.set(ShapeRenderer.ShapeType.Line);
for (int j = 0; j < neurons[i].length; j++) { for (int j = 0; j < neurons[i].length; j++) {
// get neuron result first so cache system can kick in and save some calculations // get neuron result first so cache system can kick in and save some calculations
float nr = neurons[i][j].compute(); float nr = neurons[i][j].compute();
// Draw neuron links // Draw neuron links
float[] links = neurons[i][j].getInputs(); float[] links = neurons[i][j].getWeights();
for (int f = 0; f < links.length; f++) { for (int f = 0; f < links.length; f++) {
s.setColor(links[f], links[f], links[f], 1); s.setColor(links[f] < 0 ? links[f]/2 * -1 : 0, links[f] > 0 ? links[f]/2 : 0, 0, 1);
s.line(i * 50, j * 30, (i - 1) * 50, f * 30); s.line(i * sepX + offset, j * sepY + offset, (i - 1) * sepX + offset, f * sepY + offset);
} }
// Draw neuron // Draw neuron
s.setColor(1 - nr, nr, 0, 1); s.setColor(1 - nr, nr, 0, 1);
s.set(ShapeRenderer.ShapeType.Filled); s.circle(i * sepX + offset, j * sepY + offset, 15);
s.circle(i * 50, j * 30, 15);
} }
} }
} }
@ -130,7 +129,6 @@ public class Brain {
for (int i = 0; i < values.length; i++) { for (int i = 0; i < values.length; i++) {
neurons[0][i].setOutput(values[i]); neurons[0][i].setOutput(values[i]);
} }
clearCache();
} }
/** /**
@ -140,7 +138,7 @@ public class Brain {
* result * result
*/ */
public float[] compute() { public float[] compute() {
//clearCache(); // unnecessary if already called when changing inputs clearCache(); // unnecessary if already called when changing inputs
float[] res = new float[neurons[neurons.length - 1].length]; float[] res = new float[neurons[neurons.length - 1].length];
for (int i = 0; i < neurons[neurons.length - 1].length; i++) { for (int i = 0; i < neurons[neurons.length - 1].length; i++) {
res[i] = neurons[neurons.length - 1][i].compute(); res[i] = neurons[neurons.length - 1][i].compute();

View File

@ -55,7 +55,9 @@ public class Neuron {
if (isInputNeuron) { if (isInputNeuron) {
return output; return output;
} }
if(cache.hasCachedOutput()) return cache.getCachedOutput(); if (cache.hasCachedOutput()) {
return cache.getCachedOutput();
}
float a = bias * -1; // activation float a = bias * -1; // activation
for (int i = 0; i < weights.length; i++) { for (int i = 0; i < weights.length; i++) {
if (cache.has(i)) { if (cache.has(i)) {
@ -83,6 +85,11 @@ public class Neuron {
return mutatedWeights; return mutatedWeights;
} }
/**
* Broken, doesn't work for some reason.
*
* @return
*/
public float[] getInputs() { public float[] getInputs() {
float inputs[] = new float[weights.length]; float inputs[] = new float[weights.length];
for (int i = 0; i < inputs.length; i++) { for (int i = 0; i < inputs.length; i++) {
@ -99,7 +106,7 @@ public class Neuron {
} }
return inputs; return inputs;
} }
public void setOutput(float output) { public void setOutput(float output) {
isInputNeuron = true; isInputNeuron = true;
this.output = output; this.output = output;

View File

@ -169,6 +169,38 @@
</Component> </Component>
</SubComponents> </SubComponents>
</Container> </Container>
<Container class="javax.swing.JScrollPane" name="jScrollPane2">
<AuxValues>
<AuxValue name="autoScrollPane" type="java.lang.Boolean" value="true"/>
</AuxValues>
<Constraints>
<Constraint layoutClass="org.netbeans.modules.form.compat2.layouts.support.JTabbedPaneSupportLayout" value="org.netbeans.modules.form.compat2.layouts.support.JTabbedPaneSupportLayout$JTabbedPaneConstraintsDescription">
<JTabbedPaneConstraints tabName="Creatures">
<Property name="tabTitle" type="java.lang.String" value="Creatures"/>
</JTabbedPaneConstraints>
</Constraint>
</Constraints>
<Layout class="org.netbeans.modules.form.compat2.layouts.support.JScrollPaneSupportLayout"/>
<SubComponents>
<Component class="javax.swing.JList" name="creatureList">
<Properties>
<Property name="model" type="javax.swing.ListModel" editor="org.netbeans.modules.form.editors2.ListModelEditor">
<StringArray count="5">
<StringItem index="0" value="Item 1"/>
<StringItem index="1" value="Item 2"/>
<StringItem index="2" value="Item 3"/>
<StringItem index="3" value="Item 4"/>
<StringItem index="4" value="Item 5"/>
</StringArray>
</Property>
</Properties>
<Events>
<EventHandler event="valueChanged" listener="javax.swing.event.ListSelectionListener" parameters="javax.swing.event.ListSelectionEvent" handler="creatureListValueChanged"/>
</Events>
</Component>
</SubComponents>
</Container>
</SubComponents> </SubComponents>
</Container> </Container>
<Component class="javax.swing.JLabel" name="status"> <Component class="javax.swing.JLabel" name="status">

View File

@ -1,206 +1,250 @@
/* /*
* To change this license header, choose License Headers in Project Properties. * To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates * To change this template file, choose Tools | Templates
* and open the template in the editor. * and open the template in the editor.
*/ */
package gui; package gui;
import com.badlogic.gdx.backends.lwjgl.LwjglApplication; import com.badlogic.gdx.backends.lwjgl.LwjglApplication;
import com.badlogic.gdx.backends.lwjgl.LwjglApplicationConfiguration; import com.badlogic.gdx.backends.lwjgl.LwjglApplicationConfiguration;
import com.mygdx.game.Game; import com.mygdx.game.Game;
import com.mygdx.game.Log; import com.mygdx.game.Listener;
import com.mygdx.game.Log.LogListener; import com.mygdx.game.Log;
import logic.World; import com.mygdx.game.Log.LogListener;
import javax.swing.JOptionPane;
/** import logic.World;
*
* @author fazo /**
*/ *
public class GUI extends javax.swing.JFrame implements LogListener,World.FpsListener { * @author fazo
*/
private Game game; public class GUI extends javax.swing.JFrame implements LogListener, Listener {
private LwjglApplication app;
private Game game;
/** private LwjglApplication app;
* Creates new form GUI
*/ /**
public GUI() { * Creates new form GUI
initComponents(); */
setLocationRelativeTo(null); // Center the window public GUI() {
Log.addListener(this); initComponents();
logTextArea.setText("Started GUI.\n"); setLocationRelativeTo(null); // Center the window
} Log.addListener(this);
logTextArea.setText("Started GUI.\n");
/** }
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always /**
* regenerated by the Form Editor. * This method is called from within the constructor to initialize the form.
*/ * WARNING: Do NOT modify this code. The content of this method is always
@SuppressWarnings("unchecked") * regenerated by the Form Editor.
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents */
private void initComponents() { @SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
container = new javax.swing.JPanel(); private void initComponents() {
tabs = new javax.swing.JTabbedPane();
logPane = new javax.swing.JPanel(); container = new javax.swing.JPanel();
jScrollPane1 = new javax.swing.JScrollPane(); tabs = new javax.swing.JTabbedPane();
logTextArea = new javax.swing.JTextArea(); logPane = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel(); jScrollPane1 = new javax.swing.JScrollPane();
logLevelBox = new javax.swing.JComboBox(); logTextArea = new javax.swing.JTextArea();
status = new javax.swing.JLabel(); jLabel1 = new javax.swing.JLabel();
menuBar = new javax.swing.JMenuBar(); logLevelBox = new javax.swing.JComboBox();
jMenu1 = new javax.swing.JMenu(); jScrollPane2 = new javax.swing.JScrollPane();
startButton = new javax.swing.JMenuItem(); creatureList = new javax.swing.JList();
exitButton = new javax.swing.JMenuItem(); status = new javax.swing.JLabel();
menuBar = new javax.swing.JMenuBar();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); jMenu1 = new javax.swing.JMenu();
setTitle("AIrium"); startButton = new javax.swing.JMenuItem();
exitButton = new javax.swing.JMenuItem();
logTextArea.setEditable(false);
logTextArea.setColumns(20); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
logTextArea.setLineWrap(true); setTitle("AIrium");
logTextArea.setRows(5);
logTextArea.setText("Started GUI."); logTextArea.setEditable(false);
logTextArea.setWrapStyleWord(true); logTextArea.setColumns(20);
jScrollPane1.setViewportView(logTextArea); logTextArea.setLineWrap(true);
logTextArea.setRows(5);
jLabel1.setText("Log Level"); logTextArea.setText("Started GUI.");
logTextArea.setWrapStyleWord(true);
logLevelBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Errors Only", "Default", "Debug Mode" })); jScrollPane1.setViewportView(logTextArea);
logLevelBox.setSelectedIndex(1);
logLevelBox.setToolTipText(""); jLabel1.setText("Log Level");
logLevelBox.addItemListener(new java.awt.event.ItemListener() {
public void itemStateChanged(java.awt.event.ItemEvent evt) { logLevelBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Errors Only", "Default", "Debug Mode" }));
logLevelBoxItemStateChanged(evt); logLevelBox.setSelectedIndex(1);
} logLevelBox.setToolTipText("");
}); logLevelBox.addItemListener(new java.awt.event.ItemListener() {
public void itemStateChanged(java.awt.event.ItemEvent evt) {
javax.swing.GroupLayout logPaneLayout = new javax.swing.GroupLayout(logPane); logLevelBoxItemStateChanged(evt);
logPane.setLayout(logPaneLayout); }
logPaneLayout.setHorizontalGroup( });
logPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(logPaneLayout.createSequentialGroup() javax.swing.GroupLayout logPaneLayout = new javax.swing.GroupLayout(logPane);
.addComponent(jLabel1) logPane.setLayout(logPaneLayout);
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) logPaneLayout.setHorizontalGroup(
.addComponent(logLevelBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) logPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 0, Short.MAX_VALUE)) .addGroup(logPaneLayout.createSequentialGroup()
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 404, Short.MAX_VALUE) .addComponent(jLabel1)
); .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
logPaneLayout.setVerticalGroup( .addComponent(logLevelBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
logPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGap(0, 0, Short.MAX_VALUE))
.addGroup(logPaneLayout.createSequentialGroup() .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 404, Short.MAX_VALUE)
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 235, Short.MAX_VALUE) );
.addGap(10, 10, 10) logPaneLayout.setVerticalGroup(
.addGroup(logPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) logPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel1) .addGroup(logPaneLayout.createSequentialGroup()
.addComponent(logLevelBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 235, Short.MAX_VALUE)
.addContainerGap()) .addGap(10, 10, 10)
); .addGroup(logPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
tabs.addTab("Log", logPane); .addComponent(logLevelBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap())
status.setText("Simulation stopped"); );
javax.swing.GroupLayout containerLayout = new javax.swing.GroupLayout(container); tabs.addTab("Log", logPane);
container.setLayout(containerLayout);
containerLayout.setHorizontalGroup( creatureList.setModel(new javax.swing.AbstractListModel() {
containerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
.addComponent(tabs) public int getSize() { return strings.length; }
.addGroup(containerLayout.createSequentialGroup() public Object getElementAt(int i) { return strings[i]; }
.addComponent(status) });
.addGap(0, 0, Short.MAX_VALUE)) creatureList.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
); public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
containerLayout.setVerticalGroup( creatureListValueChanged(evt);
containerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) }
.addGroup(containerLayout.createSequentialGroup() });
.addComponent(tabs) jScrollPane2.setViewportView(creatureList);
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(status)) tabs.addTab("Creatures", jScrollPane2);
);
status.setText("Simulation stopped");
jMenu1.setText("File");
javax.swing.GroupLayout containerLayout = new javax.swing.GroupLayout(container);
startButton.setText("Start"); container.setLayout(containerLayout);
startButton.addActionListener(new java.awt.event.ActionListener() { containerLayout.setHorizontalGroup(
public void actionPerformed(java.awt.event.ActionEvent evt) { containerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
startButtonActionPerformed(evt); .addComponent(tabs)
} .addGroup(containerLayout.createSequentialGroup()
}); .addComponent(status)
jMenu1.add(startButton); .addGap(0, 0, Short.MAX_VALUE))
);
exitButton.setText("Exit"); containerLayout.setVerticalGroup(
exitButton.addActionListener(new java.awt.event.ActionListener() { containerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
public void actionPerformed(java.awt.event.ActionEvent evt) { .addGroup(containerLayout.createSequentialGroup()
exitButtonActionPerformed(evt); .addComponent(tabs)
} .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
}); .addComponent(status))
jMenu1.add(exitButton); );
menuBar.add(jMenu1); jMenu1.setText("File");
setJMenuBar(menuBar); startButton.setText("Start");
startButton.addActionListener(new java.awt.event.ActionListener() {
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); public void actionPerformed(java.awt.event.ActionEvent evt) {
getContentPane().setLayout(layout); startButtonActionPerformed(evt);
layout.setHorizontalGroup( }
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) });
.addComponent(container, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) jMenu1.add(startButton);
);
layout.setVerticalGroup( exitButton.setText("Exit");
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) exitButton.addActionListener(new java.awt.event.ActionListener() {
.addComponent(container, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) public void actionPerformed(java.awt.event.ActionEvent evt) {
); exitButtonActionPerformed(evt);
}
pack(); });
}// </editor-fold>//GEN-END:initComponents jMenu1.add(exitButton);
private void exitButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exitButtonActionPerformed menuBar.add(jMenu1);
System.exit(0);
}//GEN-LAST:event_exitButtonActionPerformed setJMenuBar(menuBar);
private void startButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_startButtonActionPerformed javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
if (game != null) { getContentPane().setLayout(layout);
return; layout.setHorizontalGroup(
} layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
LwjglApplicationConfiguration.disableAudio = true; .addComponent(container, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
LwjglApplicationConfiguration config = new LwjglApplicationConfiguration(); );
config.height = 600; layout.setVerticalGroup(
config.width = 800; layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
config.resizable = false; .addComponent(container, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
app = new LwjglApplication(game = new Game(), config); );
game.getWorld().addFpsListener(this);
}//GEN-LAST:event_startButtonActionPerformed pack();
@Override }// </editor-fold>//GEN-END:initComponents
public void onLog(int level, String msg) {
logTextArea.append(msg + "\n"); private void exitButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exitButtonActionPerformed
setScrollBarToTheBottom(); System.exit(0);
} }//GEN-LAST:event_exitButtonActionPerformed
public void setScrollBarToTheBottom() { private void startButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_startButtonActionPerformed
jScrollPane1.getVerticalScrollBar().setValue(jScrollPane1.getVerticalScrollBar().getMaximum()); if (game != null) {
} return;
}
private void logLevelBoxItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_logLevelBoxItemStateChanged LwjglApplicationConfiguration.disableAudio = true;
Log.setLogLevel(logLevelBox.getSelectedIndex()); LwjglApplicationConfiguration config = new LwjglApplicationConfiguration();
}//GEN-LAST:event_logLevelBoxItemStateChanged config.height = 600;
config.width = 800;
// Variables declaration - do not modify//GEN-BEGIN:variables config.resizable = false;
private javax.swing.JPanel container; app = new LwjglApplication(game = new Game(), config);
private javax.swing.JMenuItem exitButton; game.getWorld().addListener(this);
private javax.swing.JLabel jLabel1; setCreatureList();
private javax.swing.JMenu jMenu1; }//GEN-LAST:event_startButtonActionPerformed
private javax.swing.JScrollPane jScrollPane1; @Override
private javax.swing.JComboBox logLevelBox; public void onLog(int level, String msg) {
private javax.swing.JPanel logPane; logTextArea.append(msg + "\n");
private javax.swing.JTextArea logTextArea; setScrollBarToTheBottom();
private javax.swing.JMenuBar menuBar; }
private javax.swing.JMenuItem startButton;
private javax.swing.JLabel status; @Override
private javax.swing.JTabbedPane tabs; public void on(int event) {
// End of variables declaration//GEN-END:variables if (event == Listener.FPS_CHANGED) {
status.setText("Generation: " + game.getWorld().getGeneration() + " FPS: " + game.getWorld().getFps());
@Override } else if (event == Listener.CREATURE_LIST_CHANGED) {
public void fpsChanged(int fps) { setCreatureList();
status.setText("Generation: "+game.getWorld().getGeneration()+" FPS: "+fps); }
} }
}
private void setCreatureList() {
String list[] = new String[game.getWorld().getCreatures().size()];
for (int i = 0; i < list.length; i++) {
list[i] = "Fitness: " + game.getWorld().getCreatures().get(i).getFitness();
}
creatureList.setListData(list);
}
public void setScrollBarToTheBottom() {
jScrollPane1.getVerticalScrollBar().setValue(jScrollPane1.getVerticalScrollBar().getMaximum());
}
private void logLevelBoxItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_logLevelBoxItemStateChanged
Log.setLogLevel(logLevelBox.getSelectedIndex());
}//GEN-LAST:event_logLevelBoxItemStateChanged
private void creatureListValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_creatureListValueChanged
try {
if (creatureList.getSelectedIndex() >= 0) {
Game.get().getWorld().selectCreature(Game.get().getWorld().getCreatures().get(creatureList.getSelectedIndex()));
}
} catch (IndexOutOfBoundsException ex) {
JOptionPane.showMessageDialog(this, "This creature is not available");
}
}//GEN-LAST:event_creatureListValueChanged
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JPanel container;
private javax.swing.JList creatureList;
private javax.swing.JMenuItem exitButton;
private javax.swing.JLabel jLabel1;
private javax.swing.JMenu jMenu1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JComboBox logLevelBox;
private javax.swing.JPanel logPane;
private javax.swing.JTextArea logTextArea;
private javax.swing.JMenuBar menuBar;
private javax.swing.JMenuItem startButton;
private javax.swing.JLabel status;
private javax.swing.JTabbedPane tabs;
// End of variables declaration//GEN-END:variables
}