From 359d3eace93be32af8e20cfb075369e1ea2a9779 Mon Sep 17 00:00:00 2001 From: Arthur <78031901+Arthur7770@users.noreply.github.com> Date: Mon, 19 Apr 2021 14:55:43 +0200 Subject: [PATCH] Change Example.java and Factorize Listener.java --- src/lsystem/screen/main/Example.java | 68 ++++++++++++-------------- src/lsystem/screen/main/Listener.java | 41 ++++++++++++---- src/lsystem/screen/main/MainFrame.java | 57 ++++++++++----------- src/lsystem/screen/main/Tab.java | 12 ++--- 4 files changed, 95 insertions(+), 83 deletions(-) diff --git a/src/lsystem/screen/main/Example.java b/src/lsystem/screen/main/Example.java index 31b046f..60175c5 100644 --- a/src/lsystem/screen/main/Example.java +++ b/src/lsystem/screen/main/Example.java @@ -12,46 +12,42 @@ import java.util.List; public class Example extends JPanel { private final MainFrame frame; - private final JButton a; - private final JButton b; + public final JButton exemple1; + public final JButton exemple2; public Example(MainFrame frame) { + + this.frame = frame; + this.setLayout(new GridBagLayout()); + GridBagConstraints gc = new GridBagConstraints(); + gc.gridx = 1; + gc.gridy = 0; + gc.weightx = 10; + gc.weighty = 5; + + JLabel text = new JLabel("Vous pouvez lancer le programme avec les deux exemples de simulation ci-dessous : "); + this.add(text,gc); + + gc.gridy = 1; + exemple1 = newButton("{axiom=\"Y\",rules:[\"Y=X+[[Y]-Y]-X[-XY]+Y\", \"X=XX\"]}","Example1"); + this.add(exemple1,gc); + + gc.gridy = 2; + exemple2 = newButton("{axiom=\"X\",rules:[\"X=XY\",\"Y=X\"]}","Example2"); + this.add(exemple2,gc); + + gc.gridy = 3; + JButton close = new JButton("Close"); + close.addActionListener(new Listener("Close",frame)); + close.setBackground(Color.RED); + this.add(close,gc); - a = new JButton("{axiom=\"Y\",rules:[\"Y=X+[[Y]-Y]-X[-XY]+Y\", \"X=XX\"]}"); - a.addActionListener(new ExampleListener("a")); - 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 extends AbstractListener { - - public ExampleListener(String 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); - - } - } + private JButton newButton(String str, String str2){ + JButton res = new JButton(str); + res.addActionListener(new Listener(str2,frame,this)); + res.setBackground(Color.LIGHT_GRAY); + return res; } } diff --git a/src/lsystem/screen/main/Listener.java b/src/lsystem/screen/main/Listener.java index 2845c56..cdb9379 100644 --- a/src/lsystem/screen/main/Listener.java +++ b/src/lsystem/screen/main/Listener.java @@ -5,21 +5,30 @@ import lsystem.engine.Parser; import javax.swing.*; import java.awt.*; import java.awt.event.*; +import java.util.Arrays; import java.util.List; public class Listener extends AbstractListener implements KeyListener, MouseWheelListener { Tab tab; + Example ex; MainFrame frame; - Integer index; Integer nbAxioms= 0; - - public Listener(MainFrame frame, Integer index, String type, Tab tab){ + public Listener(String type, MainFrame frame,Tab tab){ super(type); - this.tab = tab; this.frame = frame; - this.index = index; + this.tab = tab; } + public Listener(String type, MainFrame frame, Example ex){ + super(type); + this.frame = frame; + this.ex = ex; + } + public Listener(String type, MainFrame frame){ + super(type); + this.frame = frame; + } + @Override public void actionPerformed(ActionEvent e) { @@ -29,7 +38,7 @@ public class Listener extends AbstractListener implements KeyListener, MouseWhee if (selected != null) { frame.tabs.remove(selected); } - frame.decreaseTab(); + frame.nbTabs-=1; frame.renameTabs(); break; @@ -38,11 +47,23 @@ public class Listener extends AbstractListener implements KeyListener, MouseWhee break; case "Tab": - frame.newTab(); + frame.newComponent((byte) 1); break; - case "example": - frame.newExample(); + + case "Example1": + Parser parser = new Parser("Y", Arrays.asList("Y=X+[[Y]-Y]-X[-XY]+Y", "X=XX"), 5); + frame.generateLSystem(this, parser, ex.exemple1); break; + + case "Example2": + Parser parser1 = new Parser("X", Arrays.asList("X=XY", "Y=X"), 5); + frame.generateLSystem(this, parser1 , ex.exemple2); + break; + + case "Example": + frame.newComponent((byte)0); + break; + case "Clear": tab.getTextArea((byte) 0).setText("Axiome : \n"); tab.getTextArea((byte) 1).setText("Règles : \n"); @@ -65,7 +86,7 @@ public class Listener extends AbstractListener implements KeyListener, MouseWhee @Override public void openDialog(String message) { JOptionPane.showMessageDialog(null, message); - new Listener(null, index, "Clear", tab); + new Listener("Clear",null, tab); } @Override diff --git a/src/lsystem/screen/main/MainFrame.java b/src/lsystem/screen/main/MainFrame.java index 101caeb..0089b7a 100644 --- a/src/lsystem/screen/main/MainFrame.java +++ b/src/lsystem/screen/main/MainFrame.java @@ -38,13 +38,13 @@ public class MainFrame extends JFrame { JToolBar toolBar = new JToolBar(); newGen = new JButton("Nouvelle génération"); - newGen.addActionListener(new Listener(this,null,"Tab",null)); + newGen.addActionListener(new Listener("Tab",this)); toolBar.add(newGen); example = new JButton("Exemples"); - example.addActionListener(new Listener(this, null, "example", null)); + example.addActionListener(new Listener("Example", this)); toolBar.add(example); help = new JButton("Aide"); - help.addActionListener(new Listener(this,null,"Help",null)); + help.addActionListener(new Listener("Help",this)); toolBar.add(help); this.setTitle("L-system interface"); @@ -55,16 +55,11 @@ public class MainFrame extends JFrame { this.add(tabs); this.add(toolBar, BorderLayout.NORTH); this.setPreferredSize(windowDimension); - newTab(); + newComponent((byte)1); renameTabs(); + this.setResizable(false); } - /** - * Decrease the nbTabs variable by one. - */ - public void decreaseTab(){ - nbTabs -=1; - } /** * Create and display a new frame (and throws a message if one is already open) which contains a helping text (Uses the Constants class). @@ -102,36 +97,36 @@ public class MainFrame extends JFrame { } } - /** - * Call the Tab class to create a new JPanel that will be added to the tabs variable of this class (JTabbedPane). - */ - public void newTab() { - if(nbTabs>2) - JOptionPane.showMessageDialog(null, "Nombre maximal d'onglets atteintes"); - else { - nbTabs++; - tabs.addTab("Génération" + nbTabs,new Tab(this)); - } - - } /** * Checks the name of each tab and change it considering their order. */ public void renameTabs(){ for(int i =0;i 2) + JOptionPane.showMessageDialog(null, "Nombre maximal d'onglets atteint"); + else { + nbTabs++; + switch(i){ + case 1: + tabs.addTab("Génération" + nbTabs,new Tab(this)); + break; + case 0: + tabs.addTab("Exemples" ,new Example(this)); + break; + } } } - public void newExample() { - if(nbTabs > 2) - JOptionPane.showMessageDialog(null, "Nombre maximal d'onglets atteintes"); - else { - nbTabs++; - tabs.addTab("Exemples", new Example(this)); - } - } public void generateLSystem(AbstractListener listener, Parser parser, JButton submitButton) { if(Main.joglFrame.frame.isVisible()) { diff --git a/src/lsystem/screen/main/Tab.java b/src/lsystem/screen/main/Tab.java index a564392..73552a1 100644 --- a/src/lsystem/screen/main/Tab.java +++ b/src/lsystem/screen/main/Tab.java @@ -23,26 +23,26 @@ public class Tab extends JPanel{ JLabel itLabel = new JLabel("Nombre d'itérations : "); itSpinner = new JSpinner(new SpinnerNumberModel(1, 1, 30, 1)); ((JSpinner.DefaultEditor) itSpinner.getEditor()).getTextField().setEditable(false); - itSpinner.addMouseWheelListener(new Listener(null,null,"Spinner",this)); + itSpinner.addMouseWheelListener(new Listener("Spinner",null,this)); JLabel axiome = new JLabel("Axiome :"); JLabel rules = new JLabel("Règles :"); axiomeField = new JTextField(); - axiomeField.addKeyListener(new Listener(null,frame.nbTabs,"Axiome",this)); + axiomeField.addKeyListener(new Listener("Axiome",null,this)); axiomeField.setPreferredSize(new Dimension(120,20)); rulesField = new JTextField(); - rulesField.addKeyListener(new Listener(null,frame.nbTabs + 10,"Règles",this)); + rulesField.addKeyListener(new Listener("Règles",null,this)); rulesField.setPreferredSize(new Dimension(120,20)); submitButton = new JButton("Générer en 3D"); JButton clearButton = new JButton("Clear"); - clearButton.addActionListener(new Listener(null,frame.nbTabs,"Clear",this)); + clearButton.addActionListener(new Listener("Clear",null,this)); clearButton.setBackground(Color.GREEN); - submitButton.addActionListener(new Listener(frame, frame.nbTabs,"Generate 3D",this)); + submitButton.addActionListener(new Listener("Generate 3D",frame,this)); submitButton.setBackground(Color.CYAN); JButton close = new JButton("Close"); - close.addActionListener(new Listener(frame,null,"Close",null)); + close.addActionListener(new Listener("Close",frame)); close.setBackground(Color.RED); JPanel southComponents = subPanel(submitButton,close,null); southComponents = subPanel(clearButton, southComponents, null);