Advertisement
Guest User

BOLA BANG VANDY

a guest
Nov 21st, 2019
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.53 KB | None | 0 0
  1. package com.billy.accelerator;
  2.  
  3. import android.content.Context;
  4. import android.graphics.Canvas;
  5. import android.graphics.Color;
  6. import android.graphics.Paint;
  7. import android.graphics.RectF;
  8. import android.graphics.Typeface;
  9. import android.util.Log;
  10. import android.view.MotionEvent;
  11. import android.view.View;
  12. import android.widget.Toast;
  13.  
  14. import java.util.Formatter;
  15. import java.util.Random;
  16.  
  17. public class Ball extends View {
  18.  
  19. private int xMin = 0;
  20. private int xMax;
  21. private int yMin = 0;
  22. private int yMax;
  23. private float ballRadius = 80, ballX = ballRadius + 20, ballY = ballRadius + 20, ballSpeedX = 0, ballSpeedY = 0;
  24. private RectF ballBounds;
  25. private Paint paint ;
  26. public boolean touch = false;
  27.  
  28. public float rx, ry, currentX, currentY;
  29.  
  30. private Context c;
  31.  
  32. private StringBuilder statusMsg = new StringBuilder();
  33. private Formatter formatter = new Formatter(statusMsg);
  34.  
  35. public Ball(Context context) {
  36. super(context);
  37. c = context;
  38. ballBounds = new RectF();
  39. paint = new Paint();
  40.  
  41. paint.setTypeface(Typeface.MONOSPACE);
  42. paint.setTextSize(50);
  43.  
  44. this.setFocusableInTouchMode(true);
  45. }
  46.  
  47. int x = 0, y = 1;
  48.  
  49. @Override
  50. protected void onDraw(Canvas canvas) {
  51. super.onDraw(canvas);
  52. ballBounds.set(rx-ballRadius, ry-ballRadius, rx+ballRadius, ry+ballRadius);
  53. paint.setColor((Color.BLUE));
  54. canvas.drawOval(ballBounds, paint);
  55.  
  56. canvas.drawText(String.valueOf(statusMsg), 0, 50 ,paint);
  57.  
  58. MainActivity mc = (MainActivity)c;
  59.  
  60. this.ballSpeedX = mc.getX();
  61. this.ballSpeedY = mc.getY();
  62.  
  63. update();
  64.  
  65. try{
  66. Thread.sleep(1);
  67. }catch (Exception e){
  68. e.printStackTrace();
  69. }
  70. invalidate();
  71.  
  72. }
  73.  
  74. public void update() {
  75. rx += ballSpeedX;
  76. ry += ballSpeedY;
  77.  
  78. if (currentX < rx + ballRadius && currentX > rx - ballRadius && touch) {
  79. ballSpeedX = 0;
  80. }
  81.  
  82. if (currentY< ry + ballRadius && currentY > ry - ballRadius && touch) {
  83. ballSpeedY = 0;
  84. }
  85.  
  86. if(currentX > rx + ballRadius && touch){
  87. ballSpeedX = 16;
  88. }else if(currentX < rx + ballRadius && touch){
  89. ballSpeedX = -16;
  90. }
  91.  
  92. if(currentY > ry + ballRadius && touch){
  93. ballSpeedY = 16;
  94. }else if(currentY < ry + ballRadius && touch){
  95. ballSpeedY = -16;
  96. }
  97.  
  98. statusMsg.delete(0, statusMsg.length());
  99. formatter.format("Ball@(%3.0f, %3.0f).Speed=(%2.0f, %2.0f)", rx, rx, ballSpeedX, ballSpeedY);
  100. }
  101.  
  102. @Override
  103. public boolean onTouchEvent(MotionEvent event) {
  104. currentX = event.getX();
  105. currentY = event.getY();
  106.  
  107. if(event.getAction() == MotionEvent.ACTION_DOWN){
  108. touch = true;
  109. if(event.getAction() == MotionEvent.ACTION_MOVE){
  110. currentX = event.getX();
  111. currentY = event.getY();
  112. }
  113. }
  114.  
  115. if(event.getAction() == MotionEvent.ACTION_UP){
  116. ballSpeedX = 0;
  117. ballSpeedY = 0;
  118. touch = false;
  119. }
  120.  
  121. return true;
  122. }
  123.  
  124. @Override
  125. public void onSizeChanged(int w, int h, int oldW, int oldH) {
  126. xMax = w - 1;
  127. yMax = h - 1;
  128.  
  129. rx = new Random().nextFloat() * (xMax - ballX) + ballRadius;
  130. ry = new Random().nextFloat() * (yMax - ballY) + ballRadius;
  131. }
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement