238025681

DrawImage

Jun 7th, 2016
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.64 KB | None | 0 0
  1. import java.awt.*;
  2. import java.net.URL;
  3. import java.util.Random;
  4.  
  5. /**
  6.  * Created by kalin on 06.06.16.
  7.  */
  8. public class Platform {
  9.  
  10.  
  11.     private int dx;
  12.     private int x;
  13.     private int y;
  14.     private int whdth;
  15.     private int height;
  16.     private URL url;
  17.     private Image plat;
  18.     private float frame = 0;
  19.  
  20.     public Platform(int x, int y) {
  21.         this.x = x;
  22.         this.y = y;
  23.         whdth = 201;
  24.         height = 51;
  25.         dx = -1;
  26.  
  27.         plat = Pictures.platform;
  28.     }
  29.  
  30.     public void update (StartingPoints sp, Ball b){
  31.  
  32.         int tester = (int)(frame + .2);
  33.         if (tester < 3){
  34.             frame += .2;
  35.         }else {
  36.             frame = 0;
  37.         }
  38.         x +=dx;
  39.         checkForCollision(b);
  40.         //move  right to left
  41.         if (x < 0 - whdth){
  42.             Random r = new Random();
  43.             //random position
  44.             y =  sp.getHeight() - 40  - r.nextInt(400);
  45.             x = sp.getWidth() + r.nextInt(300);
  46.         }
  47.  
  48.     }
  49.  
  50.     private void checkForCollision(Ball b) {
  51.  
  52.         int ballX = b.getX();
  53.         int ballY = b.getY();
  54.         int radius = b.getRadius();
  55.  
  56.         if (ballY + radius > y && ballY + radius < y + height){
  57.             if (ballX > x && ballX < x + whdth) {
  58.                 double newDY = b.getGameDy();
  59.                 b.setY(y-radius);
  60.                 b.setDy(newDY);
  61.             }
  62.         }
  63.     }
  64.  
  65.     public void paint(Graphics g){
  66.         g.setColor(Color.BLUE);
  67.  
  68.         g.fillRect(x,y,whdth,height);
  69.  
  70.         g.drawImage(plat,x, y, x + whdth, y + height, 0 , 55 * (int)frame,  201, 55 * (int)frame + 55 ,Pictures.sp);
  71.  
  72.     }
  73.  
  74. }
Advertisement
Add Comment
Please, Sign In to add comment