Advertisement
Guest User

Untitled

a guest
Dec 16th, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.09 KB | None | 0 0
  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 lab77;
  7.  
  8. import java.util.Random;
  9. import java.util.Timer;
  10. import java.util.TimerTask;
  11. import javax.microedition.lcdui.Canvas;
  12. import javax.microedition.lcdui.Display;
  13. import javax.microedition.lcdui.Form;
  14. import javax.microedition.lcdui.Graphics;
  15. import javax.microedition.midlet.*;
  16.  
  17. /**
  18. * @author admin
  19. */
  20. public class Midlet extends MIDlet {
  21.  
  22. Display ekran;
  23. PadajacySnieg zamiec = new PadajacySnieg();
  24. Grawitacja padaSnieg = new Grawitacja();
  25. Timer timer = new Timer();
  26.  
  27. public Midlet() {
  28. ekran = Display.getDisplay(this);
  29. }
  30.  
  31. public void startApp() {
  32. ekran.setCurrent(zamiec);
  33. timer.schedule(padaSnieg, 10, 10);
  34. }
  35.  
  36. public void pauseApp() {
  37. }
  38.  
  39. protected void exit() {
  40. timer.cancel();
  41. destroyApp(true);
  42. notifyDestroyed();
  43.  
  44. }
  45.  
  46. public void destroyApp(boolean unconditional) {
  47. }
  48.  
  49. class Grawitacja extends TimerTask {
  50.  
  51. public void run() {
  52. zamiec.scroll();
  53. }
  54. }
  55.  
  56. class PadajacySnieg extends Canvas {
  57.  
  58. int height;
  59. int width;
  60. int i = 1;
  61. int size;
  62. int[] snieg;
  63. int[] tab_y;
  64. Random generator = new Random();
  65. boolean painting = false;
  66.  
  67. public PadajacySnieg() {
  68. height = getHeight();
  69. width = getWidth();
  70. snieg = new int[height];
  71. tab_y = new int[height];
  72. for (int i = 0; i < height; i++) {
  73. snieg[i] = -1;
  74. }
  75. }
  76.  
  77. public void scroll() {
  78. if (painting) {
  79. return;
  80. }
  81. if (snieg[height - 1] > 0) {
  82. tab_y[snieg[height - 1]] += 1;
  83. }
  84. for (int i = height - 1; i > 0; --i) {
  85. snieg[i] = snieg[i - 1];
  86. }
  87. snieg[0] = Math.abs((generator.nextInt() % (2 * width)) / 2);
  88. repaint();
  89. }
  90.  
  91. protected void paint(Graphics g) {
  92. painting = true;
  93. g.setColor(128, 128, 128);
  94. g.fillRect(0, 0, width, height);
  95. g.setColor(255, 255, 255);
  96. int y = 0, x = 0;
  97. for (y = 0; y < height; ++y) {
  98. x = snieg[y];
  99. if (x == -1) {
  100. continue;
  101. }
  102. g.drawLine(x, y, x, y);
  103. }
  104. for (int i = 0; i < tab_y.length; i++) {
  105. if (i < tab_y.length - 1) {
  106. if (tab_y[i + 1] > tab_y[i] + 2) {
  107. g.drawLine(i, height, i, height - tab_y[i] + 1);
  108. } else {
  109. g.drawLine(i, height, i, height - tab_y[i]);
  110. }
  111. }
  112. }
  113. painting = false;
  114. }
  115. }
  116.  
  117. protected void keyPressed(int keyCode) {
  118. exit();
  119. }
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement