円をタッチすると、新しい円を描くようにしたいのですが、onTouch()のあたりで、エラーが出ているようです。
自力で、解明できません。
java
1public class Circles extends View implements View.OnTouchListener { 2 3 final Paint paint; 4 5 static int r = 60; 6 static int i = 0; 7 static int x; 8 static int y; 9 10 final static int bc = Color.BLACK; 11 final static int dc = Color.MAGENTA; 12 13 public Circles(Context context, AttributeSet attrs){ 14 super(context, attrs); 15 paint = new Paint(); 16 } 17 18 @Override 19 protected void onDraw(Canvas canvas) { 20 super.onDraw(canvas); 21 22 final int width = canvas.getWidth(); 23 final int height = canvas.getHeight(); 24 25 x = (int) (Math.random() * width); 26 y = (int) (Math.random() * height); 27 if ((x < width - 2 * r) && (y < height - 2 * r)) { 28 canvas.drawColor(bc); 29 paint.setColor(dc); 30 canvas.drawCircle(x, y, r, paint); 31 findViewById(R.id.view).setOnTouchListener(this); 32 } else { 33 invalidate(); 34 } 35 } 36 37 protected void onTouch(View v, MotionEvent e) { 38 double mouseX = e.getX(); 39 double mouseY = e.getY(); 40 if (mouseX > x && mouseX < x + 2 * r) { 41 if (mouseY > y && mouseY < y + 2 * r) { 42 invalidate(); 43 } 44 } 45 } 46}
回答2件
あなたの回答
tips
プレビュー