Browse Source

initial version

master
leo 1 year ago
commit
691e909d01

+ 49
- 0
src/wuerfel/Start.java View File

@@ -0,0 +1,49 @@
/*
* 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 wuerfel;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import wuerfel.controller.StartStopController;
import wuerfel.controller.WertAdapter;
import wuerfel.model.WuerfelModel;
import wuerfel.view.WuerfelView;
/**
*
* @author leo
*/
public class Start
{
public Start()
{
WuerfelView view = new WuerfelView();
WuerfelModel model = new WuerfelModel();
StartStopController controller = new StartStopController(view, model);
controller.registerEvents();
WertAdapter wAdapter = new WertAdapter(view, model);
wAdapter.einschreiben();
view.setVisible(true);
}
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
try
{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch (Exception ex)
{
JOptionPane.showMessageDialog(null, ex.toString());
}
new Start();
}
}

+ 46
- 0
src/wuerfel/controller/StartStopController.java View File

@@ -0,0 +1,46 @@
/*
* 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 wuerfel.controller;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import wuerfel.model.WuerfelModel;
import wuerfel.view.WuerfelView;
/**
*
* @author leo
*/
public class StartStopController implements ActionListener
{
private WuerfelView view;
private WuerfelModel model;
public StartStopController(WuerfelView view, WuerfelModel model)
{
this.view = view;
this.model = model;
}
public void registerEvents()
{
view.getBtnStart().addActionListener(this);
view.getBtnStop().addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == view.getBtnStart())
{
model.start();
}
else
{
model.stop();
}
}
}

+ 64
- 0
src/wuerfel/controller/WertAdapter.java View File

@@ -0,0 +1,64 @@
/*
* 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 wuerfel.controller;
import java.util.concurrent.Flow;
import java.util.concurrent.Flow.Subscriber;
import java.util.concurrent.Flow.Subscription;
import javax.swing.JOptionPane;
import wuerfel.model.WuerfelModel;
import wuerfel.view.WuerfelView;
/**
*
* @author leo
*/
public class WertAdapter implements Subscriber<Integer>
{
private WuerfelView view;
private WuerfelModel model;
private Subscription subscription;
public WertAdapter(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; // wird bei onNext und onError gebraucht
subscription.request(1); // 1 Wert bekommen
}
@Override
public void onNext(Integer item)
{
String strWert = String.valueOf(item); // Wert in String umwandeln
view.getLblWert().setText(strWert);
subscription.request(1); // für nächsten Wert anmelden
}
@Override
public void onError(Throwable throwable)
{
JOptionPane.showMessageDialog(view, throwable.toString());
}
@Override
public void onComplete() // Fertig
{
}
}

+ 88
- 0
src/wuerfel/model/WuerfelModel.java View File

@@ -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 wuerfel.model;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Flow.Subscriber;
import java.util.concurrent.SubmissionPublisher;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author leo
*/
public class WuerfelModel implements Runnable
{
private int wert;
private volatile boolean laufend;
private SubmissionPublisher<Integer> iPublisher;
private ExecutorService eService;
public WuerfelModel()
{
wert = 1;
laufend = false;
iPublisher = new SubmissionPublisher<>();
eService = Executors.newSingleThreadExecutor(); // ein einziger nebenläufiger Thread
}
public void addWertSubscription(Subscriber<Integer> subscriber)
{
iPublisher.subscribe(subscriber);
}
public void start()
{
laufend = true;
eService.submit(this); // Thread starten
notifyAll();
}
public void stop()
{
laufend = false;
}
@Override
public void run() {
while (laufend)
//while (true)
{
try
{
wert++;
if (wert > 6)
{
wert = 1;
}
Thread.sleep(100);
}
catch (Exception e)
{
System.err.println(e);
}
//if (laufend)
iPublisher.submit(wert);
}
while (!laufend)
{
try {
wait();
} catch (InterruptedException ex) {
Logger.getLogger(WuerfelModel.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}

+ 74
- 0
src/wuerfel/view/WuerfelView.form View File

@@ -0,0 +1,74 @@
<?xml version="1.0" encoding="UTF-8" ?>

<Form version="1.3" maxVersion="1.9" type="org.netbeans.modules.form.forminfo.JFrameFormInfo">
<Properties>
<Property name="defaultCloseOperation" type="int" value="3"/>
</Properties>
<SyntheticProperties>
<SyntheticProperty name="formSize" type="java.awt.Dimension" value="-84,-19,0,5,115,114,0,18,106,97,118,97,46,97,119,116,46,68,105,109,101,110,115,105,111,110,65,-114,-39,-41,-84,95,68,20,2,0,2,73,0,6,104,101,105,103,104,116,73,0,5,119,105,100,116,104,120,112,0,0,2,27,0,0,3,48"/>
<SyntheticProperty name="formSizePolicy" type="int" value="0"/>
<SyntheticProperty name="generateSize" type="boolean" value="true"/>
<SyntheticProperty name="generateCenter" type="boolean" value="true"/>
</SyntheticProperties>
<AuxValues>
<AuxValue name="FormSettings_autoResourcing" type="java.lang.Integer" value="0"/>
<AuxValue name="FormSettings_autoSetComponentName" type="java.lang.Boolean" value="false"/>
<AuxValue name="FormSettings_generateFQN" type="java.lang.Boolean" value="true"/>
<AuxValue name="FormSettings_generateMnemonicsCode" type="java.lang.Boolean" value="false"/>
<AuxValue name="FormSettings_i18nAutoMode" type="java.lang.Boolean" value="false"/>
<AuxValue name="FormSettings_layoutCodeTarget" type="java.lang.Integer" value="1"/>
<AuxValue name="FormSettings_listenerGenerationStyle" type="java.lang.Integer" value="0"/>
<AuxValue name="FormSettings_variablesLocal" type="java.lang.Boolean" value="false"/>
<AuxValue name="FormSettings_variablesModifier" type="java.lang.Integer" value="2"/>
</AuxValues>

<Layout class="org.netbeans.modules.form.compat2.layouts.DesignBorderLayout"/>
<SubComponents>
<Container class="javax.swing.JPanel" name="pnlTop">
<Constraints>
<Constraint layoutClass="org.netbeans.modules.form.compat2.layouts.DesignBorderLayout" value="org.netbeans.modules.form.compat2.layouts.DesignBorderLayout$BorderConstraintsDescription">
<BorderConstraints direction="First"/>
</Constraint>
</Constraints>

<Layout class="org.netbeans.modules.form.compat2.layouts.DesignFlowLayout"/>
<SubComponents>
<Component class="javax.swing.JButton" name="btnStart">
<Properties>
<Property name="text" type="java.lang.String" value="Start"/>
</Properties>
</Component>
<Component class="javax.swing.JButton" name="btnStop">
<Properties>
<Property name="text" type="java.lang.String" value="Stop"/>
</Properties>
</Component>
</SubComponents>
</Container>
<Container class="javax.swing.JPanel" name="pnlMain">
<Constraints>
<Constraint layoutClass="org.netbeans.modules.form.compat2.layouts.DesignBorderLayout" value="org.netbeans.modules.form.compat2.layouts.DesignBorderLayout$BorderConstraintsDescription">
<BorderConstraints direction="Center"/>
</Constraint>
</Constraints>

<Layout class="org.netbeans.modules.form.compat2.layouts.DesignBorderLayout"/>
<SubComponents>
<Component class="javax.swing.JLabel" name="lblWert">
<Properties>
<Property name="font" type="java.awt.Font" editor="org.netbeans.beaninfo.editors.FontEditor">
<Font name="Tahoma" size="48" style="0"/>
</Property>
<Property name="horizontalAlignment" type="int" value="0"/>
<Property name="text" type="java.lang.String" value="1"/>
</Properties>
<Constraints>
<Constraint layoutClass="org.netbeans.modules.form.compat2.layouts.DesignBorderLayout" value="org.netbeans.modules.form.compat2.layouts.DesignBorderLayout$BorderConstraintsDescription">
<BorderConstraints direction="Center"/>
</Constraint>
</Constraints>
</Component>
</SubComponents>
</Container>
</SubComponents>
</Form>

+ 122
- 0
src/wuerfel/view/WuerfelView.java View File

@@ -0,0 +1,122 @@
/*
* 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 wuerfel.view;

/**
*
* @author leo
*/
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 lblWert
*/
public javax.swing.JLabel getLblWert() {
return lblWert;
}

/**
* 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")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {

pnlTop = new javax.swing.JPanel();
btnStart = new javax.swing.JButton();
btnStop = new javax.swing.JButton();
pnlMain = new javax.swing.JPanel();
lblWert = new javax.swing.JLabel();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

btnStart.setText("Start");
pnlTop.add(btnStart);

btnStop.setText("Stop");
pnlTop.add(btnStop);

getContentPane().add(pnlTop, java.awt.BorderLayout.PAGE_START);

pnlMain.setLayout(new java.awt.BorderLayout());

lblWert.setFont(new java.awt.Font("Tahoma", 0, 48)); // NOI18N
lblWert.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
lblWert.setText("1");
pnlMain.add(lblWert, java.awt.BorderLayout.CENTER);

getContentPane().add(pnlMain, java.awt.BorderLayout.CENTER);

setSize(new java.awt.Dimension(816, 539));
setLocationRelativeTo(null);
}// </editor-fold>//GEN-END:initComponents

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* 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);
}
//</editor-fold>

/* 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.JLabel lblWert;
private javax.swing.JPanel pnlMain;
private javax.swing.JPanel pnlTop;
// End of variables declaration//GEN-END:variables
}

Loading…
Cancel
Save