Change Example.java and Factorize Listener.java
This commit is contained in:
parent
4b34a788b2
commit
359d3eace9
@ -12,46 +12,42 @@ import java.util.List;
|
|||||||
public class Example extends JPanel {
|
public class Example extends JPanel {
|
||||||
|
|
||||||
private final MainFrame frame;
|
private final MainFrame frame;
|
||||||
private final JButton a;
|
public final JButton exemple1;
|
||||||
private final JButton b;
|
public final JButton exemple2;
|
||||||
public Example(MainFrame frame) {
|
public Example(MainFrame frame) {
|
||||||
|
|
||||||
|
|
||||||
this.frame = 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 JButton newButton(String str, String str2){
|
||||||
private class ExampleListener extends AbstractListener {
|
JButton res = new JButton(str);
|
||||||
|
res.addActionListener(new Listener(str2,frame,this));
|
||||||
public ExampleListener(String type) {
|
res.setBackground(Color.LIGHT_GRAY);
|
||||||
super(type);
|
return res;
|
||||||
}
|
|
||||||
|
|
||||||
@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);
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -5,21 +5,30 @@ import lsystem.engine.Parser;
|
|||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
import java.awt.event.*;
|
import java.awt.event.*;
|
||||||
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class Listener extends AbstractListener implements KeyListener, MouseWheelListener {
|
public class Listener extends AbstractListener implements KeyListener, MouseWheelListener {
|
||||||
Tab tab;
|
Tab tab;
|
||||||
|
Example ex;
|
||||||
MainFrame frame;
|
MainFrame frame;
|
||||||
Integer index;
|
|
||||||
Integer nbAxioms= 0;
|
Integer nbAxioms= 0;
|
||||||
|
|
||||||
|
public Listener(String type, MainFrame frame,Tab tab){
|
||||||
public Listener(MainFrame frame, Integer index, String type, Tab tab){
|
|
||||||
super(type);
|
super(type);
|
||||||
this.tab = tab;
|
|
||||||
this.frame = frame;
|
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
|
@Override
|
||||||
public void actionPerformed(ActionEvent e) {
|
public void actionPerformed(ActionEvent e) {
|
||||||
@ -29,7 +38,7 @@ public class Listener extends AbstractListener implements KeyListener, MouseWhee
|
|||||||
if (selected != null) {
|
if (selected != null) {
|
||||||
frame.tabs.remove(selected);
|
frame.tabs.remove(selected);
|
||||||
}
|
}
|
||||||
frame.decreaseTab();
|
frame.nbTabs-=1;
|
||||||
frame.renameTabs();
|
frame.renameTabs();
|
||||||
break;
|
break;
|
||||||
|
|
||||||
@ -38,11 +47,23 @@ public class Listener extends AbstractListener implements KeyListener, MouseWhee
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
case "Tab":
|
case "Tab":
|
||||||
frame.newTab();
|
frame.newComponent((byte) 1);
|
||||||
break;
|
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;
|
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":
|
case "Clear":
|
||||||
tab.getTextArea((byte) 0).setText("Axiome : \n");
|
tab.getTextArea((byte) 0).setText("Axiome : \n");
|
||||||
tab.getTextArea((byte) 1).setText("Règles : \n");
|
tab.getTextArea((byte) 1).setText("Règles : \n");
|
||||||
@ -65,7 +86,7 @@ public class Listener extends AbstractListener implements KeyListener, MouseWhee
|
|||||||
@Override
|
@Override
|
||||||
public void openDialog(String message) {
|
public void openDialog(String message) {
|
||||||
JOptionPane.showMessageDialog(null, message);
|
JOptionPane.showMessageDialog(null, message);
|
||||||
new Listener(null, index, "Clear", tab);
|
new Listener("Clear",null, tab);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -38,13 +38,13 @@ public class MainFrame extends JFrame {
|
|||||||
|
|
||||||
JToolBar toolBar = new JToolBar();
|
JToolBar toolBar = new JToolBar();
|
||||||
newGen = new JButton("Nouvelle génération");
|
newGen = new JButton("Nouvelle génération");
|
||||||
newGen.addActionListener(new Listener(this,null,"Tab",null));
|
newGen.addActionListener(new Listener("Tab",this));
|
||||||
toolBar.add(newGen);
|
toolBar.add(newGen);
|
||||||
example = new JButton("Exemples");
|
example = new JButton("Exemples");
|
||||||
example.addActionListener(new Listener(this, null, "example", null));
|
example.addActionListener(new Listener("Example", this));
|
||||||
toolBar.add(example);
|
toolBar.add(example);
|
||||||
help = new JButton("Aide");
|
help = new JButton("Aide");
|
||||||
help.addActionListener(new Listener(this,null,"Help",null));
|
help.addActionListener(new Listener("Help",this));
|
||||||
toolBar.add(help);
|
toolBar.add(help);
|
||||||
|
|
||||||
this.setTitle("L-system interface");
|
this.setTitle("L-system interface");
|
||||||
@ -55,16 +55,11 @@ public class MainFrame extends JFrame {
|
|||||||
this.add(tabs);
|
this.add(tabs);
|
||||||
this.add(toolBar, BorderLayout.NORTH);
|
this.add(toolBar, BorderLayout.NORTH);
|
||||||
this.setPreferredSize(windowDimension);
|
this.setPreferredSize(windowDimension);
|
||||||
newTab();
|
newComponent((byte)1);
|
||||||
renameTabs();
|
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).
|
* 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.
|
* Checks the name of each tab and change it considering their order.
|
||||||
*/
|
*/
|
||||||
public void renameTabs(){
|
public void renameTabs(){
|
||||||
for(int i =0;i<nbTabs;i++){
|
for(int i =0;i<nbTabs;i++){
|
||||||
tabs.setTitleAt(i,("Génération"+(i+1)));
|
if(!tabs.getTitleAt(i).equals("Exemples"))
|
||||||
|
tabs.setTitleAt(i,("Génération"+(i+1)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Call the Tab or the Exemple class to create a new JPanel that will be added to the tabs variable of this class (JTabbedPane).
|
||||||
|
*/
|
||||||
|
|
||||||
|
public void newComponent(byte i ){
|
||||||
|
if(nbTabs > 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) {
|
public void generateLSystem(AbstractListener listener, Parser parser, JButton submitButton) {
|
||||||
if(Main.joglFrame.frame.isVisible()) {
|
if(Main.joglFrame.frame.isVisible()) {
|
||||||
|
@ -23,26 +23,26 @@ public class Tab extends JPanel{
|
|||||||
JLabel itLabel = new JLabel("Nombre d'itérations : ");
|
JLabel itLabel = new JLabel("Nombre d'itérations : ");
|
||||||
itSpinner = new JSpinner(new SpinnerNumberModel(1, 1, 30, 1));
|
itSpinner = new JSpinner(new SpinnerNumberModel(1, 1, 30, 1));
|
||||||
((JSpinner.DefaultEditor) itSpinner.getEditor()).getTextField().setEditable(false);
|
((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 axiome = new JLabel("Axiome :");
|
||||||
JLabel rules = new JLabel("Règles :");
|
JLabel rules = new JLabel("Règles :");
|
||||||
|
|
||||||
axiomeField = new JTextField();
|
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));
|
axiomeField.setPreferredSize(new Dimension(120,20));
|
||||||
|
|
||||||
rulesField = new JTextField();
|
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));
|
rulesField.setPreferredSize(new Dimension(120,20));
|
||||||
submitButton = new JButton("Générer en 3D");
|
submitButton = new JButton("Générer en 3D");
|
||||||
JButton clearButton = new JButton("Clear");
|
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);
|
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);
|
submitButton.setBackground(Color.CYAN);
|
||||||
JButton close = new JButton("Close");
|
JButton close = new JButton("Close");
|
||||||
close.addActionListener(new Listener(frame,null,"Close",null));
|
close.addActionListener(new Listener("Close",frame));
|
||||||
close.setBackground(Color.RED);
|
close.setBackground(Color.RED);
|
||||||
JPanel southComponents = subPanel(submitButton,close,null);
|
JPanel southComponents = subPanel(submitButton,close,null);
|
||||||
southComponents = subPanel(clearButton, southComponents, null);
|
southComponents = subPanel(clearButton, southComponents, null);
|
||||||
|
Loading…
Reference in New Issue
Block a user