You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Model.java 2.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package model;
  7. import java.util.Observable;
  8. import java.util.logging.Level;
  9. import java.util.logging.Logger;
  10. import javax.swing.JOptionPane;
  11. /**
  12. *
  13. * @author nobody
  14. */
  15. public class Model extends Observable implements Runnable
  16. {
  17. private int zahl;
  18. private boolean status;
  19. private Thread thr;
  20. private boolean notify;
  21. private boolean schlafen;
  22. public Model()
  23. {
  24. zahl = 0;
  25. status = false;
  26. thr = null;
  27. }
  28. public void start()
  29. {
  30. if (status == true)
  31. {
  32. //1
  33. //notify = true;
  34. synchronized (thr)
  35. {
  36. thr.notify();
  37. schlafen = false;
  38. }
  39. }
  40. if (status == false && thr == null)
  41. {
  42. zahl = 0;
  43. thr = new Thread(this);
  44. thr.start();
  45. status = true;
  46. notify = true;
  47. }
  48. }
  49. @Override
  50. public void run()
  51. {
  52. while (status == true)
  53. {
  54. if (schlafen == true)
  55. {
  56. synchronized (thr)
  57. {
  58. try
  59. {
  60. thr.wait();
  61. }
  62. catch (InterruptedException ex)
  63. {
  64. JOptionPane.showMessageDialog(null, "Alles Kaputt");
  65. }
  66. }
  67. }
  68. // Zahl incrementieren
  69. zahl--;
  70. // Zahl ändern
  71. if (notify == true)
  72. {
  73. this.setChanged();
  74. this.notifyObservers();
  75. }
  76. // Zahl zurücksetzen
  77. if (zahl == 0)
  78. {
  79. zahl = 10;
  80. }
  81. // kurze Wartezeit
  82. try
  83. {
  84. Thread.sleep(1000);
  85. }
  86. catch (InterruptedException ex)
  87. {
  88. Logger.getLogger(Model.class.getName()).log(Level.SEVERE, null, ex);
  89. }
  90. }
  91. }
  92. /**
  93. * @return the zahl
  94. */
  95. public int getZahl()
  96. {
  97. return zahl;
  98. }
  99. public void setStartZahl(int zahl)
  100. {
  101. this.zahl = zahl;
  102. }
  103. public void isWarten()
  104. {
  105. schlafen = true;
  106. }
  107. public void isWeiterlaufen(){
  108. schlafen = false;
  109. }
  110. }