Guest User

Untitled

a guest
Feb 25th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.20 KB | None | 0 0
  1. public class Juego extends SurfaceView implements View.OnTouchListener{
  2.  
  3. private Paint paint;
  4. int x = 100, y = 100, radio = 100, otroX, otroY;
  5.  
  6. public Juego(Context context, AttributeSet attrs) {
  7. super(context, attrs);
  8. this.setOnTouchListener(this);
  9. setFocusable(true);
  10. paint = new Paint();
  11. }
  12.  
  13. public void onDraw(Canvas canvas) {
  14.  
  15. paint.setColor(Color.WHITE);
  16. canvas.drawRect(0, 0, getWidth(), getHeight(), paint);
  17.  
  18. paint.setColor(Color.BLACK);
  19. canvas.drawCircle(x, y, radio, paint);
  20.  
  21. otroX = canvas.getWidth() / 2;
  22. otroY = canvas.getHeight() / 2;
  23.  
  24. canvas.drawCircle(otroX, otroY, radio, paint);
  25.  
  26. invalidate();
  27. }
  28.  
  29. @Override
  30. public boolean onTouch(View view,MotionEvent motionEvent){
  31.  
  32. x = (int)motionEvent.getX();
  33. y = (int)motionEvent.getY();
  34.  
  35. double dist = Math.sqrt(Math.pow((x - otroX), 2) + Math.pow((y - otroY), 2));
  36.  
  37. if (dist <= radio + radio) {
  38.  
  39. if (x < otroX) {
  40. x = otroX - radio;
  41. }
  42. if (x > otroX) {
  43. x = otroX + radio;
  44. }
  45. if (y < otroY) {
  46. y = otroY - radio;
  47. }
  48. if (y > otroY) {
  49. y = otroY + radio;
  50. }
  51. }
  52.  
  53. invalidate();
  54. return true;
  55. }
Add Comment
Please, Sign In to add comment