refactored windows

This commit is contained in:
Quentin Legot 2021-03-03 13:23:22 +01:00
parent 16df134cd4
commit bd48d32080
18 changed files with 144 additions and 339 deletions

View File

@ -1,14 +1,14 @@
package lsystem;
import lsystem.screen.MainFrame;
import lsystem.screen.SwingGLCanvas;
import lsystem.screen.gl3d.GLCanvas;
import lsystem.screen.main.MainFrame;
public class Main {
public static void main(String[] args) {
MainFrame frame = new MainFrame();
frame.setVisible(true);
SwingGLCanvas canvas = new SwingGLCanvas();
GLCanvas canvas = new GLCanvas();
canvas.setVisible(true);
}

View File

@ -1,8 +1,7 @@
package lsystem;
import lsystem.screen.MainFrame;
import lsystem.screen.SwingGLCanvas;
import lsystem.screen.SwingGLCanvas2D;
import lsystem.screen.gl2d.SwingGLCanvas2D;
import lsystem.screen.main.MainFrame;
public class Main2D {
@ -11,6 +10,7 @@ public class Main2D {
MainFrame frame = new MainFrame();
frame.setVisible(true);
SwingGLCanvas2D canvas = new SwingGLCanvas2D();
canvas.setVisible(true);
}

View File

@ -6,34 +6,27 @@ import com.jogamp.opengl.awt.GLCanvas;
import com.jogamp.opengl.glu.GLU;
import com.jogamp.opengl.util.FPSAnimator;
import com.jogamp.opengl.util.gl2.GLUT;
import lsystem.screen.listener.JoglEventListener;
import lsystem.screen.listener.JoglMouseListener;
import lsystem.screen.listener.KeyboardListener;
import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class SwingGLCanvas {
public abstract class AbstractCanvas {
public JFrame frame;
FPSAnimator animator;
protected FPSAnimator animator;
public final GLCanvas glCanvas;
public float[] camera = {0f, 1f, 5f, // camera pos x,y, z
0f, 0f, 0f}; // camera rotation yaw(x-axis), pitch(y-axis), roll(z-axis)
public GLU glu = new GLU();
public GLUT glut = new GLUT();
public float[] camera = {0f, 1f, 5f, // camera pos x,y, z
0f, 0f, 0f}; // camera rotation yaw(x-axis), pitch(y-axis), roll(z-axis)
public SwingGLCanvas() {
protected AbstractCanvas() {
GLProfile glProfile = GLProfile.getDefault();
GLCapabilities glCapabilities = new GLCapabilities(glProfile);
this.glCanvas = new GLCanvas(glCapabilities);
glCanvas.addGLEventListener(new JoglEventListener(this));
JoglMouseListener mouse = new JoglMouseListener(this);
glCanvas.addMouseListener(mouse);
glCanvas.addMouseMotionListener(mouse);
glCanvas.addMouseWheelListener(mouse);
glCanvas.addKeyListener(new KeyboardListener(this));
frame = new JFrame("L-System");
animator = new FPSAnimator(glCanvas, 60);
frame.addWindowListener(new WindowAdapter() {
@ -42,11 +35,13 @@ public class SwingGLCanvas {
frame.dispose();
}
});
frame.getContentPane().add(glCanvas, BorderLayout.CENTER);
frame.setSize(Constants.WIDTH, Constants.HEIGHT);
addEventsListeners();
}
protected abstract void addEventsListeners();
public void setVisible(boolean bl) {
if(bl)
animator.start();

View File

@ -1,116 +0,0 @@
package lsystem.screen;
import com.jogamp.opengl.GL;
import com.jogamp.opengl.GL2;
import com.jogamp.opengl.GLAutoDrawable;
import com.jogamp.opengl.GLEventListener;
import com.jogamp.opengl.awt.GLCanvas;
import com.jogamp.opengl.util.Animator;
import lsystem.screen.listener.JoglEventListener2D;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
/**
* Article1.java author: InfoRital
*
* Code source du premier article. Article1 implémente {@link GLEventListener}
* pour obtenir le mécanisme de callback
*
*/
public class Jogl implements GLEventListener {
public static void initialize(String[] args) {
// Création de la fenêtre
Frame frame = new Frame("L-système 3D");
// Création du canvas pour dessiner dessus
GLCanvas canvas = new GLCanvas();
// Nous attachons ensuite le méchanisme de callback à notre surface dessinable
canvas.addGLEventListener((GLEventListener) new Jogl());
// dessin -> fenêtre
frame.add(canvas);
// Création de l'animator
final Animator animator = new Animator(canvas);
// croix rouge = fermeture de la fenêtre
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
// Thread pour arrêter l'animator avant la fenêtre
new Thread(animator::stop).start();
}
});
// Taille de la fenêtre
frame.setSize(800, 800);
frame.setVisible(true);
// Démarrage de l'animator qui va se charger de faire des appels à la méthode
animator.start();
}
// init() sera appelée une fois au début de l'animation. C'est dans cette méthode
// que nous nous chargerons de toutes les opérations d'initialisation
public void init(GLAutoDrawable drawable) {
// GLEventListener renvoie un contexte (drawable)
// que nous allons utiliser pour instancier un objet de type GL
// qui nous permettra d'utiliser les fonctions OpenGL
GL gl = drawable.getGL();
// désactiver la synchronisation verticale indépendamment de la plateforme utilisée
gl.setSwapInterval(1);
}
// Appelée que si la fenêtre d'affichage est redimensionnée
public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
}
// display() sera appelée en boucle tout au long de l'application par la classe Animator.
// C'est dans cette fonction qu'on fera tout ce qui doit être affiché
public void display(GLAutoDrawable drawable) {
GL2 gl = drawable.getGL().getGL2();
float xDefault = -1.0f, yDefault = -1.0f;
/*gl.glBegin(GL2.GL_LINES);
gl.glVertex2f(xDefault, yDefault);
gl.glVertex2f(xDefault + (0.1f * 1), yDefault + (0.1f * 1));
gl.glEnd();
gl.glBegin(GL2.GL_LINES);
gl.glVertex2f(xDefault + (0.1f * 1), -1.1f);
gl.glVertex2f(xDefault + (0.1f * 1) + (0.2f*1), yDefault + (0.1f * 1) + (0.2f*-1));
gl.glEnd();*/
DrawHelper.drawStick(gl, 0.1f, xDefault, yDefault, 0);
DrawHelper.drawStick(gl, 0.2f, -0.9f, -0.9f, 90);
}
/**
* displayChanged() est appelée si le mode d'affichage par exemple est modifié.
*/
/*
* public void displayChanged(GLAutoDrawable drawable, boolean modeChanged,
* boolean deviceChanged) { }
*/
@Override
public void dispose(GLAutoDrawable arg0) {
// TODO Auto-generated method stub
}
public static void main(String[] args) {
//Jogl jogl = new Jogl();
//jogl.initialize(args);
SwingGLCanvas2D canvas = new SwingGLCanvas2D();
JoglEventListener2D a = new JoglEventListener2D(canvas);
}
}

View File

@ -1,53 +0,0 @@
package lsystem.screen;
import com.jogamp.opengl.GLCapabilities;
import com.jogamp.opengl.GLProfile;
import com.jogamp.opengl.awt.GLCanvas;
import com.jogamp.opengl.glu.GLU;
import com.jogamp.opengl.util.FPSAnimator;
import com.jogamp.opengl.util.gl2.GLUT;
import lsystem.screen.listener.JoglEventListener;
import lsystem.screen.listener.JoglEventListener2D;
import lsystem.screen.listener.JoglMouseListener;
import lsystem.screen.listener.JoglMouseListener2D;
import lsystem.screen.listener.KeyboardListener;
import lsystem.screen.listener.KeyboardListener2D;
import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class SwingGLCanvas2D {
public final GLCanvas glCanvas;
public float[] camera = {0f, 1f, 5f, // camera pos x,y, z
0f, 0f, 0f}; // camera rotation yaw(x-axis), pitch(y-axis), roll(z-axis)
public GLU glu = new GLU();
public GLUT glut = new GLUT();
public SwingGLCanvas2D() {
GLProfile glProfile = GLProfile.getDefault();
GLCapabilities glCapabilities = new GLCapabilities(glProfile);
this.glCanvas = new GLCanvas(glCapabilities);
glCanvas.addGLEventListener(new JoglEventListener2D(this));
JoglMouseListener2D mouse = new JoglMouseListener2D(this);
glCanvas.addMouseListener(mouse);
glCanvas.addMouseMotionListener(mouse);
glCanvas.addMouseWheelListener(mouse);
glCanvas.addKeyListener(new KeyboardListener2D(this));
final JFrame jframe = new JFrame("L-System");
final FPSAnimator animator = new FPSAnimator(glCanvas, 60);
jframe.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
jframe.dispose();
}
});
jframe.getContentPane().add(glCanvas, BorderLayout.CENTER);
jframe.setSize(Constants.WIDTH, Constants.HEIGHT);
animator.start();
jframe.setVisible(true);
}
}

