Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.applet.*;
- import java.awt.*;
- import java.awt.event.*;
- import java.util.Random;
- import javax.swing.Timer;
- public class HeliEngine extends Applet implements ActionListener {
- Dimension dim;
- Random rg;
- Timer timer;
- int totalWallSegments, previousyLength = 50, x = 200;
- int[] xpos, yLength;
- Image image1, image2;
- public void init(){
- setSize(300,200);
- dim = getSize();
- rg = new Random();
- timer = new Timer(10,this);
- totalWallSegments = (dim.width/5);
- xpos = new int [totalWallSegments];
- yLength = new int [totalWallSegments];
- image1 = createBackground();
- // Populate xpos array
- for (int x = 0; x < xpos.length; x++)
- {
- xpos[x] = (x * 5);
- }
- setBackground (Color.BLACK);
- timer.start();
- }
- public void paint (Graphics g)
- {
- g.drawImage(image1,0-x,0,this);
- if (x % 300 == 0)
- {
- x = 0;
- image1 = image2;
- image2 = createBackground();
- }
- g.drawImage(image2,300-x,0,this);
- }
- public void update(Graphics g)
- {
- paint(g);
- }
- public Image createBackground()
- {
- Image offscreen = createImage (dim.width, dim.height);
- Graphics imageg = offscreen.getGraphics();
- yLength = generateYLengths(previousyLength);
- imageg.setColor(Color.GREEN);
- for (int x = 0 ; x < totalWallSegments ; x++)
- {
- imageg.fillRect(xpos[x], 0, 5, yLength[x]);
- imageg.fillRect(xpos[x], (yLength[x]+(int)(dim.height/1.5)), 5, 1000);
- }
- return offscreen;
- }
- public int[] generateYLengths(int previousY)
- {
- int[] localyLength = new int[totalWallSegments];
- for (int x = 0; x < localyLength.length; x++)
- {
- int randomNumber = rg.nextInt(3)-1;
- if (randomNumber == -1 && previousY > 0)
- {
- localyLength[x] = previousY -= 5;
- }
- else if (randomNumber == 0)
- {
- localyLength[x] = previousY;
- }
- else if (randomNumber == 1 && (previousY+(dim.height/1.5)) < dim.height)
- {
- localyLength[x] = previousY += 5;
- }
- previousY = localyLength[x];
- }
- previousyLength = previousY;
- return localyLength;
- }
- public void actionPerformed(ActionEvent e)
- {
- x++;
- repaint();
- }
- }
Add Comment
Please, Sign In to add comment