why some rectangles are not drawn when the screen is clicked?
...when a green field is clicked, it should get black again.
note: its landscape mode (btw, how to start an application in landscape mode?)
Using java Syntax Highlighting
- import android.content.Context;
- import android.graphics.Canvas;
- import android.graphics.Color;
- import android.graphics.Paint;
- import android.graphics.Rect;
- import android.view.MotionEvent;
- import android.view.View;
- public class MyView extends View {
- int x;
- int y;
- boolean[][] matrix = new boolean[16][4];
- public MyView(Context context) {
- super(context);
- setFocusable(true);
- this.setOnTouchListener(new OnTouchListener() {
- public boolean onTouch(View view, MotionEvent event) {
- x = (int) event.getX();
- y = (int) event.getY();
- invalidate();
- return true;
- }
- });
- }
- protected void onDraw(Canvas canvas) {
- Paint p = new Paint();
- p.setColor(Color.GREEN);
- int xPos = Math.round(x / 60);
- int yPos = Math.round(y / 60);
- try {
- if (matrix[xPos][yPos] == false) {
- matrix[xPos][yPos] = true;
- } else {
- matrix[xPos][yPos] = false;
- }
- for (int Py = 0; Py < 4; Py++) {
- for (int Px = 0; Px < 15; Px++)
- if (matrix[Px][Py] == true) {
- Rect mRect = new Rect(60*Px, 60*Py, 60*Px+60, 60*Py+60);
- canvas.drawRect(mRect, p);
- }
- }
- } catch (ArrayIndexOutOfBoundsException e) {}
- }
- }
Parsed in 0.035 seconds, using GeSHi 1.0.8.4
Using java Syntax Highlighting
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.WindowManager;
- public class MyOnTouchListener extends Activity {
- public void onCreate(Bundle savedValues) {
- super.onCreate(savedValues);
- setContentView(new MyView(this));
- getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
- WindowManager.LayoutParams.FLAG_FULLSCREEN);
- }
- }
Parsed in 0.032 seconds, using GeSHi 1.0.8.4


