From 4b34a788b281616b11c034e7e2e98d5fd6aad2dc Mon Sep 17 00:00:00 2001 From: Quentin Legot Date: Mon, 19 Apr 2021 11:50:33 +0200 Subject: [PATCH] improve example tab + improve camera when generate l system --- src/lsystem/screen/gl3d/AbstractCanvas.java | 1 + src/lsystem/screen/gl3d/GLEventListener.java | 4 +-- src/lsystem/screen/main/AbstractListener.java | 17 +++++++++ src/lsystem/screen/main/Example.java | 36 ++++++++++++++----- src/lsystem/screen/main/Listener.java | 9 +++-- src/lsystem/screen/main/MainFrame.java | 2 +- src/lsystem/screen/main/Tab.java | 2 +- 7 files changed, 53 insertions(+), 18 deletions(-) create mode 100644 src/lsystem/screen/main/AbstractListener.java diff --git a/src/lsystem/screen/gl3d/AbstractCanvas.java b/src/lsystem/screen/gl3d/AbstractCanvas.java index 71efea7..1f59441 100644 --- a/src/lsystem/screen/gl3d/AbstractCanvas.java +++ b/src/lsystem/screen/gl3d/AbstractCanvas.java @@ -65,6 +65,7 @@ public abstract class AbstractCanvas { // -XX:+UseG1GC to force jre 1.8 to use G1GC (G1GC is default on jre 11+), if you have jre 15+ you should use // ZGC which is very quick compared to G1GC and have a lower CPU usage System.gc(); + camera = new float[]{0f, 1f, 5f, 0f, 0f, 0f}; // reset default camera position } }); frame.getContentPane().add(glCanvas, BorderLayout.CENTER); diff --git a/src/lsystem/screen/gl3d/GLEventListener.java b/src/lsystem/screen/gl3d/GLEventListener.java index b0cad84..ce7d193 100644 --- a/src/lsystem/screen/gl3d/GLEventListener.java +++ b/src/lsystem/screen/gl3d/GLEventListener.java @@ -92,8 +92,8 @@ public class GLEventListener extends AbstractListener { if(element.property == ElementProperties.DRAW) { if(firstGen) { - canvas.camera[1] += 0.10f; - canvas.camera[2] += 0.10f; + canvas.camera[1] = (float) Math.min(50f, canvas.camera[1] + 0.10f-Math.sin(element.rotation[1])); + canvas.camera[2] = Math.min(50f, canvas.camera[2] + 0.10f); } glut.glutSolidCylinder(0.25f, 1f, 10, 10); gl.glTranslatef(0f, 0f, 1f); diff --git a/src/lsystem/screen/main/AbstractListener.java b/src/lsystem/screen/main/AbstractListener.java new file mode 100644 index 0000000..0ba993f --- /dev/null +++ b/src/lsystem/screen/main/AbstractListener.java @@ -0,0 +1,17 @@ +package lsystem.screen.main; + +import javax.swing.*; +import java.awt.*; +import java.awt.event.ActionListener; + +public abstract class AbstractListener implements ActionListener { + + protected final String type; + protected ImageIcon staticIcon = new ImageIcon(Toolkit.getDefaultToolkit().createImage(getClass().getClassLoader().getResource("./loading-gif.gif"))); + + public AbstractListener(String type) { + this.type = type; + } + + public abstract void openDialog(String message); +} diff --git a/src/lsystem/screen/main/Example.java b/src/lsystem/screen/main/Example.java index b2951f2..31b046f 100644 --- a/src/lsystem/screen/main/Example.java +++ b/src/lsystem/screen/main/Example.java @@ -1,37 +1,55 @@ package lsystem.screen.main; +import lsystem.engine.Parser; + import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; +import java.util.Arrays; +import java.util.List; public class Example extends JPanel { private final MainFrame frame; - + private final JButton a; + private final JButton b; public Example(MainFrame frame) { this.frame = frame; - JPanel list = new JPanel(); - list.setLayout(new GridBagLayout()); - JButton a = new JButton("{axiom=\"Y\",rules:[\"Y=X+[[Y]-Y]-X[-XY]+Y\", \"X=XX\"]}"); + a = new JButton("{axiom=\"Y\",rules:[\"Y=X+[[Y]-Y]-X[-XY]+Y\", \"X=XX\"]}"); a.addActionListener(new ExampleListener("a")); - JPanel aboveComponents = new JPanel(); + this.add(a); + b = new JButton("{axiom=\"X\",rules:[\"X=XY\",\"Y=X\"]}"); + b.addActionListener(new ExampleListener("b")); + this.add(b); + this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS)); } - private class ExampleListener implements ActionListener { - - private final String type; + private class ExampleListener extends AbstractListener { public ExampleListener(String type) { - this.type = type; + super(type); + } + + @Override + public void openDialog(String message) { + JOptionPane.showMessageDialog(null, message); + new ExampleListener("Clear"); } @Override public void actionPerformed(ActionEvent e) { + Parser parser; switch(type) { case "a": + parser = new Parser("Y", Arrays.asList("Y=X+[[Y]-Y]-X[-XY]+Y", "X=XX"), 5); + frame.generateLSystem(this, parser, a); + break; + case "b": + parser = new Parser("X", Arrays.asList("X=XY", "Y=X"), 5); + frame.generateLSystem(this, parser , b); } } diff --git a/src/lsystem/screen/main/Listener.java b/src/lsystem/screen/main/Listener.java index f261ca5..2845c56 100644 --- a/src/lsystem/screen/main/Listener.java +++ b/src/lsystem/screen/main/Listener.java @@ -7,20 +7,18 @@ import java.awt.*; import java.awt.event.*; import java.util.List; -public class Listener implements ActionListener, KeyListener, MouseWheelListener { +public class Listener extends AbstractListener implements KeyListener, MouseWheelListener { Tab tab; MainFrame frame; Integer index; - String type; Integer nbAxioms= 0; - ImageIcon staticIcon = new ImageIcon(Toolkit.getDefaultToolkit().createImage(getClass().getClassLoader().getResource("./loading-gif.gif"))); public Listener(MainFrame frame, Integer index, String type, Tab tab){ + super(type); this.tab = tab; this.frame = frame; this.index = index; - this.type = type; } @Override @@ -64,7 +62,8 @@ public class Listener implements ActionListener, KeyListener, MouseWheelListener } - void openDialog(String message) { + @Override + public void openDialog(String message) { JOptionPane.showMessageDialog(null, message); new Listener(null, index, "Clear", tab); } diff --git a/src/lsystem/screen/main/MainFrame.java b/src/lsystem/screen/main/MainFrame.java index e79b38f..101caeb 100644 --- a/src/lsystem/screen/main/MainFrame.java +++ b/src/lsystem/screen/main/MainFrame.java @@ -133,7 +133,7 @@ public class MainFrame extends JFrame { } } - public void generateLSystem(Listener listener, Parser parser, JButton submitButton) { + public void generateLSystem(AbstractListener listener, Parser parser, JButton submitButton) { if(Main.joglFrame.frame.isVisible()) { listener.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) { diff --git a/src/lsystem/screen/main/Tab.java b/src/lsystem/screen/main/Tab.java index 05f90eb..a564392 100644 --- a/src/lsystem/screen/main/Tab.java +++ b/src/lsystem/screen/main/Tab.java @@ -131,7 +131,7 @@ public class Tab extends JPanel{ /** * @return A string which contains the axiom entered by the user. */ - public String getAxiom(){ + public String getAxiom() { String str = axiomList.getText(); str = str.substring(10).replaceAll(";", ""); return str;