Hej Udviklere,
Jeg har siddet, og rodet lidt med et problem. Dette problem omhandler, at i programmet, har jeg 2 dele. En visuel del og en beregnings del, og jeg ville gerne have, at de køre på en sådan måde, så når beregnings delen har beregnet en del, så bliver visualiseringen opdateret. Indtil videre er det design, der er lagt ind i det på følgende måde:
Beregnings delen startes
Køre en beregning og sender beregningen til en Observer
Denne Observer, sender det så direkte videre til vores visualiserings del.
Koden for den del, hvor beregnings delen startes er her:
- package control.setupView;
-
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
-
- import javax.swing.JButton;
- import javax.swing.JOptionPane;
- import javax.swing.JPanel;
-
- import misc.StopCriteria;
- import misc.StopCriterias;
- import mmas.mmas;
- import view.ResultBox;
- import view.SetupFrame;
- import view.graphs.BitString;
- import view.graphs.EnKortEnLang;
- import view.graphs.Graph2;
- import view.graphs.GraphBest2;
- import view.graphs.GraphMix2;
- import view.graphs.GraphicView;
- import view.graphs.Hypercube;
- import view.graphs.Map;
- import DataLoader.Loader;
- import EA.EA;
- import History.History;
- import SA.Sa;
-
- public class RunListener implements ActionListener{
-
- SetupFrame setup;
- int type;
- //type = 1 bitstring
- //type = 2 tsp
-
- JButton button;
-
- public RunListener(SetupFrame setup, int type) {
- this.setup=setup;
- this.type=type;
- }
-
- @Override
- public void actionPerformed(ActionEvent ae) {
- Loader loader = null;
- int length = 0;
- if ((type==2 && !setup.filepath.getText().equals("")) || type==1) {
- if(!setup.filepath.getText().equals("")){
- try {
- loader = new Loader(setup.filepath.getText());
- } catch (Exception e) {
- JOptionPane.showMessageDialog(setup,
- "Eggs are not supposed to be green.",
- "Insane warning",
- JOptionPane.WARNING_MESSAGE);
- return;
- }
- }else if(type==2){
- return;
- }else if(type==1){
- length= (Integer) setup.nrInput.getValue();
- }
-
- setup.frame.clear();
- History his = new History();
- setup.frame.setHistory(his);
-
- StopCriterias stop = new StopCriterias(his) {
-
- };
- GraphicView view = null;
-
- if(setup.viewList.getSelectedItem().equals("Map")){
- view = new Map(his, loader.getNodes());
- }else if(setup.viewList.getSelectedItem().equals("Graph")){
- view = new Graph2(his);
- }else if(setup.viewList.getSelectedItem().equals("GraphBest")){
- view = new GraphBest2(his);
- }else if(setup.viewList.getSelectedItem().equals("GraphMix")){
- view = new GraphMix2(his);
- }else if(setup.viewList.getSelectedItem().equals("Søjle Diagram")){
- view = new EnKortEnLang(his, loader.getEdges());
- }else if(setup.viewList.getSelectedItem().equals("Hypercube")){
- view = new Hypercube(his, length);
- }else if(setup.viewList.getSelectedItem().equals("bitstring")){
- view = new BitString(length, his);
-
- }
-
- JPanel resultBox = new ResultBox(his);
-
- setup.frame.setGraphicView(view, resultBox);
-
- for(int i = 0; i < setup.nrOfStopConditions; i++){
- int con = setup.conditionList[i].getSelectedIndex();
- int param = (Integer)setup.condition[i].getValue();
- stop.addStopCriteria(new StopCriteria(con, param, false));
- }
-
-
-
- try {
- if(type==2){
- if (setup.algoInstance instanceof EA) {
- setup.algoInstance = new EA(loader.getEdges(), setup.getRule(),
- stop);
- } else if (setup.algoInstance instanceof Sa) {
- Sa sa = new Sa(loader.getEdges(),setup.getRule(),stop);
- sa.setTemperatur((Integer)setup.saSpinnerTemp.getValue());
- sa.setChoice(setup.saFunction.getSelectedIndex());
- setup.algoInstance = sa;
- } else if (setup.algoInstance instanceof mmas) {
- mmas ma = new mmas(loader.getEdges(),setup.getRule(),stop);
- ma.setPheromon((Double)setup.mmasPhermonSpinner.getValue());
- ma.setAlpha((Double)setup.mmasAlphaSpinner.getValue());
- ma.setBeta((Double)setup.mmasBetaSpinner.getValue());
- setup.algoInstance = ma;
- }
-
- }else if(type==1){
- if (setup.algoInstance instanceof EA) {
- setup.algoInstance = new EA(length, setup.getRule(), stop);
- } else if (setup.algoInstance instanceof Sa) {
- Sa sa = new Sa(length,setup.getRule(),stop);
- sa.setTemperatur((Integer)setup.saSpinnerTemp.getValue());
- sa.setChoice(setup.saFunction.getSelectedIndex());
- setup.algoInstance = sa;
- } else if (setup.algoInstance instanceof mmas) {
- mmas ma = new mmas(length,setup.getRule(),stop);
- ma.setPheromon((Double)setup.mmasPhermonSpinner.getValue());
- setup.algoInstance = ma;
- }
- }
- setup.algoInstance.addObserver(his);
- new Thread(new Runnable() {
- @Override
- public void run() {
- try {
- setup.algoInstance.run();
- } catch (Exception e) {
- e.printStackTrace();
- }
-
- }
- }).start();
-
- } catch (Exception e) {
- e.printStackTrace();
- }
- setup.frame.startTimer();
- setup.dispose();
- }
- }
- }
Og et eksempel på mit problem kan ses her:
http://www.youtube.com/watch?v=2rnDmpz1tzA Håber at I har nogle idéer til, hvordan man kan opnå den ønskede funktionalitet.