From a168e308be57ccfc27ab1bf05286bb6836b79c0c Mon Sep 17 00:00:00 2001 From: Arthur <78031901+Arthur7770@users.noreply.github.com> Date: Tue, 16 Feb 2021 16:38:17 +0100 Subject: [PATCH 1/5] Adding some changes --- src/lsystem/screen/MainFrame.java | 17 +++++++++++------ src/lsystem/screen/Tab.java | 11 +++++++++-- .../screen/listener/AxiomFieldListener.java | 10 +++++++--- 3 files changed, 27 insertions(+), 11 deletions(-) diff --git a/src/lsystem/screen/MainFrame.java b/src/lsystem/screen/MainFrame.java index 260f8b1..09c8577 100644 --- a/src/lsystem/screen/MainFrame.java +++ b/src/lsystem/screen/MainFrame.java @@ -21,10 +21,12 @@ public class MainFrame extends JFrame { private JButton newGen; private JButton help; private int nbRules; - public HashMap componentList; + public HashMap textAreaList; + public HashMap textFieldList; public MainFrame(){ - componentList = new HashMap<>(); + textAreaList = new HashMap<>(); + textFieldList = new HashMap<>(); nbRules = 1; nbTabs = 0; basePanel = new JPanel(); @@ -47,8 +49,11 @@ public class MainFrame extends JFrame { this.add(toolBar, BorderLayout.NORTH); this.setPreferredSize(new Dimension(640,600)); } - public void addToComponentList(Component c ,Integer i){ - componentList.put(i,c); + public void addToTextAreaList(JTextArea c ,Integer i){ + textAreaList.put(i,c); + } + public void addToTextFieldList(JTextField c ,Integer i){ + textFieldList.put(i,c); } public void newHelp() { @@ -66,7 +71,7 @@ public class MainFrame extends JFrame { public void closeTab() { //TODO : Pour fermer un onglet, nécessite l'implémentation d'un button fermer grâce à la méthode newTab(). } - public void changeList(Character stringToAdd, byte messageType, JTextArea list) { + public void changeList(String stringToAdd, byte messageType, JTextArea list) { if (stringToAdd == null) { switch(messageType) { case 0: @@ -77,7 +82,7 @@ public class MainFrame extends JFrame { throw new IllegalArgumentException("Wrong argument given to method changeList"); } }else{ - list.append(String.valueOf(stringToAdd)); + list.append(stringToAdd); } } public String getAxiom(){ diff --git a/src/lsystem/screen/Tab.java b/src/lsystem/screen/Tab.java index 1058263..1fae1a9 100644 --- a/src/lsystem/screen/Tab.java +++ b/src/lsystem/screen/Tab.java @@ -1,5 +1,7 @@ package lsystem.screen; +import lsystem.screen.listener.AxiomFieldListener; + import javax.swing.*; import java.awt.*; import javax.swing.JTabbedPane; @@ -12,22 +14,27 @@ public class Tab { JTextArea axiomList = new JTextArea(); axiomList.setText("Axiome : \n"); axiomList.setEditable(false); + frame.addToTextAreaList(axiomList,nbTabs); JTextArea rulesList = new JTextArea(); rulesList.setText("Règles : \n"); rulesList.setEditable(false); + frame.addToTextAreaList(rulesList,nbTabs+10); JButton closeButton = new JButton("x"); JLabel axiome = new JLabel("Axiome"); JLabel rules = new JLabel("Règle "+ nbRules); + JTextField axiomeField = new JTextField(); - frame.addToComponentList(axiomeField,nbTabs); + axiomeField.addKeyListener(new AxiomFieldListener(frame,nbTabs)); axiomeField.setPreferredSize(new Dimension(20,20)); + frame.addToTextFieldList(axiomeField,nbTabs); + JTextField rulesField = new JTextField(); - frame.addToComponentList(rulesField,nbTabs+10); rulesField.setPreferredSize(new Dimension(20,20)); + tab.add(axiome); tab.add(axiomeField); tab.add(axiomList); diff --git a/src/lsystem/screen/listener/AxiomFieldListener.java b/src/lsystem/screen/listener/AxiomFieldListener.java index e343261..7cc60b3 100644 --- a/src/lsystem/screen/listener/AxiomFieldListener.java +++ b/src/lsystem/screen/listener/AxiomFieldListener.java @@ -19,16 +19,20 @@ public class AxiomFieldListener implements KeyListener { } @Override public void keyTyped(KeyEvent ke) { - frame.changeList(ke.getKeyChar(),(byte)0, (JTextArea) frame.componentList.get(index)); + if(ke.getKeyCode() != 10) + frame.changeList(String.valueOf(ke.getKeyChar()),(byte)0, (JTextArea) frame.textAreaList.get(index)); + else{ + String str = ";\n"; + frame.changeList(str,(byte)0, (JTextArea) frame.textAreaList.get(index)); + frame.textFieldList.get(index).setText(""); + } } @Override public void keyPressed(KeyEvent ke) { - } @Override public void keyReleased(KeyEvent ke) { - } } From 74dd97fc449f432f06f7bbb16b79d31bb8dce9da Mon Sep 17 00:00:00 2001 From: Eideen Date: Tue, 16 Feb 2021 17:12:02 +0100 Subject: [PATCH 2/5] Add a Help window --- src/lsystem/screen/MainFrame.java | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/src/lsystem/screen/MainFrame.java b/src/lsystem/screen/MainFrame.java index 09c8577..daa18b3 100644 --- a/src/lsystem/screen/MainFrame.java +++ b/src/lsystem/screen/MainFrame.java @@ -32,7 +32,7 @@ public class MainFrame extends JFrame { basePanel = new JPanel(); basePanel.setLayout(new FlowLayout(FlowLayout.CENTER)); tabs = new JTabbedPane(SwingConstants.TOP); - + JToolBar toolBar = new JToolBar(); newGen = new JButton("Nouvelle génération"); newGen.addActionListener(new NewGenListener(this)); @@ -40,8 +40,8 @@ public class MainFrame extends JFrame { help = new JButton("Aide"); help.addActionListener(new HelpListener(this)); toolBar.add(help); - - this.setTitle("L-system interface"); + + this.setTitle("L-system interface"); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setSize(Constants.WIDTH, Constants.HEIGHT); this.setLocationRelativeTo(null); @@ -57,7 +57,18 @@ public class MainFrame extends JFrame { } public void newHelp() { - new Help(tabs); + JFrame aide = new JFrame(); + aide.setTitle("Aide"); + aide.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); + aide.setSize(700,800); + aide.setLocationRelativeTo(null); + aide.setVisible(true); + + JTextArea helpText = new JTextArea(); + helpText.setText(Constants.HELP); + helpText.setEditable(false); + + aide.add(helpText); } public void newTab() { if(nbTabs>2) @@ -80,7 +91,7 @@ public class MainFrame extends JFrame { list.setText("Règles : \n"); default: throw new IllegalArgumentException("Wrong argument given to method changeList"); - } + } }else{ list.append(stringToAdd); } @@ -111,4 +122,3 @@ public class MainFrame extends JFrame { } } - From 0c0d5ffc53f0b19333f998d6c1b4db82ee9b0e19 Mon Sep 17 00:00:00 2001 From: Eideen Date: Tue, 16 Feb 2021 17:21:38 +0100 Subject: [PATCH 3/5] Delete Help.java --- src/lsystem/screen/Help.java | 15 --------------- 1 file changed, 15 deletions(-) delete mode 100644 src/lsystem/screen/Help.java diff --git a/src/lsystem/screen/Help.java b/src/lsystem/screen/Help.java deleted file mode 100644 index 5eba14a..0000000 --- a/src/lsystem/screen/Help.java +++ /dev/null @@ -1,15 +0,0 @@ -package lsystem.screen; - -import javax.swing.*; - -public class Help { - public Help(JTabbedPane tabs){ - JPanel helpTab = new JPanel(); - JTextArea helpText = new JTextArea(); - helpText.setText(Constants.HELP); - helpText.setEditable(false); - helpTab.add(helpText); - tabs.addTab("Aide",(new JScrollPane(helpTab))); - - } -} From 785d0ac429aacd166f392d3542619fa693f76e22 Mon Sep 17 00:00:00 2001 From: Quentin Legot Date: Tue, 16 Feb 2021 17:40:29 +0100 Subject: [PATCH 4/5] refactored jogl --- src/lsystem/screen/Jogl.java | 98 ----------------------- src/lsystem/screen/JoglEventListener.java | 89 ++++++++++++++++++++ src/lsystem/screen/SwingGLCanvas.java | 34 ++++++++ 3 files changed, 123 insertions(+), 98 deletions(-) delete mode 100644 src/lsystem/screen/Jogl.java create mode 100644 src/lsystem/screen/JoglEventListener.java create mode 100644 src/lsystem/screen/SwingGLCanvas.java diff --git a/src/lsystem/screen/Jogl.java b/src/lsystem/screen/Jogl.java deleted file mode 100644 index ba10d63..0000000 --- a/src/lsystem/screen/Jogl.java +++ /dev/null @@ -1,98 +0,0 @@ -package lsystem.screen; - -import java.awt.*; -import java.awt.event.*; - -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; - -/** - * 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(300, 300); - 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) { - GL gl = drawable.getGL(); - - } - - // 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) { - - GL gl = drawable.getGL(); - ((GL2) gl).glBegin( GL2.GL_LINES ); - ((GL2) gl).glVertex3f( -0.75f,0f,3f );// 3 units into the window - ((GL2) gl).glVertex3f( 0f,-0.75f,3f ); - ((GL2) gl).glEnd(); - } - - /** - * displayChanged() est appelée si le mode d'affichage par exemple est modifié. - * Cependant nous n'implémenterons pas cette méthode. - */ - /* - * public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, - * boolean deviceChanged) { } - */ - - @Override - public void dispose(GLAutoDrawable arg0) { - // TODO Auto-generated method stub - - } - -} diff --git a/src/lsystem/screen/JoglEventListener.java b/src/lsystem/screen/JoglEventListener.java new file mode 100644 index 0000000..53ca84d --- /dev/null +++ b/src/lsystem/screen/JoglEventListener.java @@ -0,0 +1,89 @@ +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.glu.GLU; +import com.jogamp.opengl.util.gl2.GLUT; + +public class JoglEventListener implements GLEventListener { + + private GLU glu = new GLU(); + private GLUT glut = new GLUT(); + + private float camera [] = {0f, 0f, 5f}; + + private float [] light_0_ambient = {0.01f, 0.01f, 0.01f, 0.01f}; + private float [] light_0_diffuse = {1.0f, 1.0f, 1.0f, 1.0f}; + private float [] light_0_specular = {1.0f,1.0f, 1.0f, 1.0f}; + private float [] light_0_position = {100f, 0f, 10f, 1f}; + + private float [] material_specular = {1.0f, 1.0f, 1.0f, 1.0f}; + + private float angle = 0f; + + + @Override + public void init(GLAutoDrawable glAutoDrawable) { + GL2 gl = glAutoDrawable.getGL().getGL2(); + + gl.glClearColor(0.8f, 0.5f, 0.0f, 1.0f); + + gl.glEnable(GL2.GL_DEPTH_TEST); + gl.glClearDepth(1.0f); + + gl.glShadeModel(GL2.GL_SMOOTH); + gl.glEnable(GL2.GL_LIGHTING); + + gl.glEnable(GL2.GL_LIGHT0); + 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.glLightfv(GL2.GL_LIGHT0, GL2.GL_POSITION, light_0_position, 0); + + gl.glEnable(GL2.GL_COLOR_MATERIAL); + 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); + + } + + @Override + public void dispose(GLAutoDrawable glAutoDrawable) { + + } + + @Override + public void display(GLAutoDrawable glAutoDrawable) { + GL2 gl = glAutoDrawable.getGL().getGL2(); + gl.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT); + gl.glLoadIdentity(); + + glu.gluLookAt( + camera[0], camera[1], camera[2], + 0.0f, 0.0f, 0.0f, + 0.0f, 1.0f, 0.0f + ); + + gl.glRotatef(angle, 0f, 1f, 0f); + + gl.glColor3f(1.0f, 0.0f, 0.0f); + glut.glutSolidSphere(1.0f, 20, 20); + + angle += 0.1f; + angle %= 360f; + } + + @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, 10.0f); + + gl.glMatrixMode(GL2.GL_MODELVIEW); + } +} diff --git a/src/lsystem/screen/SwingGLCanvas.java b/src/lsystem/screen/SwingGLCanvas.java new file mode 100644 index 0000000..98b5d1a --- /dev/null +++ b/src/lsystem/screen/SwingGLCanvas.java @@ -0,0 +1,34 @@ +package lsystem.screen; + +import com.jogamp.opengl.GLCapabilities; +import com.jogamp.opengl.GLProfile; +import com.jogamp.opengl.awt.GLCanvas; +import com.jogamp.opengl.util.FPSAnimator; + +import javax.swing.*; +import java.awt.*; +import java.awt.event.WindowAdapter; +import java.awt.event.WindowEvent; + +public class SwingGLCanvas { + + public SwingGLCanvas() { + GLProfile glProfile = GLProfile.getDefault(); + GLCapabilities glCapabilities = new GLCapabilities(glProfile); + final GLCanvas glCanvas = new GLCanvas(glCapabilities); + glCanvas.addGLEventListener(new JoglEventListener()); + 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); + } +} From e32cfa6fe6bf7f2f3bb44381dc3ef147812e2659 Mon Sep 17 00:00:00 2001 From: Arthur <78031901+Arthur7770@users.noreply.github.com> Date: Tue, 16 Feb 2021 17:58:26 +0100 Subject: [PATCH 5/5] Adding some changes on the AxiomFieldListener --- src/lsystem/screen/listener/AxiomFieldListener.java | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/lsystem/screen/listener/AxiomFieldListener.java b/src/lsystem/screen/listener/AxiomFieldListener.java index 7cc60b3..2a88999 100644 --- a/src/lsystem/screen/listener/AxiomFieldListener.java +++ b/src/lsystem/screen/listener/AxiomFieldListener.java @@ -19,17 +19,19 @@ public class AxiomFieldListener implements KeyListener { } @Override public void keyTyped(KeyEvent ke) { - if(ke.getKeyCode() != 10) + if(ke.getKeyCode() != KeyEvent.VK_ENTER) frame.changeList(String.valueOf(ke.getKeyChar()),(byte)0, (JTextArea) frame.textAreaList.get(index)); else{ - String str = ";\n"; + String str = ";\r\n"; frame.changeList(str,(byte)0, (JTextArea) frame.textAreaList.get(index)); - frame.textFieldList.get(index).setText(""); + frame.textFieldList.get(index).setText(null); } } @Override public void keyPressed(KeyEvent ke) { + if(ke.getKeyCode() == KeyEvent.VK_ENTER) + frame.textFieldList.get(index).setText(null); } @Override