Advertisement
far_light

Prize

Nov 18th, 2017
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.26 KB | None | 0 0
  1. package com.vladislav;
  2.  
  3. // Import libraries for create UI and event
  4. import javax.swing.*;
  5. import java.awt.*;
  6. import java.awt.event.*;
  7.  
  8. // Class for prize
  9. public class Prize {
  10.     // Texture, coordinates, activity and timer
  11.     private Image image;
  12.     public int x, y;
  13.     public boolean activity;
  14.     private Timer timer;
  15.  
  16.     // In constructor load image and initialize all param
  17.     public Prize(Image image) {
  18.         // For 0.5 seconds move prize to down
  19.         timer = new Timer(500, new ActionListener() {
  20.             public void actionPerformed(ActionEvent e) {
  21.                 down();
  22.             }
  23.         });
  24.  
  25.         // Load image and initialize activity
  26.         this.image = image;
  27.         activity = false;
  28.     }
  29.  
  30.     // If start - move object in start for random x.  (If)
  31.     public void start() {
  32.         timer.start();
  33.         y = 0; x = (int)(Math.random() * 700);
  34.         activity = true;
  35.     }
  36.  
  37.     // For moving
  38.     public void down() {
  39.         if (activity == true)
  40.             y += 6;
  41.  
  42.         // If go to limit edge
  43.         if (y + image.getHeight(null) >= 470)
  44.             timer.stop();
  45.     }
  46.  
  47.     public void draw(Graphics graph) {
  48.         if (activity == true)
  49.             graph.drawImage(image, x, y, null);
  50.     }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement