Moving MainFrame methods to Tab and remove useless HashMaps, Implements nbIterations modification
This commit is contained in:
parent
f4fe768cd8
commit
0d4d8c3169
@ -21,12 +21,10 @@ public class MainFrame extends JFrame {
|
|||||||
private JButton newGen;
|
private JButton newGen;
|
||||||
private JButton help;
|
private JButton help;
|
||||||
private int nbRules;
|
private int nbRules;
|
||||||
public HashMap<Integer,JTextArea> textAreaList;
|
|
||||||
public HashMap<Integer,JTextField> textFieldList;
|
|
||||||
|
|
||||||
public MainFrame(){
|
public MainFrame(){
|
||||||
textAreaList = new HashMap<>();
|
|
||||||
textFieldList = new HashMap<>();
|
|
||||||
nbRules = 1;
|
nbRules = 1;
|
||||||
nbTabs = 0;
|
nbTabs = 0;
|
||||||
basePanel = new JPanel();
|
basePanel = new JPanel();
|
||||||
@ -35,10 +33,10 @@ 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"));
|
newGen.addActionListener(new Listener(this,null,"Tab",null));
|
||||||
toolBar.add(newGen);
|
toolBar.add(newGen);
|
||||||
help = new JButton("Aide");
|
help = new JButton("Aide");
|
||||||
help.addActionListener(new Listener(this,null,"Help"));
|
help.addActionListener(new Listener(this,null,"Help",null));
|
||||||
toolBar.add(help);
|
toolBar.add(help);
|
||||||
|
|
||||||
this.setTitle("L-system interface");
|
this.setTitle("L-system interface");
|
||||||
@ -49,12 +47,6 @@ public class MainFrame extends JFrame {
|
|||||||
this.add(toolBar, BorderLayout.NORTH);
|
this.add(toolBar, BorderLayout.NORTH);
|
||||||
this.setPreferredSize(new Dimension(640,600));
|
this.setPreferredSize(new Dimension(640,600));
|
||||||
}
|
}
|
||||||
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() {
|
public void newHelp() {
|
||||||
JFrame aide = new JFrame();
|
JFrame aide = new JFrame();
|
||||||
@ -75,38 +67,15 @@ public class MainFrame extends JFrame {
|
|||||||
JOptionPane.showMessageDialog(null, "Nombre maximal de générations atteintes");
|
JOptionPane.showMessageDialog(null, "Nombre maximal de générations atteintes");
|
||||||
else {
|
else {
|
||||||
nbTabs++;
|
nbTabs++;
|
||||||
new Tab(this, nbTabs, nbRules, tabs);
|
new Tab(nbTabs, nbRules, tabs);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
public void closeTab() {
|
public void closeTab() {
|
||||||
//TODO : Pour fermer un onglet, nécessite l'implémentation d'un button fermer grâce à la méthode newTab().
|
//TODO : Pour fermer un onglet, nécessite l'implémentation d'un button fermer grâce à la méthode newTab().
|
||||||
}
|
}
|
||||||
public void changeList(String stringToAdd, JTextArea list,int nbAxioms) {
|
|
||||||
if(nbAxioms>0)
|
|
||||||
JOptionPane.showMessageDialog(null, "Nombre maximal d'axiomes créés");
|
|
||||||
else {
|
|
||||||
list.append(stringToAdd);
|
|
||||||
if (stringToAdd == ";")
|
|
||||||
nbAxioms++;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
public String getAxiom(int index){
|
|
||||||
String str = textAreaList.get(index).getText();
|
|
||||||
str = str.substring(10,str.length());
|
|
||||||
return str;
|
|
||||||
}
|
|
||||||
public List<String> getRules(int index){
|
|
||||||
List<String> list = new ArrayList<>();
|
|
||||||
String str = textAreaList.get(index).getText();
|
|
||||||
str = str.substring(10,str.length());
|
|
||||||
String[] strsplit = str.split("\n");
|
|
||||||
for(int y = 0;y<strsplit.length;y++){
|
|
||||||
list.add(strsplit[y]);
|
|
||||||
}
|
|
||||||
return list;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
@ -4,43 +4,52 @@ import lsystem.screen.listener.Listener;
|
|||||||
|
|
||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
import javax.swing.JTabbedPane;
|
import javax.swing.JTabbedPane;
|
||||||
|
|
||||||
public class Tab {
|
public class Tab extends JPanel{
|
||||||
MainFrame frame;
|
|
||||||
int nbTabs;
|
int nbTabs;
|
||||||
int nbRules;
|
int nbRules;
|
||||||
|
JSpinner nbIterations;
|
||||||
|
JTextField axiomeField;
|
||||||
|
JTextField rulesField;
|
||||||
|
JTextArea axiomList;
|
||||||
|
JTextArea rulesList;
|
||||||
|
JSpinner itSpinner;
|
||||||
|
|
||||||
public Tab(MainFrame frame,int nbTabs,int nbRules,JTabbedPane tabs) {
|
public Tab(int nbTabs,int nbRules,JTabbedPane tabs) {
|
||||||
this.frame = frame;
|
|
||||||
this.nbRules = nbRules;
|
this.nbRules = nbRules;
|
||||||
this.nbTabs = nbTabs;
|
this.nbTabs = nbTabs;
|
||||||
|
|
||||||
JPanel tab = new JPanel();
|
JPanel tab = new JPanel();
|
||||||
|
|
||||||
JTextArea axiomList = textArea("Axiome : \n",nbTabs);
|
axiomList = textArea("Axiome : \n",nbTabs);
|
||||||
JTextArea rulesList = textArea("Règles : \n",nbTabs+10);
|
rulesList = textArea("Règles : \n",nbTabs+10);
|
||||||
|
|
||||||
|
|
||||||
|
nbIterations = new JSpinner();
|
||||||
|
nbIterations.setModel(new SpinnerNumberModel(1, 1, 15, 1));
|
||||||
|
|
||||||
|
JLabel itLabel = new JLabel("Nombre d'itérations : ");
|
||||||
|
itSpinner = new JSpinner(new SpinnerNumberModel(1, 1, 15, 1));
|
||||||
|
((JSpinner.DefaultEditor) itSpinner.getEditor()).getTextField().setEditable(false);
|
||||||
|
|
||||||
JLabel axiome = new JLabel("Axiome :");
|
JLabel axiome = new JLabel("Axiome :");
|
||||||
JLabel rules = new JLabel("Règle "+ nbRules+" :");
|
JLabel rules = new JLabel("Règle "+ nbRules+" :");
|
||||||
|
|
||||||
JTextField axiomeField = new JTextField();
|
axiomeField = new JTextField();
|
||||||
axiomeField.addKeyListener(new Listener(frame,nbTabs,"Axiome"));
|
axiomeField.addKeyListener(new Listener(null,nbTabs,"Axiome",this));
|
||||||
axiomeField.setPreferredSize(new Dimension(120,20));
|
axiomeField.setPreferredSize(new Dimension(120,20));
|
||||||
frame.addToTextFieldList(axiomeField,nbTabs);
|
|
||||||
|
|
||||||
JTextField rulesField = new JTextField();
|
rulesField = new JTextField();
|
||||||
rulesField.addKeyListener(new Listener(frame,nbTabs+10,"Règles"));
|
rulesField.addKeyListener(new Listener(null,nbTabs+10,"Règles",this));
|
||||||
rulesField.setPreferredSize(new Dimension(120,20));
|
rulesField.setPreferredSize(new Dimension(120,20));
|
||||||
frame.addToTextFieldList(rulesField,nbTabs+10);
|
|
||||||
|
|
||||||
JButton submitButton = new JButton("Générer");
|
JButton submitButton = new JButton("Générer");
|
||||||
JButton clearButton = new JButton("Clear");
|
JButton clearButton = new JButton("Clear");
|
||||||
clearButton.addActionListener(new Listener(frame,nbTabs,"Clear"));
|
clearButton.addActionListener(new Listener(null,nbTabs,"Clear",this));
|
||||||
submitButton.addActionListener(new Listener(frame,nbTabs,"Generate"));
|
submitButton.addActionListener(new Listener(null,nbTabs,"Generate",this));
|
||||||
JPanel southComponents = subPanel(clearButton,submitButton,null);
|
JPanel southComponents = subPanel(clearButton,submitButton,null);
|
||||||
|
|
||||||
GridBagConstraints gc = new GridBagConstraints();
|
GridBagConstraints gc = new GridBagConstraints();
|
||||||
@ -49,17 +58,19 @@ public class Tab {
|
|||||||
gc.weightx = 0;
|
gc.weightx = 0;
|
||||||
gc.weighty = 0.5;
|
gc.weighty = 0.5;
|
||||||
|
|
||||||
JPanel eastComponents = subPanel(axiomList,rulesList,gc);
|
JPanel lists = subPanel(axiomList,rulesList,gc);
|
||||||
JPanel centerComponents = subPanel(axiomeField,rulesField,gc);
|
JPanel fields = subPanel(axiomeField,rulesField,gc);
|
||||||
JPanel westComponents = subPanel(axiome,rules,gc);
|
JPanel labels = subPanel(axiome,rules,gc);
|
||||||
|
|
||||||
|
JPanel Iterations = subPanel(itLabel,itSpinner,gc);
|
||||||
|
|
||||||
JPanel aboveComponents = new JPanel();
|
JPanel aboveComponents = new JPanel();
|
||||||
aboveComponents.add(westComponents);
|
aboveComponents.add(labels);
|
||||||
aboveComponents.add(centerComponents);
|
aboveComponents.add(fields);
|
||||||
aboveComponents.add(eastComponents);
|
aboveComponents.add(lists);
|
||||||
|
aboveComponents.add(Iterations);
|
||||||
|
|
||||||
aboveComponents.setLayout(new GridLayout(1,3));
|
aboveComponents.setLayout(new GridLayout(1,4));
|
||||||
tab.add(aboveComponents);
|
tab.add(aboveComponents);
|
||||||
tab.add(southComponents);
|
tab.add(southComponents);
|
||||||
tab.setLayout(new BoxLayout(tab,1));
|
tab.setLayout(new BoxLayout(tab,1));
|
||||||
@ -71,7 +82,7 @@ public class Tab {
|
|||||||
res.setText(texte);
|
res.setText(texte);
|
||||||
res.setEditable(false);
|
res.setEditable(false);
|
||||||
res.setPreferredSize(new Dimension(120,100));
|
res.setPreferredSize(new Dimension(120,100));
|
||||||
frame.addToTextAreaList(res,nb);
|
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
public JPanel subPanel(Component a, Component b,GridBagConstraints gc){
|
public JPanel subPanel(Component a, Component b,GridBagConstraints gc){
|
||||||
@ -88,4 +99,38 @@ public class Tab {
|
|||||||
gc.gridy = 0;
|
gc.gridy = 0;
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
public JTextArea getTextArea(byte i){
|
||||||
|
return (i == 0) ? axiomList : rulesList;
|
||||||
|
}
|
||||||
|
public JTextField getTextField(byte i){
|
||||||
|
return (i == 0) ? axiomeField : rulesField;
|
||||||
|
}
|
||||||
|
public void changeList(String stringToAdd, JTextArea list,int nbAxioms) {
|
||||||
|
if(nbAxioms>0)
|
||||||
|
JOptionPane.showMessageDialog(null, "Nombre maximal d'axiomes créés");
|
||||||
|
else {
|
||||||
|
list.append(stringToAdd);
|
||||||
|
if (stringToAdd == ";")
|
||||||
|
nbAxioms++;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
public String getAxiom(){
|
||||||
|
String str = axiomList.getText();
|
||||||
|
str = str.substring(10,str.length());
|
||||||
|
return str;
|
||||||
|
}
|
||||||
|
public java.util.List<String> getRules(){
|
||||||
|
List<String> list = new ArrayList<>();
|
||||||
|
String str = rulesList.getText();
|
||||||
|
str = str.substring(10,str.length());
|
||||||
|
String[] strsplit = str.split("\n");
|
||||||
|
for(int y = 0;y<strsplit.length;y++){
|
||||||
|
list.add(strsplit[y]);
|
||||||
|
}
|
||||||
|
return list;
|
||||||
|
}
|
||||||
|
public int getNbIterations(){
|
||||||
|
return (int) itSpinner.getValue();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -3,6 +3,7 @@ package lsystem.screen.listener;
|
|||||||
import lsystem.engine.Parser;
|
import lsystem.engine.Parser;
|
||||||
import lsystem.engine.Rewrite;
|
import lsystem.engine.Rewrite;
|
||||||
import lsystem.screen.MainFrame;
|
import lsystem.screen.MainFrame;
|
||||||
|
import lsystem.screen.Tab;
|
||||||
|
|
||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
import java.awt.event.ActionEvent;
|
import java.awt.event.ActionEvent;
|
||||||
@ -12,13 +13,14 @@ import java.awt.event.KeyListener;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class Listener implements ActionListener, KeyListener {
|
public class Listener implements ActionListener, KeyListener {
|
||||||
|
Tab tab;
|
||||||
MainFrame frame;
|
MainFrame frame;
|
||||||
Integer index;
|
Integer index;
|
||||||
String type;
|
String type;
|
||||||
Integer nbAxioms;
|
Integer nbAxioms;
|
||||||
|
|
||||||
public Listener(MainFrame frame, Integer index, String type){
|
public Listener(MainFrame frame, Integer index, String type, Tab tab){
|
||||||
|
this.tab = tab;
|
||||||
this.frame = frame;
|
this.frame = frame;
|
||||||
this.index = index;
|
this.index = index;
|
||||||
this.type = type;
|
this.type = type;
|
||||||
@ -27,40 +29,52 @@ public class Listener implements ActionListener, KeyListener {
|
|||||||
@Override
|
@Override
|
||||||
public void actionPerformed(ActionEvent e) {
|
public void actionPerformed(ActionEvent e) {
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case "Help" -> frame.newHelp();
|
case "Help":
|
||||||
case "Tab" -> frame.newTab();
|
frame.newHelp();
|
||||||
case "Clear" -> {
|
break;
|
||||||
frame.textAreaList.get(index).setText("Axiome : \n");
|
case "Tab":
|
||||||
frame.textAreaList.get(index + 10).setText("Règles : \n");
|
frame.newTab();
|
||||||
frame.textFieldList.get(index).setText("");
|
break;
|
||||||
frame.textFieldList.get(index + 10).setText("");
|
case "Clear":
|
||||||
}
|
tab.getTextArea((byte) 0).setText("Axiome : \n");
|
||||||
case "Generate" -> {
|
tab.getTextArea((byte) 1).setText("Règles : \n");
|
||||||
String axiom = frame.getAxiom(index);
|
tab.getTextField((byte) 0).setText("");
|
||||||
List<String> rules = frame.getRules(index + 10);
|
tab.getTextField((byte) 1).setText("");
|
||||||
Parser parser = new Parser(axiom, rules, 12);
|
Listener kl = (Listener) tab.getTextField((byte) 0).getKeyListeners()[0];
|
||||||
|
kl.resetNbAxioms();
|
||||||
|
|
||||||
|
break;
|
||||||
|
|
||||||
|
case "Generate":
|
||||||
|
String axiom = tab.getAxiom();
|
||||||
|
List<String> rules = tab.getRules();
|
||||||
|
Parser parser = new Parser(axiom, rules, tab.getNbIterations());
|
||||||
if (!parser.isCorrect()) {
|
if (!parser.isCorrect()) {
|
||||||
JOptionPane.showMessageDialog(null, "Vos règles ou votre axiome ne sont pas correctement écrites, veuillez recommencer");
|
JOptionPane.showMessageDialog(null, "Vos règles ou votre axiome ne sont pas correctement écrites, veuillez recommencer");
|
||||||
new Listener(frame, index, "Clear");
|
new Listener(null, index, "Clear",tab);
|
||||||
} else {
|
} else {
|
||||||
Rewrite rewriter = new Rewrite(axiom, parser.parseRules(), 12);
|
Rewrite rewriter = new Rewrite(axiom, parser.parseRules(), tab.getNbIterations());
|
||||||
final String word = rewriter.rewrite();
|
final String word = rewriter.rewrite();
|
||||||
System.out.println(word);
|
System.out.println(word);
|
||||||
}
|
}
|
||||||
}
|
break;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@Override
|
@Override
|
||||||
public void keyTyped(KeyEvent ke) {
|
public void keyTyped(KeyEvent ke) {
|
||||||
|
|
||||||
|
byte i = (byte) ((type.equals("Axiome")) ? 0 : 1);
|
||||||
|
|
||||||
if(nbAxioms==0 && ke.getKeyChar() !='\b')
|
if(nbAxioms==0 && ke.getKeyChar() !='\b')
|
||||||
frame.changeList(String.valueOf(ke.getKeyChar()), frame.textAreaList.get(index),nbAxioms);
|
tab.changeList(String.valueOf(ke.getKeyChar()), tab.getTextArea(i),nbAxioms);
|
||||||
if(ke.getKeyChar() == '\b'){
|
if(ke.getKeyChar() == '\b'){
|
||||||
String str = frame.textAreaList.get(index).getText();
|
String str = tab.getTextArea(i).getText();
|
||||||
if(str.length()>10) {
|
if(str.length()>10) {
|
||||||
if (!(str.endsWith(";\n") || str.endsWith(";"))){
|
if (!(str.endsWith(";\n") || str.endsWith(";"))){
|
||||||
str = str.substring(10, str.length() - 1);
|
str = str.substring(10, str.length() - 1);
|
||||||
frame.textAreaList.get(index).setText(type + " : \n" + str);
|
tab.getTextArea(i).setText(type + " : \n" + str);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -68,11 +82,14 @@ public class Listener implements ActionListener, KeyListener {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void keyPressed(KeyEvent ke) {
|
public void keyPressed(KeyEvent ke) {
|
||||||
|
|
||||||
|
byte i = (byte) ((type.equals("Axiome")) ? 0 : 1);
|
||||||
|
|
||||||
if(ke.getKeyCode() == KeyEvent.VK_ENTER) {
|
if(ke.getKeyCode() == KeyEvent.VK_ENTER) {
|
||||||
frame.textFieldList.get(index).setText(null);
|
tab.getTextField(i).setText(null);
|
||||||
String str = ";";
|
String str = ";";
|
||||||
frame.changeList(str, frame.textAreaList.get(index),nbAxioms);
|
tab.changeList(str, tab.getTextArea(i),nbAxioms);
|
||||||
if(type.equals("Axiome"))
|
if(i == 0)
|
||||||
nbAxioms ++;
|
nbAxioms ++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -81,4 +98,7 @@ public class Listener implements ActionListener, KeyListener {
|
|||||||
public void keyReleased(KeyEvent ke) {
|
public void keyReleased(KeyEvent ke) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
public void resetNbAxioms(){
|
||||||
|
nbAxioms = 0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user