/* Copyright (c) SEMM NL All rights reserved.
Author : Paul Hamaker. Part of JavaLessons.com
This code is for educational purposes only. Use at own risk.*/

1. // MainActivity.java 

2. package com.javalessons.android.simplest2; 

3.  

4. import android.os.Bundle

5. import android.app.Activity

6.  

7. public class MainActivity extends Activity

8.  

9.     @Override 

10.     public void onCreate(Bundle savedInstanceState) { 

11.         super.onCreate(savedInstanceState); 

12.  

13.         setContentView( new CanvasView(this) ); 

14.     } 

15.

16. //========================================= 

17. // Canvasview.java 

18.  

19. package com.javalessons.android.simplest2; 

20.  

21. import android.content.Context

22. import android.graphics.Canvas

23. import android.graphics.Paint

24. import android.view.MotionEvent

25. import android.view.View

26.  

27. public class CanvasView extends View

28.     Paint p = new Paint(); 

29.  

30.     int x=50

31.     int y=50

32.     int x2=50

33.     int y2=50

34.  

35.   @Override 

36.   protected void onDraw(Canvas c) { 

37.        super.onDraw(c); 

38.         

39.          p.setColor(0xffff0000); 

40.          c.drawLine(x, y, x2, y2, p); 

41.          c.drawLine(x, y, x2+100, y2, p); 

42.          c.drawLine(x, y, x2, y2+50, p); 

43.          c.drawLine(x, y, x2+100, y2+50, p); 

44.           

45.          p.setColor(0x9900ffff); 

46.          c.drawRect(x2, y2, x2+100, y2+50, p); 

47.           

48.          p.setColor(0x9900ff00); 

49.          c.drawCircle(x2+25, y2+25, 25, p); 

50.     } 

51.  

52.   @Override 

53.   public boolean onTouchEvent(MotionEvent ev) { 

54.     if ( ev.getAction()==MotionEvent.ACTION_DOWN ) { 

55.       x = x2 = (int)ev.getX(); 

56.       y = y2 = (int)ev.getY(); 

57.     } 

58.     else

59.       x2 = (int)ev.getX(); 

60.       y2 = (int)ev.getY(); 

61.     } 

62.     invalidate(); 

63.     return true

64.   } 

65.    

66.     public CanvasView(Context context) { 

67.         super(context); 

68.     }     

69.