View File

@ -0,0 +1,61 @@
package lsystem.screen.gl2d;
import com.jogamp.opengl.GL2;
import com.jogamp.opengl.GLAutoDrawable;
import com.jogamp.opengl.GLEventListener;
import com.jogamp.opengl.glu.GLU;
import com.jogamp.opengl.util.gl2.GLUT;
import lsystem.screen.gl3d.DrawHelper;
public class JoglEventListener2D implements GLEventListener {
private final SwingGLCanvas2D canvas;
private final GLU glu;
private final GLUT glut;
private int fps;
public JoglEventListener2D(SwingGLCanvas2D swingGLCanvas) {
this.canvas = swingGLCanvas;
this.glu = canvas.glu;
this.glut = canvas.glut;
}
@Override
public void init(GLAutoDrawable glAutoDrawable) {
GL2 gl = glAutoDrawable.getGL().getGL2();
gl.glClearColor(0f, 0f, 0f, 1.0f);
new Thread(() -> {
while (canvas.frame.isVisible()) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(fps);
fps = 0;
}
}).start();
}
@Override
public void dispose(GLAutoDrawable glAutoDrawable) {
}
@Override
public void display (GLAutoDrawable glAutoDrawable) {
GL2 gl = glAutoDrawable.getGL().getGL2();
float xDefault = -1.0f, yDefault = -1.0f;
DrawHelper.drawStick(gl, 0.1f, xDefault, yDefault, 0);
DrawHelper.drawStick(gl, 0.2f, -0.9f, -0.9f, 90);
}
@Override
public void reshape(GLAutoDrawable glAutoDrawable, int x, int y, int width, int height) {
}
}

