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

added shiny new GUI

This commit is contained in:
Enrico Fasoli 2015-07-05 23:08:41 +02:00
parent 6ee54d8d8f
commit 75146638bf
6 changed files with 508 additions and 24 deletions

View File

@ -22,7 +22,6 @@ public class Game extends ApplicationAdapter {
@Override @Override
public void create() { public void create() {
game = this; game = this;
world = new World(2500, 2500);
shaper = new ShapeRenderer(); shaper = new ShapeRenderer();
shaper.setAutoShapeType(true); shaper.setAutoShapeType(true);
font = new BitmapFont(); font = new BitmapFont();
@ -32,6 +31,10 @@ public class Game extends ApplicationAdapter {
worldThread.start(); worldThread.start();
} }
public Game() {
world = new World(2500, 2500);
}
@Override @Override
public void render() { public void render() {
// Controls // Controls
@ -76,7 +79,7 @@ public class Game extends ApplicationAdapter {
try { try {
e.render(shaper); e.render(shaper);
} catch (ArrayIndexOutOfBoundsException ex) { } catch (ArrayIndexOutOfBoundsException ex) {
// No idea why it happens, but it's rendering so meh // No idea why it happens, but it's rendering so meh
//Log.log(Log.ERROR, ex+""); //Log.log(Log.ERROR, ex+"");
} }
} }

View File

@ -5,6 +5,8 @@
*/ */
package com.mygdx.game; package com.mygdx.game;
import java.util.ArrayList;
/** /**
* *
* @author fazo * @author fazo
@ -14,12 +16,18 @@ public class Log {
public static final int ERROR = 0; public static final int ERROR = 0;
public static final int INFO = 1; public static final int INFO = 1;
public static final int DEBUG = 2; public static final int DEBUG = 2;
private static ArrayList<LogListener> logListeners;
private static int logLevel = 1; private static int logLevel = 1;
public static void log(int level, String msg) { public static void log(int level, String msg) {
if (level <= logLevel) { if (level <= logLevel) {
System.out.println(msg); if (logListeners == null) {
logListeners = new ArrayList<LogListener>();
}
for (LogListener l : logListeners) {
l.onLog(level, msg);
}
} }
} }
@ -31,4 +39,15 @@ public class Log {
Log.logLevel = logLevel; Log.logLevel = logLevel;
} }
public interface LogListener {
public abstract void onLog(int level, String msg);
}
public static void addListener(LogListener l) {
if (logListeners == null) {
logListeners = new ArrayList<LogListener>();
}
logListeners.add(l);
}
} }

View File

@ -21,13 +21,14 @@ public class World implements Runnable {
private final int width, height, nPlants, creatPerGen; private final int width, height, nPlants, creatPerGen;
private int generation = 1; private int generation = 1;
private float fpsLimit = 60; private int fpsLimit = 60, fps = 0;
public ArrayList<Element> elements; private final ArrayList<Element> elements;
public ArrayList<Element> toAdd; private final ArrayList<Element> toAdd;
public ArrayList<Creature> creatures; private final ArrayList<Creature> creatures;
public ArrayList<Creature> graveyard; private final ArrayList<Creature> graveyard;
public ArrayList<Vegetable> plants; private final ArrayList<Vegetable> plants;
public ArrayList<Vegetable> deadPlants; private final ArrayList<Vegetable> deadPlants;
private final ArrayList<FpsListener> fpsListeners;
public World(int width, int height) { public World(int width, int height) {
this.width = width; this.width = width;
@ -40,20 +41,31 @@ 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();
newGen(true); newGen(true);
} }
@Override @Override
public void run() { public void run() {
Date d; Date d, timekeeper = new Date();
long time; long time;
float target; int target, frames = 0;
for (;;) { for (;;) {
if (!Game.get().isPaused()) { if (!Game.get().isPaused()) {
d = new Date(); d = new Date();
update(); update();
frames++;
Date now = new Date();
if (now.getTime() - timekeeper.getTime() > 1000) {
fps = frames;
frames = 0;
for (FpsListener f : fpsListeners) {
f.fpsChanged(fps);
}
timekeeper = new Date();
}
if (fpsLimit > 0) { if (fpsLimit > 0) {
time = new Date().getTime() - d.getTime(); time = now.getTime() - d.getTime();
target = 1000 / fpsLimit; target = 1000 / fpsLimit;
if (time < target) { if (time < target) {
try { try {
@ -195,6 +207,11 @@ public class World implements Runnable {
} }
} }
public interface FpsListener {
public abstract void fpsChanged(int newValue);
}
private void spawnVegetable() { private void spawnVegetable() {
spawn(false, null); spawn(false, null);
} }
@ -215,6 +232,14 @@ public class World implements Runnable {
return height; return height;
} }
public int getGeneration() {
return generation;
}
public void addFpsListener(FpsListener f) {
fpsListeners.add(f);
}
public void add(Element e) { public void add(Element e) {
toAdd.add(e); toAdd.add(e);
} }
@ -243,8 +268,12 @@ public class World implements Runnable {
return fpsLimit; return fpsLimit;
} }
public void setFpsLimit(float fpsLimit) { public void setFpsLimit(int fpsLimit) {
this.fpsLimit = fpsLimit; this.fpsLimit = fpsLimit;
} }
public float getFps() {
return fps;
}
} }

View File

@ -1,17 +1,62 @@
/*
* 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.desktop; package com.mygdx.game.desktop;
import com.badlogic.gdx.backends.lwjgl.LwjglApplication; import gui.GUI;
import com.badlogic.gdx.backends.lwjgl.LwjglApplicationConfiguration; import javax.swing.UIManager;
import com.mygdx.game.Game;
/**
*
* @author fazo
*/
public class DesktopLauncher { public class DesktopLauncher {
public static void main(String[] arg) { /**
LwjglApplicationConfiguration.disableAudio = true; * @param args the command line arguments
LwjglApplicationConfiguration config = new LwjglApplicationConfiguration(); */
config.height = 600; public static void main(String args[]) {
config.width = 800; /* Set the Nimbus look and feel */
config.resizable = false; //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
new LwjglApplication(new Game(), config); /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(GUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(GUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(GUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(GUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/*
Setting the native OS look and feel. This way the program uses the OS's
window toolkit instead of the Java one to render the application, if it
is possible.
*/
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception ex) {
System.out.println("Unable to load native look and feel");
}
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new GUI().setVisible(true);
}
});
} }
} }

182
desktop/src/gui/GUI.form Normal file
View File

@ -0,0 +1,182 @@
<?xml version="1.0" encoding="UTF-8" ?>
<Form version="1.3" maxVersion="1.9" type="org.netbeans.modules.form.forminfo.JFrameFormInfo">
<NonVisualComponents>
<Menu class="javax.swing.JMenuBar" name="menuBar">
<SubComponents>
<Menu class="javax.swing.JMenu" name="jMenu1">
<Properties>
<Property name="text" type="java.lang.String" value="File"/>
</Properties>
<SubComponents>
<MenuItem class="javax.swing.JMenuItem" name="startButton">
<Properties>
<Property name="text" type="java.lang.String" value="Start"/>
</Properties>
<Events>
<EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="startButtonActionPerformed"/>
</Events>
</MenuItem>
<MenuItem class="javax.swing.JMenuItem" name="exitButton">
<Properties>
<Property name="text" type="java.lang.String" value="Exit"/>
</Properties>
<Events>
<EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="exitButtonActionPerformed"/>
</Events>
</MenuItem>
</SubComponents>
</Menu>
</SubComponents>
</Menu>
</NonVisualComponents>
<Properties>
<Property name="defaultCloseOperation" type="int" value="3"/>
<Property name="title" type="java.lang.String" value="AIrium"/>
</Properties>
<SyntheticProperties>
<SyntheticProperty name="menuBar" type="java.lang.String" value="menuBar"/>
<SyntheticProperty name="formSizePolicy" type="int" value="1"/>
<SyntheticProperty name="generateCenter" type="boolean" value="false"/>
</SyntheticProperties>
<AuxValues>
<AuxValue name="FormSettings_autoResourcing" type="java.lang.Integer" value="0"/>
<AuxValue name="FormSettings_autoSetComponentName" type="java.lang.Boolean" value="false"/>
<AuxValue name="FormSettings_generateFQN" type="java.lang.Boolean" value="true"/>
<AuxValue name="FormSettings_generateMnemonicsCode" type="java.lang.Boolean" value="false"/>
<AuxValue name="FormSettings_i18nAutoMode" type="java.lang.Boolean" value="false"/>
<AuxValue name="FormSettings_layoutCodeTarget" type="java.lang.Integer" value="1"/>
<AuxValue name="FormSettings_listenerGenerationStyle" type="java.lang.Integer" value="0"/>
<AuxValue name="FormSettings_variablesLocal" type="java.lang.Boolean" value="false"/>
<AuxValue name="FormSettings_variablesModifier" type="java.lang.Integer" value="2"/>
</AuxValues>
<Layout>
<DimensionLayout dim="0">
<Group type="103" groupAlignment="0" attributes="0">
<Component id="container" alignment="0" max="32767" attributes="0"/>
</Group>
</DimensionLayout>
<DimensionLayout dim="1">
<Group type="103" groupAlignment="0" attributes="0">
<Component id="container" alignment="0" max="32767" attributes="0"/>
</Group>
</DimensionLayout>
</Layout>
<SubComponents>
<Container class="javax.swing.JPanel" name="container">
<Layout>
<DimensionLayout dim="0">
<Group type="103" groupAlignment="0" attributes="0">
<Component id="tabs" max="32767" attributes="0"/>
<Group type="102" attributes="0">
<Component id="status" min="-2" max="-2" attributes="0"/>
<EmptySpace min="0" pref="0" max="32767" attributes="0"/>
</Group>
</Group>
</DimensionLayout>
<DimensionLayout dim="1">
<Group type="103" groupAlignment="0" attributes="0">
<Group type="102" alignment="0" attributes="0">
<Component id="tabs" max="32767" attributes="0"/>
<EmptySpace max="-2" attributes="0"/>
<Component id="status" min="-2" max="-2" attributes="0"/>
</Group>
</Group>
</DimensionLayout>
</Layout>
<SubComponents>
<Container class="javax.swing.JTabbedPane" name="tabs">
<Layout class="org.netbeans.modules.form.compat2.layouts.support.JTabbedPaneSupportLayout"/>
<SubComponents>
<Container class="javax.swing.JPanel" name="logPane">
<Constraints>
<Constraint layoutClass="org.netbeans.modules.form.compat2.layouts.support.JTabbedPaneSupportLayout" value="org.netbeans.modules.form.compat2.layouts.support.JTabbedPaneSupportLayout$JTabbedPaneConstraintsDescription">
<JTabbedPaneConstraints tabName="Log">
<Property name="tabTitle" type="java.lang.String" value="Log"/>
</JTabbedPaneConstraints>
</Constraint>
</Constraints>
<Layout>
<DimensionLayout dim="0">
<Group type="103" groupAlignment="0" attributes="0">
<Group type="102" attributes="0">
<Component id="jLabel1" min="-2" max="-2" attributes="0"/>
<EmptySpace max="-2" attributes="0"/>
<Component id="logLevelBox" min="-2" max="-2" attributes="0"/>
<EmptySpace min="0" pref="0" max="32767" attributes="0"/>
</Group>
<Component id="jScrollPane1" alignment="0" pref="404" max="32767" attributes="0"/>
</Group>
</DimensionLayout>
<DimensionLayout dim="1">
<Group type="103" groupAlignment="0" attributes="0">
<Group type="102" alignment="0" attributes="0">
<Component id="jScrollPane1" pref="235" max="32767" attributes="0"/>
<EmptySpace min="-2" pref="10" max="-2" attributes="0"/>
<Group type="103" groupAlignment="3" attributes="0">
<Component id="jLabel1" alignment="3" min="-2" max="-2" attributes="0"/>
<Component id="logLevelBox" alignment="3" min="-2" max="-2" attributes="0"/>
</Group>
<EmptySpace max="-2" attributes="0"/>
</Group>
</Group>
</DimensionLayout>
</Layout>
<SubComponents>
<Container class="javax.swing.JScrollPane" name="jScrollPane1">
<AuxValues>
<AuxValue name="autoScrollPane" type="java.lang.Boolean" value="true"/>
</AuxValues>
<Layout class="org.netbeans.modules.form.compat2.layouts.support.JScrollPaneSupportLayout"/>
<SubComponents>
<Component class="javax.swing.JTextArea" name="logTextArea">
<Properties>
<Property name="editable" type="boolean" value="false"/>
<Property name="columns" type="int" value="20"/>
<Property name="lineWrap" type="boolean" value="true"/>
<Property name="rows" type="int" value="5"/>
<Property name="text" type="java.lang.String" value="Started GUI."/>
<Property name="wrapStyleWord" type="boolean" value="true"/>
</Properties>
</Component>
</SubComponents>
</Container>
<Component class="javax.swing.JLabel" name="jLabel1">
<Properties>
<Property name="text" type="java.lang.String" value="Log Level"/>
</Properties>
</Component>
<Component class="javax.swing.JComboBox" name="logLevelBox">
<Properties>
<Property name="model" type="javax.swing.ComboBoxModel" editor="org.netbeans.modules.form.editors2.ComboBoxModelEditor">
<StringArray count="3">
<StringItem index="0" value="Errors Only"/>
<StringItem index="1" value="Default"/>
<StringItem index="2" value="Debug Mode"/>
</StringArray>
</Property>
<Property name="selectedIndex" type="int" value="1"/>
<Property name="toolTipText" type="java.lang.String" value=""/>
</Properties>
<Events>
<EventHandler event="itemStateChanged" listener="java.awt.event.ItemListener" parameters="java.awt.event.ItemEvent" handler="logLevelBoxItemStateChanged"/>
</Events>
</Component>
</SubComponents>
</Container>
</SubComponents>
</Container>
<Component class="javax.swing.JLabel" name="status">
<Properties>
<Property name="text" type="java.lang.String" value="Simulation stopped"/>
</Properties>
</Component>
</SubComponents>
</Container>
</SubComponents>
</Form>

206
desktop/src/gui/GUI.java Normal file
View File

@ -0,0 +1,206 @@
/*
* 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.Log;
import com.mygdx.game.Log.LogListener;
import logic.World;
/**
*
* @author fazo
*/
public class GUI extends javax.swing.JFrame implements LogListener,World.FpsListener {
private Game game;
private LwjglApplication app;
/**
* Creates new form GUI
*/
public GUI() {
initComponents();
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.
*/
@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();
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);
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().addFpsListener(this);
}//GEN-LAST:event_startButtonActionPerformed
@Override
public void onLog(int level, String msg) {
logTextArea.append(msg + "\n");
setScrollBarToTheBottom();
}
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
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JPanel container;
private javax.swing.JMenuItem exitButton;
private javax.swing.JLabel jLabel1;
private javax.swing.JMenu jMenu1;
private javax.swing.JScrollPane jScrollPane1;
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
@Override
public void fpsChanged(int fps) {
status.setText("Generation: "+game.getWorld().getGeneration()+" FPS: "+fps);
}
}