Commit 3e1fc830 authored by Ant01n3's avatar Ant01n3

Removed the old GUI and the dependancy on OpenGL.

parent ac0e8fe4
package org.graphstream.boids.gui;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.Timer;
import org.graphstream.boids.BoidSpecies;
import org.graphstream.boids.Context;
import org.graphstream.ui.swingViewer.Viewer;
import org.graphstream.ui.swingViewer.util.Camera;
public class GUI extends JFrame implements ActionListener
{
// Attributes
private static final long serialVersionUID = 1L;
/**
* Shared information.
*/
protected Context ctx;
/**
* The swing timer.
*/
protected Timer timer;
/**
* Viewer of the particles.
*/
protected ParticleViewer viewer;
// Constructors
public GUI( Context context )
{
ctx = context;
viewer = new ParticleViewer( context );
build();
}
public void disconnect()
{
viewer.disconnect();
}
public void reconnect()
{
viewer.reconnect();
}
protected void build()
{
setLayout( new BorderLayout() );
add( viewer, BorderLayout.CENTER );
timer = new Timer( ctx.sleepTime, this );
Dimension dim = new Dimension( 770, 770 );
setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
setMinimumSize( dim );
setPreferredSize( dim );
setSize( dim );
setVisible( true );
timer.start();
}
public void actionPerformed( ActionEvent e )
{
if( e.getSource() == timer )
{
viewer.display();
}
}
public static void main(String args[]) {
Context ctx = new Context();
GUI gui = new GUI(ctx);
BoidSpecies species = ctx.getDefaultSpecies();
species.angleOfView = 0;
for (int i = 0; i < 100; i++)
ctx.addNode(String.format("boid%03d", i));
while (true) {
ctx.stepBegins(0);
try {
Thread.sleep(50);
} catch (InterruptedException e) {
}
}
}
}
\ No newline at end of file
package org.graphstream.boids.gui;
import java.awt.Color;
import java.util.HashMap;
import java.util.LinkedList;
import javax.media.opengl.GL;
import javax.media.opengl.GL2;
import org.graphstream.glutil.geom.Geometry;
import org.miv.pherd.geom.Point3;
import org.miv.pherd.geom.Vector3;
/**
* Particle representation.
*
* @author Antoine Dutot
*/
public class GraphicParticle implements Geometry {
// Attributes
/**
* Particle identifier.
*/
public Object id;
/**
* Position.
*/
public Point3 pos = new Point3();
/**
* Colour.
*/
public Color color = Color.BLACK;
/**
* Text label.
*/
public String label;
/**
* The width.
*/
public float width = 4;
/**
* Attributes.
*/
public HashMap<String, Object> attributes;
/**
* The force vectors (if drawn).
*/
public Vector3 force0, force1, force2;
/**
* Size of the trail behind the particle.
*/
public int positionsMemory = 0;
/**
* The trail memory.
*/
public LinkedList<Point3> positions = new LinkedList<Point3>();
// Constructors
public GraphicParticle(Object id, double x, double y, double z) {
this.id = id;
pos.set(x, y, z);
}
// Access
public Object getId() {
return id;
}
public double getX() {
return pos.x;
}
public double getY() {
return pos.y;
}
public double getZ() {
return pos.z;
}
// Commands
public void setPosition(double x, double y, double z) {
if (positionsMemory > 0 && positions.size() == positionsMemory)
positions.removeLast();
Point3 oldPos = pos;
pos = new Point3(x, y, z);
if (positionsMemory > 0)
positions.addFirst(oldPos);
}
public void delete(GL2 gl) {
}
public void display(GL2 gl) {
double x = pos.x;
double y = pos.y;
double z = pos.z;
gl.glPointSize(width);
gl.glBegin(GL.GL_POINTS);
gl.glColor3f(color.getRed() / 255f, color.getGreen() / 255f,
color.getBlue() / 255f);
gl.glVertex3d(x, y, z);
gl.glEnd();
float alpha = 1f;
gl.glBegin(GL.GL_LINES);
Point3 prev = pos;
for (Point3 p : positions) {
gl.glColor4f(color.getRed() / 255f, color.getGreen() / 255f,
color.getBlue() / 255f, alpha * 0.1f);
// gl.glColor4f( 0, 0, 0, alpha * 0.1f );
gl.glVertex3d(prev.x, prev.y, prev.z);
gl.glVertex3d(p.x, p.y, p.z);
prev = p;
alpha -= 1f / positionsMemory;
}
gl.glEnd();
if (force0 != null && force1 != null && force2 != null) {
gl.glBegin(GL.GL_LINES);
gl.glColor4f(1, 0, 0, 0.7f);
gl.glVertex3d(x, y, z);
gl.glVertex3d(x + force0.data[0], y + force0.data[1], z
+ force0.data[2]);
gl.glColor4f(0, 1, 0, 0.7f);
gl.glVertex3d(x, y, z);
gl.glVertex3d(x + force1.data[0], y + force1.data[1], z
+ force1.data[2]);
gl.glColor4f(0, 0, 1, 0.7f);
gl.glVertex3d(x, y, z);
gl.glVertex3d(x + force2.data[0], y + force2.data[1], z
+ force2.data[2]);
gl.glEnd();
}
}
public void setAttribute(String attribute, Object... values) {
Object v;
if (values == null || values.length == 0)
v = true;
else if (values.length == 1)
v = values[0];
else
v = values;
if (attributes == null)
attributes = new HashMap<String, Object>();
attributes.put(attribute, v);
checkAttribute(attribute, v);
}
public void removeAttribute(String attribute) {
if (attributes != null) {
attributes.remove(attributes);
}
}
public void checkAttribute(String attribute, Object value) {
if (attribute.equals("force0") && value instanceof Vector3) {
force0 = (Vector3) value;
} else if (attribute.equals("force1") && value instanceof Vector3) {
force1 = (Vector3) value;
} else if (attribute.equals("force2") && value instanceof Vector3) {
force2 = (Vector3) value;
} else if (attribute.equals("color") && value instanceof Color) {
color = (Color) value;
} else if (attribute.equals("width") && value instanceof Number) {
width = ((Number) value).floatValue();
} else if (attribute.equals("trail") && value instanceof Number) {
positionsMemory = ((Number) value).intValue();
if (positions.size() > positionsMemory)
positions.clear();
}
}
}
\ No newline at end of file
package org.graphstream.boids.gui;
import java.awt.BorderLayout;
import java.awt.Color;
import java.util.HashMap;
import java.util.Iterator;
import javax.media.opengl.GL;
import javax.media.opengl.GL2;
import javax.media.opengl.glu.GLU;
import javax.swing.JPanel;
import org.graphstream.boids.Context;
import org.graphstream.glutil.Buffer;
import org.graphstream.glutil.BufferListener;
import org.graphstream.glutil.SwingBuffer;
import org.graphstream.glutil.geom.Cube;
import org.miv.pherd.ParticleBoxListener;
import org.miv.pherd.ParticleBoxListenerProxy;
import org.miv.pherd.geom.Point3;
import org.miv.pherd.ntree.Anchor;
import org.miv.pherd.ntree.NTreeListener;
import org.miv.pherd.ntree.NTreeListenerProxy;
public class ParticleViewer extends JPanel implements ParticleBoxListener,
NTreeListener, BufferListener {
// Attributes
private static final long serialVersionUID = 1L;
/**
* Shared information.
*/
protected Context ctx;
/**
* Listener for the particles.
*/
protected ParticleBoxListenerProxy pboxProxy;
/**
* Listener for the n-tree.
*/
protected NTreeListenerProxy ntreeProxy;
/**
* Set of particles representations.
*/
protected HashMap<Object, GraphicParticle> particles;
/**
* The OpenGL canvas.
*/
protected Buffer buffer;
// View
protected float near = 1f;
protected float radius = 1.3f, theta, phi;
protected Point3 camera;
protected Point3 lookAt;
// Environment
protected Cube env;
protected Color background = new Color(255, 255, 255);
// Constructors
public ParticleViewer(Context ctx) {
this.ctx = ctx;
pboxProxy = new ParticleBoxListenerProxy(ctx.getPbox(), true);
ntreeProxy = new NTreeListenerProxy(ctx.getPbox().getNTree(), true);
particles = new HashMap<Object, GraphicParticle>();
buffer = new SwingBuffer(this, "Million boids !", 512, 512,
Buffer.OutputMode.CANVAS, true);
camera = new Point3(0, 0, -radius);
lookAt = new Point3(0, 0, 0);
env = new Cube((float) (ctx.getPbox().getNTree().getRootCell()
.getSpace().getHiAnchor().x - ctx.getPbox().getNTree()
.getRootCell().getSpace().getLoAnchor().x));
pboxProxy.addParticleBoxListener(this);
ntreeProxy.addNTreeListener(this);
setLayout(new BorderLayout());
add((java.awt.Component) buffer.getComponent(), BorderLayout.CENTER);
}
// Commands
/**
* Disconnect the view from the current particle box.
*/
public void disconnect() {
pboxProxy.disconnect(ctx.getPbox());
ntreeProxy.disconnect(ctx.getPbox().getNTree());
particles.clear();
}
/**
* Re-connect the view to the current particle box.
*/
public void reconnect() {
pboxProxy.connect(ctx.getPbox(), true);
ntreeProxy.connect(ctx.getPbox().getNTree(), true);
}
public void display() {
pboxProxy.checkEvents();
ntreeProxy.checkEvents();
buffer.display();
}
protected void setView() {
GL2 gl = buffer.getGl().getGL2();
GLU glu = buffer.getGlu();
gl.glClear(GL.GL_COLOR_BUFFER_BIT);
gl.glMatrixMode(GL2.GL_PROJECTION);
gl.glLoadIdentity();
gl.glFrustum(-ctx.area, ctx.area, -ctx.area, ctx.area, near, 10);
gl.glMatrixMode(GL2.GL_MODELVIEW);
gl.glLoadIdentity();
glu.gluLookAt(camera.x, camera.y, camera.z, lookAt.x, lookAt.y,
lookAt.z, 0, 1, 0);
}
protected void displayEnv() {
GL2 gl = buffer.getGl().getGL2();
gl.glPolygonMode(GL.GL_FRONT_AND_BACK, GL2.GL_LINE);
gl.glColor4f(0, 0, 0, 0.5f);
gl.glEnable(GL.GL_LINE_SMOOTH);
env.display(gl);
gl.glDisable(GL.GL_LINE_SMOOTH);
}
public void displayParticles() {
Iterator<? extends GraphicParticle> i = particles.values().iterator();
GL2 gl = (GL2)buffer.getGl();
while (i.hasNext()) {
GraphicParticle p = i.next();
p.display(gl);
}
}
// Particle box listener
public void particleAdded(Object id, double x, double y, double z) {
particles.put(id, new GraphicParticle(id, x, y, z));
}
public void particleAttributeChanged(Object id, String attribute,
Object newValue, boolean removed) {
GraphicParticle p = particles.get(id);
if (p != null) {
if (removed)
p.removeAttribute(attribute);
else
p.setAttribute(attribute, newValue);
}
}
public void particleMoved(Object id, double x, double y, double z) {
GraphicParticle p = particles.get(id);
if (p != null)
p.setPosition(x, y, z);
}
public void particleRemoved(Object id) {
particles.remove(id);
}
public void stepFinished(int time) {
}
// NTree Listener
public void cellAdded(Object id, Object parentId, Anchor lo, Anchor hi,
int depth, int index) {
}
public void cellData(Object id, String message, Object data) {
}
public void cellRemoved(Object id) {
}
// GL Event Listener
public void close(Buffer buffer) {
}
public void display(Buffer buffer) {
setView();
displayEnv();
displayParticles();
}
public void init(Buffer buffer) {
GL gl = buffer.getGl();
gl.glClearColor(background.getRed() / 255f,
background.getGreen() / 255f, background.getBlue() / 255f, 0);
gl.glClearDepth(255f);
gl.glEnable(GL.GL_BLEND);
gl.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA);
gl.glEnable(GL2.GL_POINT_SMOOTH);
}
public void key(Buffer buffer, int key, char unicode, boolean pressed) {
}
public void keyTyped(Buffer buffer, char unicode, int modifiers) {
}
public void mouse(Buffer buffer, int x, int y, int button) {
/*
* if( ctx.getMouse() != null ) { switch( button ) { case 0:
* ctx.getMouse().mouseMoved( x, y ); break; case 4:
* ctx.getMouse().decreaseFearFactor(); break; case 5:
* ctx.getMouse().increaseFearFactor(); break; } }
*/
}
public void reshape(Buffer buffer, int x, int y, int width, int height) {
}
}
\ No newline at end of file
package org.graphstream.glutil;
import java.io.IOException;
import javax.media.opengl.*;
import javax.media.opengl.glu.*;
/**
* Represents a pixel buffer.
*
* <p>
* A buffer is an addressable set of pixels associated with an output where
* OpenGL rendering can occur. The output makes the link between the abstract
* OGL frame buffer and a toolkit like AWT, Swing, QTJambi or SWT for example.
* </p>
*
* <p>
* The pixel buffer can be associated with a surface in the graphical toolkit
* that is part of a complete UI (the CANVAS mode), or be a window of its own
* (the WINDOW mode) or even be full screen (the FULLSCREEN mode).
* </p>
*
* @author Antoine Dutot
* @since 20040423
*/
public interface Buffer
{
// Constants
/**
* Output modes. These are the way the link is done between the GL and a
* toolkit output.
*/
public static enum OutputMode
{
/**
* Output to a canvas component.
*/
CANVAS,
/**
* Output to a canvas, this canvas will not capture any event.
*/
CANVAS_NO_EVENTS,
/**
* Output to a top-level window.
*/
WINDOW,
/**
* Output to a full-screen window.
*/
FULLSCREEN
}
/**
* Image formats supported by the {@link #dumpImage(String, DumpImageFormat, boolean)}
* method.
*/
public static enum DumpImageFormat
{
PNG,
JPG,
BMP
}
// Accessors
/**
* JOGL GL instance.
* @return A graphic context and a way to make calls to it.
*/
GL2 getGl();
/**
* JOGL GLU instance.
* @return A graphic context and a way to make calls to it.
*/
GLU getGlu();
/**
* Buffer width in pixels.
* @return The exact width of the buffer (not of a possible window around).
*/
int getWidth();
/**
* Buffer height in pixels.
* @return The exact height of the buffer (not of a possible window around).
*/
int getHeight();
/**
* Size of the memory used by this buffer in bytes.
* @return Sum of all the buffers bytes used.
*/
int getMemUsed();