1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
| public class MainActivity extends Activity
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(new MaVue(this));
}
public class MaVue extends View{
Paint mPaint = new Paint();
int cx = 300;
int cy = 200;
int cdiam = 20;
boolean avancex = true;
boolean avancey = true;
public MaVue(Context c) {
super(c);
}
public void onDraw(Canvas canvas){
mPaint.setColor(Color.WHITE);
canvas.drawRect(100, 100, 500, 400, mPaint);
mPaint.setColor(Color.BLUE);
canvas.drawCircle(cx, cy, cdiam, mPaint);
invalidate();
try {
Thread.sleep(1);
}
catch (InterruptedException e) {
e.printStackTrace();
}
update();
}
public void update() {
if (cx == 500-cdiam){avancex = false;}
if (cy == 400-cdiam){avancey = false;}
if (cx == 100+cdiam){avancex = true;}
if (cy == 100+cdiam){avancey = true;}
if (avancex){cx += 1;}
if (avancey){cy += 1;}
if (avancex == false){cx -= 1;}
if (avancey == false){cy -= 1;}
}
}
} |
Partager