mirror of
https://github.com/fazo96/AIrium.git
synced 2025-04-01 22:48:39 +02:00
275 lines
11 KiB
Java
275 lines
11 KiB
Java
/*
|
|
* 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 gui;
|
|
|
|
import com.badlogic.gdx.backends.lwjgl.LwjglApplication;
|
|
import com.badlogic.gdx.backends.lwjgl.LwjglApplicationConfiguration;
|
|
import com.mygdx.game.Game;
|
|
import com.mygdx.game.Listener;
|
|
import com.mygdx.game.Log;
|
|
import com.mygdx.game.Log.LogListener;
|
|
import java.util.logging.Level;
|
|
import java.util.logging.Logger;
|
|
import javax.swing.JOptionPane;
|
|
import logic.World;
|
|
|
|
/**
|
|
*
|
|
* @author fazo
|
|
*/
|
|
public class GUI extends javax.swing.JFrame implements LogListener, Listener {
|
|
|
|
private Game game;
|
|
private LwjglApplication app;
|
|
private boolean shouldUpdateGUI = false;
|
|
private Thread guiUpdater;
|
|
|
|
/**
|
|
* Creates new form GUI
|
|
*/
|
|
public GUI() {
|
|
initComponents();
|
|
setLocationRelativeTo(null); // Center the window
|
|
Log.addListener(this);
|
|
guiUpdater = new Thread() {
|
|
@Override
|
|
public void run() {
|
|
for (;;) {
|
|
if (shouldUpdateGUI) {
|
|
updateGUI();
|
|
shouldUpdateGUI = false;
|
|
} else {
|
|
try {
|
|
Thread.sleep(5000);
|
|
} catch (InterruptedException ex) {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
};
|
|
guiUpdater.setPriority(Thread.MAX_PRIORITY);
|
|
guiUpdater.start();
|
|
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.
|
|
*/
|
|
@SuppressWarnings("unchecked")
|
|
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
|
|
private void initComponents() {
|
|
|
|
container = new javax.swing.JPanel();
|
|
tabs = new javax.swing.JTabbedPane();
|
|
logPane = new javax.swing.JPanel();
|
|
jScrollPane1 = new javax.swing.JScrollPane();
|
|
logTextArea = new javax.swing.JTextArea();
|
|
jLabel1 = new javax.swing.JLabel();
|
|
logLevelBox = new javax.swing.JComboBox();
|
|
jScrollPane2 = new javax.swing.JScrollPane();
|
|
creatureList = new javax.swing.JList();
|
|
status = new javax.swing.JLabel();
|
|
menuBar = new javax.swing.JMenuBar();
|
|
jMenu1 = new javax.swing.JMenu();
|
|
startButton = new javax.swing.JMenuItem();
|
|
exitButton = new javax.swing.JMenuItem();
|
|
|
|
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
|
|
setTitle("AIrium");
|
|
|
|
logTextArea.setEditable(false);
|
|
logTextArea.setColumns(20);
|
|
logTextArea.setLineWrap(true);
|
|
logTextArea.setRows(5);
|
|
logTextArea.setText("Started GUI.");
|
|
logTextArea.setWrapStyleWord(true);
|
|
jScrollPane1.setViewportView(logTextArea);
|
|
|
|
jLabel1.setText("Log Level");
|
|
|
|
logLevelBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Errors Only", "Default", "Debug Mode" }));
|
|
logLevelBox.setSelectedIndex(1);
|
|
logLevelBox.setToolTipText("");
|
|
logLevelBox.addItemListener(new java.awt.event.ItemListener() {
|
|
public void itemStateChanged(java.awt.event.ItemEvent evt) {
|
|
logLevelBoxItemStateChanged(evt);
|
|
}
|
|
});
|
|
|
|
javax.swing.GroupLayout logPaneLayout = new javax.swing.GroupLayout(logPane);
|
|
logPane.setLayout(logPaneLayout);
|
|
logPaneLayout.setHorizontalGroup(
|
|
logPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
|
|
.addGroup(logPaneLayout.createSequentialGroup()
|
|
.addComponent(jLabel1)
|
|
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
|
|
.addComponent(logLevelBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
|
|
.addGap(0, 0, Short.MAX_VALUE))
|
|
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 404, Short.MAX_VALUE)
|
|
);
|
|
logPaneLayout.setVerticalGroup(
|
|
logPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
|
|
.addGroup(logPaneLayout.createSequentialGroup()
|
|
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 235, Short.MAX_VALUE)
|
|
.addGap(10, 10, 10)
|
|
.addGroup(logPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
|
|
.addComponent(jLabel1)
|
|
.addComponent(logLevelBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
|
|
.addContainerGap())
|
|
);
|
|
|
|
tabs.addTab("Log", logPane);
|
|
|
|
creatureList.setModel(new javax.swing.AbstractListModel() {
|
|
String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
|
|
public int getSize() { return strings.length; }
|
|
public Object getElementAt(int i) { return strings[i]; }
|
|
});
|
|
creatureList.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
|
|
public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
|
|
creatureListValueChanged(evt);
|
|
}
|
|
});
|
|
jScrollPane2.setViewportView(creatureList);
|
|
|
|
tabs.addTab("Creatures", jScrollPane2);
|
|
|
|
status.setText("Simulation stopped");
|
|
|
|
javax.swing.GroupLayout containerLayout = new javax.swing.GroupLayout(container);
|
|
container.setLayout(containerLayout);
|
|
containerLayout.setHorizontalGroup(
|
|
containerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
|
|
.addComponent(tabs)
|
|
.addGroup(containerLayout.createSequentialGroup()
|
|
.addComponent(status)
|
|
.addGap(0, 0, Short.MAX_VALUE))
|
|
);
|
|
containerLayout.setVerticalGroup(
|
|
containerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
|
|
.addGroup(containerLayout.createSequentialGroup()
|
|
.addComponent(tabs)
|
|
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
|
|
.addComponent(status))
|
|
);
|
|
|
|
jMenu1.setText("File");
|
|
|
|
startButton.setText("Start");
|
|
startButton.addActionListener(new java.awt.event.ActionListener() {
|
|
public void actionPerformed(java.awt.event.ActionEvent evt) {
|
|
startButtonActionPerformed(evt);
|
|
}
|
|
});
|
|
jMenu1.add(startButton);
|
|
|
|
exitButton.setText("Exit");
|
|
exitButton.addActionListener(new java.awt.event.ActionListener() {
|
|
public void actionPerformed(java.awt.event.ActionEvent evt) {
|
|
exitButtonActionPerformed(evt);
|
|
}
|
|
});
|
|
jMenu1.add(exitButton);
|
|
|
|
menuBar.add(jMenu1);
|
|
|
|
setJMenuBar(menuBar);
|
|
|
|
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
|
|
getContentPane().setLayout(layout);
|
|
layout.setHorizontalGroup(
|
|
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
|
|
.addComponent(container, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
|
|
);
|
|
layout.setVerticalGroup(
|
|
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
|
|
.addComponent(container, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
|
|
);
|
|
|
|
pack();
|
|
}// </editor-fold>//GEN-END:initComponents
|
|
|
|
private void exitButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exitButtonActionPerformed
|
|
System.exit(0);
|
|
}//GEN-LAST:event_exitButtonActionPerformed
|
|
|
|
private void startButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_startButtonActionPerformed
|
|
if (game != null) {
|
|
return;
|
|
}
|
|
LwjglApplicationConfiguration.disableAudio = true;
|
|
LwjglApplicationConfiguration config = new LwjglApplicationConfiguration();
|
|
config.height = 600;
|
|
config.width = 800;
|
|
config.resizable = false;
|
|
app = new LwjglApplication(game = new Game(), config);
|
|
game.getWorld().addListener(this);
|
|
setCreatureList();
|
|
}//GEN-LAST:event_startButtonActionPerformed
|
|
@Override
|
|
public void onLog(int level, String msg) {
|
|
logTextArea.append(msg + "\n");
|
|
setScrollBarToTheBottom();
|
|
}
|
|
|
|
@Override
|
|
public void on(int event) {
|
|
shouldUpdateGUI = true;
|
|
guiUpdater.interrupt();
|
|
}
|
|
|
|
public void updateGUI() {
|
|
status.setText("Generation: " + game.getWorld().getGeneration() + " FPS: " + game.getWorld().getFps());
|
|
setCreatureList();
|
|
}
|
|
|
|
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
|
|
|
|
}
|