From 51de7dfa3c7acb1ca3a689885f1042cde612556d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hannes=20D=C3=BCrr?= Date: Wed, 25 Nov 2020 10:19:08 +0100 Subject: [PATCH] init --- .gitignore | 9 ++ src/bandit/Start.java | 47 ++++++ src/bandit/controller/BanditAdapter.java | 66 ++++++++ src/bandit/controller/BanditController.java | 59 +++++++ src/bandit/model/WuerfelModel.java | 88 +++++++++++ src/bandit/view/WuerfelView.form | 86 +++++++++++ src/bandit/view/WuerfelView.java | 161 ++++++++++++++++++++ 7 files changed, 516 insertions(+) create mode 100644 .gitignore create mode 100644 src/bandit/Start.java create mode 100644 src/bandit/controller/BanditAdapter.java create mode 100644 src/bandit/controller/BanditController.java create mode 100644 src/bandit/model/WuerfelModel.java create mode 100644 src/bandit/view/WuerfelView.form create mode 100644 src/bandit/view/WuerfelView.java diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..03ef08b --- /dev/null +++ b/.gitignore @@ -0,0 +1,9 @@ +/build/ +/nbproject/private/ +*.xml +*.properties +*.mf +/dist/ +/wurfel/nbproject/private/ +/wurfel/dist/ +/wurfel/build/ diff --git a/src/bandit/Start.java b/src/bandit/Start.java new file mode 100644 index 0000000..90e7257 --- /dev/null +++ b/src/bandit/Start.java @@ -0,0 +1,47 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package bandit; + +import javax.swing.JOptionPane; +import javax.swing.UIManager; +import bandit.controller.BanditAdapter; +import bandit.controller.BanditController; +import bandit.model.WuerfelModel; +import bandit.view.WuerfelView; + +/** + * + * @author hd, chris + */ +public class Start +{ + public Start() + { + WuerfelView view = new WuerfelView(); + WuerfelModel model = new WuerfelModel(); + BanditAdapter adapter = new BanditAdapter(view, model); + BanditController ctrlCommand = new BanditController(view, model); + ctrlCommand.registerEvents(); + ctrlCommand.registerCommands(); + adapter.einschreiben(); + view.setVisible(true); + } + + public static void main(String[] args) + { + try + { + UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); + } + catch (Exception e) + { + JOptionPane.showMessageDialog(null, e.toString()); + } + new Start(); + } + +} diff --git a/src/bandit/controller/BanditAdapter.java b/src/bandit/controller/BanditAdapter.java new file mode 100644 index 0000000..9a0ffa0 --- /dev/null +++ b/src/bandit/controller/BanditAdapter.java @@ -0,0 +1,66 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package bandit.controller; + + +import java.util.concurrent.Flow; +import java.util.concurrent.Flow.Subscriber; +import java.util.logging.Logger; + +import bandit.model.WuerfelModel; +import bandit.view.WuerfelView; + +/** + * + * @author hd + */ +public class BanditAdapter implements Subscriber +{ + private WuerfelView view; + private WuerfelModel model; + private Flow.Subscription subscription; + private static Logger lg = Logger.getLogger("Wuerfelthreads"); + + public BanditAdapter(WuerfelView view, WuerfelModel model) + { + this.view = view; + this.model = model; + } + + public void einschreiben() + { + model.addWertSubscription(this); + } + + @Override + public void onSubscribe(Flow.Subscription subscription) + { + this.subscription = subscription; + subscription.request(1); + lg.info("onSubsribe"); + } + + @Override + public void onNext(Integer item) + { + view.getLbZahl().setText(String.valueOf(item)); + subscription.request(1); + lg.info("onNext"); + } + + @Override + public void onError(Throwable throwable) + { + } + + @Override + public void onComplete() + { + } + + +} \ No newline at end of file diff --git a/src/bandit/controller/BanditController.java b/src/bandit/controller/BanditController.java new file mode 100644 index 0000000..6fae14c --- /dev/null +++ b/src/bandit/controller/BanditController.java @@ -0,0 +1,59 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package bandit.controller; + +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import bandit.model.WuerfelModel; +import bandit.view.WuerfelView; + +/** + * + * @author chris + */ +public class BanditController implements ActionListener +{ + private WuerfelView view; + private WuerfelModel model; + + public BanditController(WuerfelView view, WuerfelModel model) + { + this.view = view; + this.model = model; + + } + public void registerEvents() + { + view.getBtnStart().addActionListener(this); + view.getBtnStop().addActionListener(this); + } + public void registerCommands() + { +// CommandOpen cmdOpen = new CommandOpen(view, model); +// invoker.addCommand(view.getMnuOpen(), cmdOpen); +// invoker.addCommand(view.getBtnOpen(), cmdOpen); +// //usw. +// +// +// CommandSave cmdSave = new CommandSave(view, model); +// invoker.addCommand(view.getMnuSave(), cmdSave); +// invoker.addCommand(view.getBtnSave(), cmdSave); + } + + @Override + public void actionPerformed(ActionEvent e) + { + if (e.getSource() == view.getBtnStart()) + { + model.start(); + } + else + { + model.stop(); + } + } +} diff --git a/src/bandit/model/WuerfelModel.java b/src/bandit/model/WuerfelModel.java new file mode 100644 index 0000000..f916357 --- /dev/null +++ b/src/bandit/model/WuerfelModel.java @@ -0,0 +1,88 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package bandit.model; + +import java.util.logging.Logger; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Flow.Subscriber; +import java.util.concurrent.SubmissionPublisher; + +/** + * + * @author chris + */ +public class WuerfelModel implements Runnable +{ + private int wert; + private volatile boolean laufend; + private SubmissionPublisher iPublisher; + private SubmissionPublisher bPublisher; + private ExecutorService eService; + private static Logger lg = Logger.getLogger("Wuerfelthreads"); + + public WuerfelModel() + { + wert = 1; + laufend = false; + iPublisher = new SubmissionPublisher<>(); + bPublisher = new SubmissionPublisher<>(); + eService = Executors.newSingleThreadExecutor(); + } + public void start() + { + laufend = true; + eService.submit(this); + lg.info("start"); + } + + public void stop() + { + laufend = false; + lg.info("stop"); + } + + public void addWertSubscription(Subscriber subscriber) + { + iPublisher.subscribe(subscriber); + } + + public void addZustandSubscription(Subscriber subscriber) + { + bPublisher.subscribe(subscriber); + } + + @Override + public void run() + { + //wert++; + //if(wert >= 7){ + // wert = 1; + //} + while (laufend) + { + + /* + try + { + Thread.sleep(10); + } + catch (Exception e) + { + System.err.println(e); + } + wert++; + if(wert >= 7){ + wert = 1; + } + + iPublisher.submit(wert); + */ + } + } +} + diff --git a/src/bandit/view/WuerfelView.form b/src/bandit/view/WuerfelView.form new file mode 100644 index 0000000..4df6a39 --- /dev/null +++ b/src/bandit/view/WuerfelView.form @@ -0,0 +1,86 @@ + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/bandit/view/WuerfelView.java b/src/bandit/view/WuerfelView.java new file mode 100644 index 0000000..fa849c6 --- /dev/null +++ b/src/bandit/view/WuerfelView.java @@ -0,0 +1,161 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package bandit.view; + +/** + * + * @author chris + */ +public class WuerfelView extends javax.swing.JFrame +{ + + /** + * @return the btnStart + */ + public javax.swing.JButton getBtnStart() + { + return btnStart; + } + + /** + * @return the btnStop + */ + public javax.swing.JButton getBtnStop() + { + return btnStop; + } + + /** + * @return the lbZahl + */ + public javax.swing.JLabel getLbZahl() + { + return lbZahl; + } + + /** + * @param lbZahl the lbZahl to set + */ + public void setLbZahl(javax.swing.JLabel lbZahl) + { + this.lbZahl = lbZahl; + } + + /** + * Creates new form WuerfelView + */ + public WuerfelView() + { + initComponents(); + } + + /** + * This method is called from within the constructor to initialize the form. + * WARNING: Do NOT modify this code. The content of this method is always + * regenerated by the Form Editor. + */ + @SuppressWarnings("unchecked") + // //GEN-BEGIN:initComponents + private void initComponents() + { + + jPanel1 = new javax.swing.JPanel(); + btnStart = new javax.swing.JButton(); + btnStop = new javax.swing.JButton(); + jPanel2 = new javax.swing.JPanel(); + lbZahl1 = new javax.swing.JLabel(); + lbZahl2 = new javax.swing.JLabel(); + lbZahl3 = new javax.swing.JLabel(); + + setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); + + btnStart.setText("Start"); + jPanel1.add(btnStart); + + btnStop.setText("Stop"); + jPanel1.add(btnStop); + + getContentPane().add(jPanel1, java.awt.BorderLayout.PAGE_END); + + lbZahl1.setFont(new java.awt.Font("Tahoma", 0, 64)); // NOI18N + lbZahl1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); + lbZahl1.setText("0"); + jPanel2.add(lbZahl1); + + lbZahl2.setFont(new java.awt.Font("Tahoma", 0, 64)); // NOI18N + lbZahl2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); + lbZahl2.setText("0"); + jPanel2.add(lbZahl2); + + lbZahl3.setFont(new java.awt.Font("Tahoma", 0, 64)); // NOI18N + lbZahl3.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); + lbZahl3.setText("0"); + jPanel2.add(lbZahl3); + + getContentPane().add(jPanel2, java.awt.BorderLayout.CENTER); + + pack(); + }// //GEN-END:initComponents + + /** + * @param args the command line arguments + */ + public static void main(String args[]) + { + /* Set the Nimbus look and feel */ + // + /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. + * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html + */ + try + { + for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) + { + if ("Nimbus".equals(info.getName())) + { + javax.swing.UIManager.setLookAndFeel(info.getClassName()); + break; + } + } + } + catch (ClassNotFoundException ex) + { + java.util.logging.Logger.getLogger(WuerfelView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); + } + catch (InstantiationException ex) + { + java.util.logging.Logger.getLogger(WuerfelView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); + } + catch (IllegalAccessException ex) + { + java.util.logging.Logger.getLogger(WuerfelView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); + } + catch (javax.swing.UnsupportedLookAndFeelException ex) + { + java.util.logging.Logger.getLogger(WuerfelView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); + } + // + + /* Create and display the form */ + java.awt.EventQueue.invokeLater(new Runnable() + { + public void run() + { + new WuerfelView().setVisible(true); + } + }); + } + + // Variables declaration - do not modify//GEN-BEGIN:variables + private javax.swing.JButton btnStart; + private javax.swing.JButton btnStop; + private javax.swing.JPanel jPanel1; + private javax.swing.JPanel jPanel2; + private javax.swing.JLabel lbZahl1; + private javax.swing.JLabel lbZahl2; + private javax.swing.JLabel lbZahl3; + // End of variables declaration//GEN-END:variables +}