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 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
| public class Dessineretcolorer extends Activity{
private Paint paint= new Paint();
private Path path= new Path();
int tool;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new graph(this,null));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
SubMenu m =menu.addSubMenu(0, 0, 0, "crayons");
m.setIcon(R.drawable.jaune);
m.setHeaderIcon(R.drawable.jaune);
m.add(0, 1, 0, "noir");
m.add(0, 2, 0, "bleu");
m.add(0, 3, 0, "rouge");
m.add(0, 4, 0, "vert");
m.add(0, 5, 0, "effacer l'ecran");
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId())
{
case 1:
paint.setColor(Color.BLACK);
break;
case 2:
paint.setColor(Color.BLUE);
path.set(path);
break;
case 3:
paint.setColor(Color.RED);
path.set(path);
break;
case 4:
paint.setColor(Color.GREEN);
path.set(path);
break;
case 5:
paint.setColor(Color.WHITE);
break;
}
return super.onOptionsItemSelected(item);
}
public class graph extends View {
public graph(Context context, AttributeSet attrs) {
super(context, attrs);
paint.setAntiAlias(true);
paint.setStrokeWidth(2f);
paint.setColor(Color.BLACK);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeJoin(Paint.Join.BEVEL);
}
@Override
protected void onDraw(Canvas c) {
c.drawColor(Color.WHITE);
c.drawPath(path, paint);
c.drawText("couleur vert",70, 80, paint);
c.drawCircle(150, 150,50 , paint);
c.drawText("couleur bleu",200, 200, paint);
c.drawCircle(300, 300,50 , paint);
c.drawText("couleur rouge",350, 400, paint);
c.drawRect(100,350,400,400, paint);
}
public boolean onTouchEvent(MotionEvent event) {
float eventX = event.getX();
float eventY = event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
path.moveTo(eventX, eventY);
return true;
case MotionEvent.ACTION_MOVE:
path.lineTo(eventX, eventY);
break;
case MotionEvent.ACTION_UP:
break;
default:
return false;
}
// Schedules a repaint.
invalidate();
return true;
}
}
} |
Partager