From d50b4f00429ae283b323cc2b80dab4e619bea71f Mon Sep 17 00:00:00 2001 From: Arthur7770 <78031901+Arthur7770@users.noreply.github.com> Date: Tue, 30 Mar 2021 16:04:09 +0200 Subject: [PATCH 1/5] Add a scroll-control on the spinner --- src/lsystem/screen/main/Listener.java | 17 +++++++++++------ src/lsystem/screen/main/Tab.java | 6 ++---- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/src/lsystem/screen/main/Listener.java b/src/lsystem/screen/main/Listener.java index 06e1975..275038d 100644 --- a/src/lsystem/screen/main/Listener.java +++ b/src/lsystem/screen/main/Listener.java @@ -7,13 +7,10 @@ import lsystem.utils.Pair; import javax.swing.*; import java.awt.*; -import java.awt.event.ActionEvent; -import java.awt.event.ActionListener; -import java.awt.event.KeyEvent; -import java.awt.event.KeyListener; +import java.awt.event.*; import java.util.List; -public class Listener implements ActionListener, KeyListener { +public class Listener implements ActionListener, KeyListener, MouseWheelListener { Tab tab; MainFrame frame; Integer index; @@ -36,7 +33,7 @@ public class Listener implements ActionListener, KeyListener { case "Close": frame.getTab().removeTabAt(index-1); frame.decreaseTab(); - for(int i = 0; i Date: Tue, 30 Mar 2021 16:30:20 +0200 Subject: [PATCH 2/5] Refactored GLEventListener.java and JoglEventListener2D.java + deleted useless 2d classes --- src/lsystem/screen/AbstractListener.java | 57 ++++++++++++ .../screen/gl2d/JoglEventListener2D.java | 55 ++---------- .../screen/gl2d/JoglMouseListener2D.java | 87 ------------------- .../screen/gl2d/KeyboardListener2D.java | 56 ------------ src/lsystem/screen/gl2d/SwingGLCanvas2D.java | 6 -- src/lsystem/screen/gl3d/DrawHelper.java | 26 ++---- src/lsystem/screen/gl3d/GLEventListener.java | 46 +++------- src/lsystem/screen/gl3d/GLMouseListener.java | 13 +-- 8 files changed, 84 insertions(+), 262 deletions(-) create mode 100644 src/lsystem/screen/AbstractListener.java delete mode 100644 src/lsystem/screen/gl2d/JoglMouseListener2D.java delete mode 100644 src/lsystem/screen/gl2d/KeyboardListener2D.java diff --git a/src/lsystem/screen/AbstractListener.java b/src/lsystem/screen/AbstractListener.java new file mode 100644 index 0000000..64a66cd --- /dev/null +++ b/src/lsystem/screen/AbstractListener.java @@ -0,0 +1,57 @@ +package lsystem.screen; + +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.engine.Element; + +public abstract class AbstractListener implements GLEventListener { + + protected final AbstractCanvas canvas; + protected final GLU glu; + protected final GLUT glut; + protected int fps; + + public AbstractListener(AbstractCanvas 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 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); + } + + public abstract void drawLSystem(GL2 gl, Element element); +} diff --git a/src/lsystem/screen/gl2d/JoglEventListener2D.java b/src/lsystem/screen/gl2d/JoglEventListener2D.java index a42750b..c88facf 100644 --- a/src/lsystem/screen/gl2d/JoglEventListener2D.java +++ b/src/lsystem/screen/gl2d/JoglEventListener2D.java @@ -2,51 +2,24 @@ 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.engine.Element; import lsystem.engine.ElementProperties; +import lsystem.screen.AbstractListener; 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 class JoglEventListener2D extends AbstractListener { public JoglEventListener2D(SwingGLCanvas2D swingGLCanvas) { - this.canvas = swingGLCanvas; - this.glu = canvas.glu; - this.glut = canvas.glut; + super(swingGLCanvas); } @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(); + public void drawLSystem(GL2 gl, Element element) { + drawAll(gl, element, new lsystem.screen.gl2d.Point2(-1.0f, -1.0f)); } - @Override - public void dispose(GLAutoDrawable glAutoDrawable) { - } - - public void drawAll(GL2 gl, Element actual, lsystem.screen.gl2d.Point2 origin) { + private void drawAll(GL2 gl, Element actual, lsystem.screen.gl2d.Point2 origin) { if (actual.property == ElementProperties.DRAW) { System.out.println("DESSIN"); lsystem.screen.gl2d.Point2 newOrigin = new lsystem.screen.gl2d.Point2(origin, actual.getRotation2D()); @@ -61,25 +34,11 @@ public class JoglEventListener2D implements GLEventListener { @Override public void display(GLAutoDrawable glAutoDrawable) { - /* - * Element str = new Element(ElementProperties.DRAW, null, new int[]{0, 0, 0}); - * - * Element child1 = new Element(ElementProperties.DRAW, str, new int[]{0, 0, - * 0}); Element child2 = new Element(ElementProperties.DRAW, str, new int[]{45, - * 0, 0}); str.children.add(child1); str.children.add(child2); Element child11 = - * new Element(ElementProperties.DRAW, child1, new int[]{225, 0, 0}); Element - * child12 = new Element(ElementProperties.DRAW, child1, new int[]{270, 0, 0}); - * child1.children.add(child11); child1.children.add(child12); - */ GL2 gl = glAutoDrawable.getGL().getGL2(); + drawLSystem(gl, canvas.getLSystem()); drawAll(gl, canvas.getLSystem(), new lsystem.screen.gl2d.Point2(-1.0f, -1.0f)); } - - @Override - public void reshape(GLAutoDrawable glAutoDrawable, int x, int y, int width, int height) { - - } } diff --git a/src/lsystem/screen/gl2d/JoglMouseListener2D.java b/src/lsystem/screen/gl2d/JoglMouseListener2D.java deleted file mode 100644 index 0fdc4de..0000000 --- a/src/lsystem/screen/gl2d/JoglMouseListener2D.java +++ /dev/null @@ -1,87 +0,0 @@ -package lsystem.screen.gl2d; - -import com.jogamp.opengl.glu.GLU; - -import java.awt.*; -import java.awt.event.*; - -public class JoglMouseListener2D implements MouseListener, MouseMotionListener, MouseWheelListener { - - private final GLU glu; - private final SwingGLCanvas2D canvas; - private int button = 0; - private Point origine; - - public JoglMouseListener2D(SwingGLCanvas2D canvas) { - this.canvas = canvas; - this.glu = canvas.glu; - } - - @Override - public void mouseClicked(MouseEvent e) { - } - - @Override - public void mousePressed(MouseEvent e) { - if(button == 0) { - button = e.getButton(); - origine = e.getPoint(); - } else { - button = 0; - origine = null; - } - } - - @Override - public void mouseReleased(MouseEvent e) { - button = 0; - } - - @Override - public void mouseEntered(MouseEvent e) { - } - - @Override - public void mouseExited(MouseEvent e) { - System.out.println("exited " + canvas.camera[0] + ", " + canvas.camera[1]); - } - - @Override - public void mouseDragged(MouseEvent e) { - if(origine != null) { - double xDiff = origine.getX() - e.getPoint().getX(); - double yDiff = origine.getY() - e.getPoint().getY(); - /* if(button == 2) { - canvas.camera[0] += Math.cos(canvas.camera[3]) * xDiff * 0.01; - canvas.camera[1] += Math.cos(canvas.camera[4]) * yDiff * 0.01; - canvas.camera[2] += Math.sin(canvas.camera[3]) * xDiff * 0.01; - } */ - if(button == 3) { - canvas.camera[3] += xDiff * 0.1; - canvas.camera[4] += yDiff * 0.1; - } - origine = e.getPoint(); - } - for (int i = 0; i < canvas.camera.length; i++) { - canvas.camera[i] = canvas.camera[i] % 360; - } - } - - @Override - public void mouseMoved(MouseEvent e) { - } - - @Override - public void mouseWheelMoved(MouseWheelEvent e) { - if( 45 > Math.abs(canvas.camera[3]) && 45>Math.abs(canvas.camera[4])) - canvas.camera[0] += e.getWheelRotation() * Math.tan(Math.toRadians(-canvas.camera[3])) * 0.25; - else - canvas.camera[0] += e.getWheelRotation() * Math.tan(Math.toRadians(canvas.camera[3])) * 0.25; - canvas.camera[1] += e.getWheelRotation() * Math.tan(Math.toRadians(canvas.camera[4])) * 0.25; - if( 45 < Math.abs(canvas.camera[3]) || 45 < Math.abs(canvas.camera[4])) - canvas.camera[2] += -e.getWheelRotation()*0.25; - else - canvas.camera[2] += e.getWheelRotation()*0.25; - - } -} diff --git a/src/lsystem/screen/gl2d/KeyboardListener2D.java b/src/lsystem/screen/gl2d/KeyboardListener2D.java deleted file mode 100644 index 7989eb9..0000000 --- a/src/lsystem/screen/gl2d/KeyboardListener2D.java +++ /dev/null @@ -1,56 +0,0 @@ -package lsystem.screen.gl2d; - -import java.awt.event.KeyEvent; -import java.awt.event.KeyListener; - -public class KeyboardListener2D implements KeyListener { - - private final SwingGLCanvas2D canvas; - - - public KeyboardListener2D(SwingGLCanvas2D swingGLCanvas) { - this.canvas = swingGLCanvas; - } - - @Override - public void keyTyped(KeyEvent e) { - - switch (e.getKeyChar()) { - case 'z': - canvas.camera[2] -= 0.1f; - break; - case 's': - canvas.camera[2] += 0.1f; - break; - case 'q': - canvas.camera[0] -= 0.1f; - break; - case 'd': - canvas.camera[0] += 0.1f; - break; - case 'a': - canvas.camera[3] -= 1; - break; - case 'e': - canvas.camera[3] += 1; - break; - case 'w': - canvas.camera[1] += 0.1f; - break; - case 'x': - canvas.camera[1] -= 0.1f; - break; - } - } - - - @Override - public void keyPressed(KeyEvent e) { - - } - - @Override - public void keyReleased(KeyEvent e) { - - } -} diff --git a/src/lsystem/screen/gl2d/SwingGLCanvas2D.java b/src/lsystem/screen/gl2d/SwingGLCanvas2D.java index faa981e..9c93afc 100644 --- a/src/lsystem/screen/gl2d/SwingGLCanvas2D.java +++ b/src/lsystem/screen/gl2d/SwingGLCanvas2D.java @@ -7,12 +7,6 @@ public class SwingGLCanvas2D extends AbstractCanvas { @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)); } } diff --git a/src/lsystem/screen/gl3d/DrawHelper.java b/src/lsystem/screen/gl3d/DrawHelper.java index 840525b..fa22d94 100644 --- a/src/lsystem/screen/gl3d/DrawHelper.java +++ b/src/lsystem/screen/gl3d/DrawHelper.java @@ -2,18 +2,12 @@ package lsystem.screen.gl3d; import com.jogamp.opengl.GL2; import com.jogamp.opengl.util.gl2.GLUT; +import lsystem.screen.AbstractCanvas; import lsystem.screen.gl2d.Point2; -import lsystem.screen.gl2d.SwingGLCanvas2D; public class DrawHelper { - 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); - } - - public static void placeCamera(GL2 gl, SwingGLCanvas2D canvas) { + public static void placeCamera(GL2 gl, AbstractCanvas 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); @@ -86,15 +80,7 @@ public class DrawHelper { gl.glPopMatrix(); } - 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, - "x=" + canvas.camera[0] + ", y=" + canvas.camera[1] + ", z=" + canvas.camera[2] + "\n yaw = " - + canvas.camera[3] + " pitch = " + canvas.camera[4] + " roll = " + canvas.camera[5]); - } - - public static void drawDebugInformation(GL2 gl, GLUT glut, SwingGLCanvas2D canvas) { + public static void drawDebugInformation(GL2 gl, GLUT glut, AbstractCanvas canvas) { gl.glRasterPos3f(canvas.camera[0], canvas.camera[1], canvas.camera[2] - 1); gl.glColor3f(1f, 1f, 1f); glut.glutBitmapString(GLUT.BITMAP_HELVETICA_18, @@ -103,13 +89,13 @@ public class DrawHelper { } public static void drawStick(GL2 gl, Point2 origin, Point2 newOrigin) { - + gl.glBegin(GL2.GL_LINES); gl.glVertex2f(origin.getX(), origin.getY()); gl.glVertex2f(newOrigin.getX(), newOrigin.getY()); gl.glEnd(); } - - + + } diff --git a/src/lsystem/screen/gl3d/GLEventListener.java b/src/lsystem/screen/gl3d/GLEventListener.java index f336efc..3ae258b 100644 --- a/src/lsystem/screen/gl3d/GLEventListener.java +++ b/src/lsystem/screen/gl3d/GLEventListener.java @@ -2,24 +2,18 @@ package lsystem.screen.gl3d; 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.engine.Element; import lsystem.engine.ElementProperties; import lsystem.screen.Constants; +import lsystem.screen.AbstractListener; -public class GLEventListener implements com.jogamp.opengl.GLEventListener { +public class GLEventListener extends AbstractListener { - private final GLCanvas canvas; private final float[] light_0_position = {1000f, 1000f, 1000f, 1f}; - private final GLU glu; - private final GLUT glut; - private int fps; + public GLEventListener(GLCanvas swingGLCanvas) { - this.canvas = swingGLCanvas; - this.glu = canvas.glu; - this.glut = canvas.glut; + super(swingGLCanvas); } @@ -43,21 +37,7 @@ public class GLEventListener implements com.jogamp.opengl.GLEventListener { gl.glDepthFunc(GL2.GL_LEQUAL); gl.glHint(GL2.GL_PERSPECTIVE_CORRECTION_HINT, GL2.GL_NICEST); - 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) { + super.init(glAutoDrawable); } @Override @@ -74,7 +54,7 @@ public class GLEventListener implements com.jogamp.opengl.GLEventListener { gl.glPushMatrix(); gl.glRotatef(90f, -1f, 0f, 0f); gl.glColor3f(0f, 1f, 0f); - displayLSystem(gl, glut, canvas.getLSystem()); + drawLSystem(gl, canvas.getLSystem()); gl.glPopMatrix(); DrawHelper.drawAxes(gl, glut); @@ -82,8 +62,8 @@ public class GLEventListener implements com.jogamp.opengl.GLEventListener { gl.glFlush(); fps++; } - - private void displayLSystem(GL2 gl, GLUT glut, Element element) { + @Override + public void drawLSystem(GL2 gl, Element element) { gl.glPushMatrix(); gl.glRotatef(element.rotation[0] * 360, 1f, 0f, 0f); gl.glRotatef(element.rotation[1] * 360, 0f, 1f, 0f); @@ -93,25 +73,19 @@ public class GLEventListener implements com.jogamp.opengl.GLEventListener { if(element.property == ElementProperties.DRAW) { glut.glutSolidCylinder(0.25f, 1f, 10, 10); gl.glTranslatef(0f, 0f, 1f); - //gl.glTranslated(Math.cos(element.rotation[0] * Math.PI), Math.sin(element.rotation[1] * Math.PI), Math.cos(element.rotation[2] * Math.PI)); } for(Element child : element.children) { - displayLSystem(gl, glut, child); + drawLSystem(gl, child); } gl.glPopMatrix(); } @Override public void reshape(GLAutoDrawable glAutoDrawable, int x, int y, int width, int height) { + super.reshape(glAutoDrawable, x, y, width, 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); diff --git a/src/lsystem/screen/gl3d/GLMouseListener.java b/src/lsystem/screen/gl3d/GLMouseListener.java index 964f708..3291e63 100644 --- a/src/lsystem/screen/gl3d/GLMouseListener.java +++ b/src/lsystem/screen/gl3d/GLMouseListener.java @@ -1,14 +1,16 @@ package lsystem.screen.gl3d; import com.jogamp.opengl.glu.GLU; +import lsystem.screen.AbstractCanvas; import java.awt.*; import java.awt.event.*; public class GLMouseListener implements MouseListener, MouseMotionListener, MouseWheelListener { - private final GLU glu; - private final GLCanvas canvas; + protected final GLU glu; + protected final GLCanvas canvas; + private int button = 0; private Point origine; private final float MULTIPLIER = 0.25f; @@ -45,7 +47,6 @@ public class GLMouseListener implements MouseListener, MouseMotionListener, Mous @Override public void mouseExited(MouseEvent e) { - System.out.println("exited " + canvas.camera[0] + ", " + canvas.camera[1]); } @Override @@ -53,11 +54,6 @@ public class GLMouseListener implements MouseListener, MouseMotionListener, Mous if(origine != null) { double xDiff = origine.getX() - e.getPoint().getX(); double yDiff = origine.getY() - e.getPoint().getY(); - /* if(button == 1) { - canvas.camera[0] += Math.cos(canvas.camera[3]) * xDiff * 0.01; - canvas.camera[1] += Math.cos(canvas.camera[4]) * yDiff * 0.01; - canvas.camera[2] += Math.sin(canvas.camera[3]) * xDiff * 0.01; - } */ if(button == 3) { canvas.camera[3] += xDiff * 0.1; canvas.camera[4] += yDiff * 0.1; @@ -75,6 +71,5 @@ public class GLMouseListener implements MouseListener, MouseMotionListener, Mous canvas.camera[2] += Math.cos(Math.toRadians(canvas.camera[4]))*MULTIPLIER* e.getWheelRotation()*Math.cos(Math.toRadians(Math.abs(360-canvas.camera[3]))); canvas.camera[0] -= Math.cos(Math.toRadians(canvas.camera[4]))*MULTIPLIER* e.getWheelRotation()*Math.cos(Math.toRadians(Math.abs(450-canvas.camera[3]))); canvas.camera[1] += (1-Math.cos(Math.toRadians(canvas.camera[4])))*MULTIPLIER* e.getWheelRotation()*Math.cos(Math.toRadians(Math.abs(360-canvas.camera[4]))); - } } From a83126e5850859529b8647482e5dbbc9fad9cf53 Mon Sep 17 00:00:00 2001 From: Antonin Boyon Date: Tue, 30 Mar 2021 16:34:17 +0200 Subject: [PATCH 3/5] Button for 2d and 3d generation --- src/lsystem/screen/main/Listener.java | 70 +++++++++++++++++++++------ src/lsystem/screen/main/Tab.java | 13 +++-- 2 files changed, 63 insertions(+), 20 deletions(-) diff --git a/src/lsystem/screen/main/Listener.java b/src/lsystem/screen/main/Listener.java index 275038d..261583f 100644 --- a/src/lsystem/screen/main/Listener.java +++ b/src/lsystem/screen/main/Listener.java @@ -52,28 +52,28 @@ public class Listener implements ActionListener, KeyListener, MouseWheelListener Listener kl = (Listener) tab.getTextField((byte) 0).getKeyListeners()[0]; kl.resetNbAxioms(); - break; - - case "Generate": - String axiom = tab.getAxiom(); - List rules = tab.getRules(); - Parser parser = new Parser(axiom, rules, tab.getNbIterations()); + break; + + case "Generate 2D": + String axiom2D = tab.getAxiom(); + List rules2D = tab.getRules(); + Parser parser2D = new Parser(axiom2D, rules2D, tab.getNbIterations()); if(Main.joglFrame.frame.isVisible()) { - openDialog("Veuillez fermer la fenêtre 3D avant de lancer une nouvelle génération"); + openDialog("Veuillez fermer la fenêtre 2D ou 3D avant de lancer une nouvelle génération"); } else if(Main.joglFrame.parsedState == AbstractCanvas.State.LOAD) { openDialog("Une génération est actuellement en cours, impossible d'en relancer un autre"); openDialog("Une génération est actuellement en cours, impossible d'en relancer un autre"); - } else if (!parser.isCorrect()) { + } else if (!parser2D.isCorrect()) { openDialog("Vos règles ou votre axiome ne sont pas correctement écrites, veuillez recommencer"); } else { - tab.submitButton.setIcon(staticIcon); - tab.submitButton.setText(""); + tab.submitButton2D.setIcon(staticIcon); + tab.submitButton2D.setText(""); parserThread = new Thread(() -> { try { - List> lSystemRules = parser.parseRules(); - Main.joglFrame.setLSystem(axiom, lSystemRules, tab.getNbIterations()); + List> lSystemRules = parser2D.parseRules(); + Main.joglFrame.setLSystem(axiom2D, lSystemRules, tab.getNbIterations()); - StringBuilder message = new StringBuilder("L-System 3D - {axiom:\"").append(axiom).append("\",rules:["); + StringBuilder message = new StringBuilder("L-System 2D - {axiom:\"").append(axiom2D).append("\",rules:["); for(int i = 0; i < lSystemRules.size(); ++i) { Pair rule = lSystemRules.get(i); message.append("\"").append(rule.getLeft()).append("=").append(rule.getRight()).append("\""); @@ -87,8 +87,48 @@ public class Listener implements ActionListener, KeyListener, MouseWheelListener Main.joglFrame.parsedState = AbstractCanvas.State.FINISH_OR_NULL; openDialog("Une erreur de type " + err.getClass().getSimpleName() + " est survenue lors de l'execution du parser: " + err.getMessage()); } - tab.submitButton.setIcon(null); - tab.submitButton.setText("Générer"); + tab.submitButton2D.setIcon(null); + tab.submitButton2D.setText("Générer"); + }); + parserThread.start(); + } + break; + + case "Generate 3D": + String axiom3D = tab.getAxiom(); + List rules3D = tab.getRules(); + Parser parser3D = new Parser(axiom3D, rules3D, tab.getNbIterations()); + if(Main.joglFrame.frame.isVisible()) { + openDialog("Veuillez fermer la fenêtre 2D ou 3D avant de lancer une nouvelle génération"); + } else if(Main.joglFrame.parsedState == AbstractCanvas.State.LOAD) { + openDialog("Une génération est actuellement en cours, impossible d'en relancer un autre"); + openDialog("Une génération est actuellement en cours, impossible d'en relancer un autre"); + } else if (!parser3D.isCorrect()) { + openDialog("Vos règles ou votre axiome ne sont pas correctement écrites, veuillez recommencer"); + } else { + tab.submitButton2D.setIcon(staticIcon); + tab.submitButton2D.setText(""); + parserThread = new Thread(() -> { + try { + List> lSystemRules = parser3D.parseRules(); + Main.joglFrame.setLSystem(axiom3D, lSystemRules, tab.getNbIterations()); + + StringBuilder message = new StringBuilder("L-System 3D - {axiom:\"").append(axiom3D).append("\",rules:["); + for(int i = 0; i < lSystemRules.size(); ++i) { + Pair rule = lSystemRules.get(i); + message.append("\"").append(rule.getLeft()).append("=").append(rule.getRight()).append("\""); + if(i + 1 != lSystemRules.size()) + message.append(","); + } + Main.joglFrame.frame.setTitle(message.append("]} - Nombres d'itérations: ").append(tab.getNbIterations()).toString()); + + Main.joglFrame.setVisible(true); + } catch (NumberFormatException err) { + Main.joglFrame.parsedState = AbstractCanvas.State.FINISH_OR_NULL; + openDialog("Une erreur de type " + err.getClass().getSimpleName() + " est survenue lors de l'execution du parser: " + err.getMessage()); + } + tab.submitButton3D.setIcon(null); + tab.submitButton3D.setText("Générer"); }); parserThread.start(); } diff --git a/src/lsystem/screen/main/Tab.java b/src/lsystem/screen/main/Tab.java index 9f4c28d..7f9e8bf 100644 --- a/src/lsystem/screen/main/Tab.java +++ b/src/lsystem/screen/main/Tab.java @@ -11,7 +11,7 @@ public class Tab extends JPanel{ public JSpinner itSpinner; JTextField axiomeField,rulesField; JTextArea axiomList,rulesList;; - JButton submitButton, close; + JButton submitButton2D, submitButton3D, close; public Tab(int nbTabs,int nbRules,JTabbedPane tabs,MainFrame frame) { this.nbRules = nbRules; @@ -38,11 +38,14 @@ public class Tab extends JPanel{ rulesField.addKeyListener(new Listener(null,nbTabs+10,"Règles",this)); rulesField.setPreferredSize(new Dimension(120,20)); - submitButton = new JButton("Générer"); + submitButton2D = new JButton("Générer en 2D"); + submitButton3D = new JButton("Générer en 3D"); JButton clearButton = new JButton("Clear"); clearButton.addActionListener(new Listener(null,nbTabs,"Clear",this)); - submitButton.addActionListener(new Listener(null,nbTabs,"Generate",this)); - JPanel southComponents = subPanel(clearButton,submitButton,null); + submitButton2D.addActionListener(new Listener(null,nbTabs,"Generate 2D",this)); + submitButton3D.addActionListener(new Listener(null,nbTabs,"Generate 3D",this)); + JPanel southComponents = subPanel(submitButton2D,submitButton3D, null); + JPanel southComponents2 = subPanel(clearButton, southComponents, null); GridBagConstraints gc = new GridBagConstraints(); gc.gridx = 0; @@ -64,7 +67,7 @@ public class Tab extends JPanel{ aboveComponents.setLayout(new GridLayout(1,4)); tab.add(aboveComponents); - tab.add(southComponents); + tab.add(southComponents2); tab.setLayout(new BoxLayout(tab,1)); close = new JButton("Close"); From 76727bd28c48f0b5911ce97ff60f70b87ae44ea1 Mon Sep 17 00:00:00 2001 From: Antonin Boyon Date: Tue, 30 Mar 2021 17:30:08 +0200 Subject: [PATCH 4/5] fixing button error --- src/lsystem/screen/main/Listener.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/lsystem/screen/main/Listener.java b/src/lsystem/screen/main/Listener.java index 261583f..ec17441 100644 --- a/src/lsystem/screen/main/Listener.java +++ b/src/lsystem/screen/main/Listener.java @@ -88,7 +88,7 @@ public class Listener implements ActionListener, KeyListener, MouseWheelListener openDialog("Une erreur de type " + err.getClass().getSimpleName() + " est survenue lors de l'execution du parser: " + err.getMessage()); } tab.submitButton2D.setIcon(null); - tab.submitButton2D.setText("Générer"); + tab.submitButton2D.setText("Générer en 2D"); }); parserThread.start(); } @@ -128,7 +128,7 @@ public class Listener implements ActionListener, KeyListener, MouseWheelListener openDialog("Une erreur de type " + err.getClass().getSimpleName() + " est survenue lors de l'execution du parser: " + err.getMessage()); } tab.submitButton3D.setIcon(null); - tab.submitButton3D.setText("Générer"); + tab.submitButton3D.setText("Générer en 3D"); }); parserThread.start(); } From 6f7ef0a33c4e870a4519d5c7948dd633c42007a2 Mon Sep 17 00:00:00 2001 From: Quentin Legot Date: Tue, 30 Mar 2021 17:37:40 +0200 Subject: [PATCH 5/5] Fix button gif not disappear --- src/lsystem/screen/gl3d/DrawHelper.java | 11 ++++++----- src/lsystem/screen/main/Listener.java | 4 ++-- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/src/lsystem/screen/gl3d/DrawHelper.java b/src/lsystem/screen/gl3d/DrawHelper.java index fa22d94..71c1bc0 100644 --- a/src/lsystem/screen/gl3d/DrawHelper.java +++ b/src/lsystem/screen/gl3d/DrawHelper.java @@ -27,12 +27,13 @@ public class DrawHelper { gl.glVertex3f(0f, 0f, 1f); gl.glColor3f(255f, 255f, 255f); - for (int i = -20; i < 21; i++) { - gl.glVertex3f(-20f, 0f, i); - gl.glVertex3f(20f, 0f, i); + int limit = 20; + for (int i = -limit; i < limit + 1; i++) { + gl.glVertex3f(-limit, 0f, i); + gl.glVertex3f(limit, 0f, i); - gl.glVertex3f(i, 0, -20f); - gl.glVertex3f(i, 0, 20f); + gl.glVertex3f(i, 0, -limit); + gl.glVertex3f(i, 0, limit); } gl.glEnd(); gl.glRasterPos3f(1.1f, 0.0f, 0.0f); diff --git a/src/lsystem/screen/main/Listener.java b/src/lsystem/screen/main/Listener.java index ec17441..9b02dc6 100644 --- a/src/lsystem/screen/main/Listener.java +++ b/src/lsystem/screen/main/Listener.java @@ -106,8 +106,8 @@ public class Listener implements ActionListener, KeyListener, MouseWheelListener } else if (!parser3D.isCorrect()) { openDialog("Vos règles ou votre axiome ne sont pas correctement écrites, veuillez recommencer"); } else { - tab.submitButton2D.setIcon(staticIcon); - tab.submitButton2D.setText(""); + tab.submitButton3D.setIcon(staticIcon); + tab.submitButton3D.setText(""); parserThread = new Thread(() -> { try { List> lSystemRules = parser3D.parseRules();