View File

@ -1,8 +1,6 @@
package lsystem.screen.listener;
package lsystem.screen.gl2d;
import com.jogamp.opengl.glu.GLU;
import lsystem.screen.SwingGLCanvas;
import lsystem.screen.SwingGLCanvas2D;
import java.awt.*;
import java.awt.event.*;

View File

@ -1,7 +1,4 @@
package lsystem.screen.listener;
import lsystem.screen.SwingGLCanvas;
import lsystem.screen.SwingGLCanvas2D;
package lsystem.screen.gl2d;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

View File

@ -0,0 +1,21 @@
package lsystem.screen.gl2d;
import lsystem.screen.AbstractCanvas;
public class SwingGLCanvas2D extends AbstractCanvas {
public SwingGLCanvas2D() {
super();
}
@Override
protected void addEventsListeners() {
glCanvas.addGLEventListener(new JoglEventListener2D(this));
JoglMouseListener2D mouse = new JoglMouseListener2D(this);
glCanvas.addMouseListener(mouse);
glCanvas.addMouseMotionListener(mouse);
glCanvas.addMouseWheelListener(mouse);
glCanvas.addKeyListener(new KeyboardListener2D(this));
}
}

View File

@ -1,11 +1,12 @@
package lsystem.screen;
package lsystem.screen.gl3d;
import com.jogamp.opengl.GL2;
import com.jogamp.opengl.util.gl2.GLUT;
import lsystem.screen.gl2d.SwingGLCanvas2D;
public class DrawHelper {
public static void placeCamera(GL2 gl, SwingGLCanvas canvas) {
public static void placeCamera(GL2 gl, GLCanvas canvas) {
gl.glRotatef(canvas.camera[4], 1f, 0f, 0f);
gl.glRotatef(canvas.camera[3], 0f, 1f, 0f);
gl.glRotatef(canvas.camera[5], 0f, 0f, 1f);
@ -74,7 +75,7 @@ public class DrawHelper {
gl.glPopMatrix();
}
public static void drawDebugInformation(GL2 gl, GLUT glut, SwingGLCanvas canvas) {
public static void drawDebugInformation(GL2 gl, GLUT glut, GLCanvas canvas) {
gl.glRasterPos3f(canvas.camera[0], canvas.camera[1], canvas.camera[2] - 1);
gl.glColor3f(1f, 1f, 1f);
glut.glutBitmapString(GLUT.BITMAP_HELVETICA_18,

View File

@ -0,0 +1,22 @@
package lsystem.screen.gl3d;
import lsystem.screen.AbstractCanvas;
public class GLCanvas extends AbstractCanvas {
public GLCanvas() {
super();
}
@Override
protected void addEventsListeners() {
glCanvas.addGLEventListener(new GLEventListener(this));
GLMouseListener mouse = new GLMouseListener(this);
glCanvas.addMouseListener(mouse);
glCanvas.addMouseMotionListener(mouse);
glCanvas.addMouseWheelListener(mouse);
glCanvas.addKeyListener(new GLKeyboardListener(this));
}
}

View File

@ -1,18 +1,17 @@
package lsystem.screen.listener;
package lsystem.screen.gl3d;
import com.jogamp.opengl.*;
import com.jogamp.opengl.GL2;
import com.jogamp.opengl.GLAutoDrawable;
import com.jogamp.opengl.glu.GLU;
import com.jogamp.opengl.util.gl2.GLUT;
import lsystem.screen.DrawHelper;
import lsystem.screen.SwingGLCanvas;
import lsystem.utils.Pair;
import java.util.LinkedList;
import java.util.Random;
public class JoglEventListener implements GLEventListener {
public class GLEventListener implements com.jogamp.opengl.GLEventListener {
private final SwingGLCanvas canvas;
private final GLCanvas canvas;
private final float[] light_0_ambient = {0.01f, 0.01f, 0.01f, 0.01f};
private final float[] light_0_diffuse = {1.0f, 1.0f, 1.0f, 1.0f};
private final float[] light_0_specular = {1.0f,1.0f, 1.0f, 1.0f};
@ -27,7 +26,7 @@ public class JoglEventListener implements GLEventListener {
private final GLUT glut;
private int fps;
public JoglEventListener(SwingGLCanvas swingGLCanvas) {
public GLEventListener(GLCanvas swingGLCanvas) {
this.canvas = swingGLCanvas;
this.glu = canvas.glu;
this.glut = canvas.glut;

View File

@ -1,16 +1,14 @@
package lsystem.screen.listener;
import lsystem.screen.SwingGLCanvas;
package lsystem.screen.gl3d;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class KeyboardListener implements KeyListener {
public class GLKeyboardListener implements KeyListener {
private final SwingGLCanvas canvas;
private final GLCanvas canvas;
public KeyboardListener(SwingGLCanvas swingGLCanvas) {
public GLKeyboardListener(GLCanvas swingGLCanvas) {
this.canvas = swingGLCanvas;
}

View File

@ -1,19 +1,18 @@
package lsystem.screen.listener;
package lsystem.screen.gl3d;
import com.jogamp.opengl.glu.GLU;
import lsystem.screen.SwingGLCanvas;
import java.awt.*;
import java.awt.event.*;
public class JoglMouseListener implements MouseListener, MouseMotionListener, MouseWheelListener {
public class GLMouseListener implements MouseListener, MouseMotionListener, MouseWheelListener {
private final GLU glu;
private final SwingGLCanvas canvas;
private final GLCanvas canvas;
private int button = 0;
private Point origine;
public JoglMouseListener(SwingGLCanvas canvas) {
public GLMouseListener(GLCanvas canvas) {
this.canvas = canvas;
this.glu = canvas.glu;
}

View File

@ -1,113 +0,0 @@
package lsystem.screen.listener;
import com.jogamp.opengl.*;
import com.jogamp.opengl.glu.GLU;
import com.jogamp.opengl.util.gl2.GLUT;
import lsystem.screen.DrawHelper;
import lsystem.screen.SwingGLCanvas;
import lsystem.screen.SwingGLCanvas2D;
import lsystem.utils.Pair;
import java.util.LinkedList;
import java.util.Random;
public class JoglEventListener2D implements GLEventListener {
private final SwingGLCanvas2D canvas;
private final float[] light_0_ambient = {0.01f, 0.01f, 0.01f, 0.01f};
private final float[] light_0_diffuse = {1.0f, 1.0f, 1.0f, 1.0f};
private final float[] light_0_specular = {1.0f,1.0f, 1.0f, 1.0f};
private final float[] light_0_position = {1000f, 1000f, 1000f, 1f};
private final float[] material_specular = {0.8f, 0.8f, 0.8f, 0.8f};
private float angle = 0f;
private final LinkedList<Pair<Integer, Integer>> prismPosition = new LinkedList<>();
private final GLU glu;
private final GLUT glut;
private int fps;
public JoglEventListener2D(SwingGLCanvas2D swingGLCanvas) {
this.canvas = swingGLCanvas;
this.glu = canvas.glu;
this.glut = canvas.glut;
}
@Override
public void init(GLAutoDrawable glAutoDrawable) {
GL2 gl = glAutoDrawable.getGL().getGL2();
gl.glClearColor(0f, 0f, 0f, 1.0f);
gl.glEnable(GL2.GL_DEPTH_TEST);
gl.glEnable(GL2.GL_LIGHT0);
gl.glEnable(GL2.GL_LIGHTING);
gl.glEnable(GL2.GL_NORMALIZE);
gl.glEnable(GL2.GL_COLOR_MATERIAL);
gl.glClearDepth(1.0f);
gl.glShadeModel(GL2.GL_SMOOTH);
gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_AMBIENT, light_0_ambient, 0);
gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_DIFFUSE, light_0_diffuse, 0);
gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_SPECULAR, light_0_specular, 0);
gl.glDepthFunc(GL2.GL_LEQUAL);
gl.glHint(GL2.GL_PERSPECTIVE_CORRECTION_HINT, GL2.GL_NICEST);
for(int i = -50; i < 51; ++i) {
for(int j = -50; j < 51; ++j) {
if(new Random().nextFloat() < 0.05) {
prismPosition.add(new Pair<>(i, j));
}
}
}
System.out.println(prismPosition.size() * 8);
new Thread(() -> {
while (true) {
synchronized (this){
try {
wait(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(fps);
fps = 0;
}
}
}).start();
}
@Override
public void dispose(GLAutoDrawable glAutoDrawable) {
}
@Override
public void display (GLAutoDrawable glAutoDrawable) {
GL2 gl = glAutoDrawable.getGL().getGL2();
float xDefault = -1.0f, yDefault = -1.0f;
DrawHelper.drawStick(gl, 0.1f, xDefault, yDefault, 0);
DrawHelper.drawStick(gl, 0.2f, -0.9f, -0.9f, 90);
}
@Override
public void reshape(GLAutoDrawable glAutoDrawable, int x, int y, int width, int height) {
GL2 gl = glAutoDrawable.getGL().getGL2();
gl.glViewport(x, y, width, height);
gl.glMatrixMode(GL2.GL_PROJECTION);
gl.glLoadIdentity();
glu.gluPerspective(60.0f, (float) width / height, 0.1f, 1000.0f);
gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_POSITION, light_0_position, 0);
gl.glColorMaterial(GL2.GL_FRONT_AND_BACK, GL2.GL_AMBIENT_AND_DIFFUSE);
gl.glMateriali(GL2.GL_FRONT_AND_BACK, GL2.GL_SHININESS, 90);
gl.glMaterialfv(GL2.GL_FRONT_AND_BACK, GL2.GL_SPECULAR, material_specular, 0);
gl.glMatrixMode(GL2.GL_MODELVIEW);
}
}

View File

@ -1,10 +1,8 @@
package lsystem.screen.listener;
package lsystem.screen.main;
import lsystem.engine.Element;
import lsystem.engine.Parser;
import lsystem.engine.Rewrite;
import lsystem.screen.MainFrame;
import lsystem.screen.Tab;
import javax.swing.*;
import java.awt.event.ActionEvent;

View File

@ -1,7 +1,7 @@
package lsystem.screen;
package lsystem.screen.main;
import lsystem.screen.listener.Listener;
import lsystem.screen.Constants;
import javax.swing.*;
import java.awt.*;

View File

@ -1,6 +1,4 @@
package lsystem.screen;
import lsystem.screen.listener.Listener;
package lsystem.screen.main;
import javax.swing.*;
import java.awt.*;