Guest User

Untitled

a guest
Apr 20th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.16 KB | None | 0 0
  1. import java.applet.*;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import java.util.Random;
  5.  
  6. import javax.swing.Timer;
  7.  
  8. public class HeliEngine extends Applet implements ActionListener {
  9.  
  10. Dimension dim;
  11. Random rg;
  12. Timer timer;
  13. int totalWallSegments, previousyLength = 50, x = 200;
  14. int[] xpos, yLength;
  15. Image image1, image2;
  16.  
  17. public void init(){
  18.  
  19. setSize(300,200);
  20.  
  21. dim = getSize();
  22. rg = new Random();
  23. timer = new Timer(10,this);
  24.  
  25. totalWallSegments = (dim.width/5);
  26.  
  27. xpos = new int [totalWallSegments];
  28. yLength = new int [totalWallSegments];
  29. image1 = createBackground();
  30.  
  31. // Populate xpos array
  32. for (int x = 0; x < xpos.length; x++)
  33. {
  34. xpos[x] = (x * 5);
  35. }
  36.  
  37. setBackground (Color.BLACK);
  38.  
  39. timer.start();
  40.  
  41. }
  42.  
  43. public void paint (Graphics g)
  44. {
  45.  
  46. g.drawImage(image1,0-x,0,this);
  47.  
  48. if (x % 300 == 0)
  49. {
  50. x = 0;
  51. image1 = image2;
  52. image2 = createBackground();
  53.  
  54. }
  55. g.drawImage(image2,300-x,0,this);
  56. }
  57.  
  58. public void update(Graphics g)
  59. {
  60. paint(g);
  61. }
  62.  
  63. public Image createBackground()
  64. {
  65. Image offscreen = createImage (dim.width, dim.height);
  66.  
  67. Graphics imageg = offscreen.getGraphics();
  68.  
  69. yLength = generateYLengths(previousyLength);
  70.  
  71. imageg.setColor(Color.GREEN);
  72. for (int x = 0 ; x < totalWallSegments ; x++)
  73. {
  74. imageg.fillRect(xpos[x], 0, 5, yLength[x]);
  75. imageg.fillRect(xpos[x], (yLength[x]+(int)(dim.height/1.5)), 5, 1000);
  76. }
  77.  
  78. return offscreen;
  79.  
  80. }
  81.  
  82. public int[] generateYLengths(int previousY)
  83. {
  84. int[] localyLength = new int[totalWallSegments];
  85.  
  86.  
  87. for (int x = 0; x < localyLength.length; x++)
  88. {
  89. int randomNumber = rg.nextInt(3)-1;
  90.  
  91. if (randomNumber == -1 && previousY > 0)
  92. {
  93. localyLength[x] = previousY -= 5;
  94. }
  95. else if (randomNumber == 0)
  96. {
  97. localyLength[x] = previousY;
  98. }
  99. else if (randomNumber == 1 && (previousY+(dim.height/1.5)) < dim.height)
  100. {
  101. localyLength[x] = previousY += 5;
  102. }
  103.  
  104. previousY = localyLength[x];
  105. }
  106.  
  107. previousyLength = previousY;
  108.  
  109. return localyLength;
  110.  
  111. }
  112.  
  113. public void actionPerformed(ActionEvent e)
  114. {
  115. x++;
  116. repaint();
  117. }
  118.  
  119.  
  120.  
  121.  
  122.  
  123. }
Add Comment
Please, Sign In to add comment