Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.27 KB | None | 0 0
  1. package com.strobertschs.finalprokect.finalproject;
  2.  
  3. import android.graphics.Bitmap;
  4. import android.graphics.Canvas;
  5.  
  6. import java.util.ArrayList;
  7. import java.util.Random;
  8. /**
  9. * Created by ari on 6/23/2017.
  10. */
  11.  
  12. public class ButtonClass {
  13.  
  14. private Bitmap bitmap; // an object image
  15. protected ArrayList<Bitmap> objectImages; // a list of all object images
  16. private int x, y, screenWidth, screenHeight; // the position of the object and the screen dimensions
  17. private boolean touched; // was the object touched by the user
  18. // private boolean visible;
  19. private int numObjects; // the number of object images in the objectImages list
  20.  
  21. public ButtonClass(ArrayList<Bitmap> objectImages, int waterTop, int screenWidth, int screenHeight){
  22. this.objectImages = objectImages;
  23. this.screenHeight = screenHeight;
  24. this.screenWidth = screenWidth;
  25. numObjects = objectImages.size();
  26. Random random = new Random();
  27. bitmap = objectImages.get(random.nextInt(numObjects));
  28. x = bitmap.getWidth() + random.nextInt(screenWidth - 2*bitmap.getWidth());
  29. y = waterTop - 2*bitmap.getHeight() + random.nextInt(screenHeight - waterTop);
  30. touched = false;
  31. // visible = true;
  32. }
  33. public Bitmap getBitmap(){
  34. return bitmap;
  35. }
  36. public void setBitmap(Bitmap bitmap){
  37. this.bitmap = bitmap;
  38. }
  39. public void nextBitmap(){
  40. Random random = new Random();
  41. setBitmap(objectImages.get(random.nextInt(numObjects)));
  42. }
  43. public int getX(){
  44. return x;
  45. }
  46. public void setX(int x){
  47. this.x = x;
  48. }
  49. public int getY(){
  50. return y;
  51. }
  52. public void setY(int y){
  53. this.y=y;
  54. }
  55. public boolean isTouched(){
  56. return touched;
  57. }
  58. public void setTouched(boolean touched){
  59. this.touched = touched;
  60. }
  61. public void setNewPosition(int waterTop){
  62. Random random = new Random();
  63. x = bitmap.getWidth() + random.nextInt(screenWidth - 2*bitmap.getWidth());
  64. y = waterTop - 2*bitmap.getHeight() + random.nextInt(screenHeight - waterTop);
  65. }
  66. // public boolean isVisible(){
  67. // return touched;
  68. // }
  69. // public void setVisible(boolean visible){
  70. // this.visible = visible;
  71. // }
  72.  
  73. public void draw(Canvas canvas){
  74. if(!touched) {
  75. canvas.drawBitmap(bitmap, x, y, null);
  76. }
  77. }
  78. public void checkTouched(int hitX, int hitY){
  79. // if(hitX >= (x-bitmap.getWidth()/2) && (hitX <= (x + bitmap.getWidth()/2))){
  80. // if(hitY >= (y-bitmap.getHeight()/2) && (hitY <= (y + bitmap.getHeight()/2))){
  81. if((hitX >= x) && (hitX <= (x + bitmap.getWidth()))){
  82. if((hitY >= y) && (hitY <= (y + bitmap.getHeight()))){
  83. setTouched(true);
  84. }else{
  85. setTouched(false);
  86. }
  87. }else{
  88. setTouched(false);
  89. }
  90. }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement