Making a basic paint program

  • Replies:1
Joshua Lefelhocz
  • Forum posts: 3

Apr 25, 2013, 5:55:52 PM via Website

Create a MyLine, MyCircle and MyRectangle classes derived from MyDrawable to represent shapes drawn. These shape classes should include fields for location size and color. Create an ArrayList to hold all the shapes drawn. Add a menu item for the user to select the color of the next drawn object.

How do I make it so that I can call the draw() method from MyLine, MyCircle or MyRectangle through a switch statement?

MyLine:
1package com.lcc.java2final_lefelhocz;
2
3import android.graphics.Canvas;
4import android.graphics.Color;
5import android.graphics.Paint;
6
7public class MyLine extends MyDrawable {
8float startX;
9float startY;
10float stopX;
11float stopY;
12static int color = Color.BLACK;
13Canvas canvas = new Canvas();
14Paint paint = new Paint();
15
16public MyLine(float aStartX, float aStartY, float aStopX, float aStopY, int aColor)
17{
18 startX = aStartX;
19 startY = aStartY;
20 stopX = aStopX;
21 stopY = aStopY;
22 color = aColor;
23}
24
25 public void draw(Canvas canvas, Paint paint) {
26
27 // TODO Auto-generated method stub
28 paint.setColor(color);
29canvas.drawLine(startX, startY, stopX, stopY, paint);
30 }
31
32}


LineDrawView.java:

1// Project: Java2Paint
2// File: LineDrawView.java
3// Date: 4/16/13
4// Description: Final Exam
5//
6package com.lcc.java2final_lefelhocz;
7
8
9
10import android.annotation.SuppressLint;
11import android.content.Context;
12import android.graphics.Canvas;
13import android.graphics.Color;
14import android.graphics.Paint;
15import android.view.MotionEvent;
16import android.view.View;
17
18@SuppressLint("DrawAllocation")
19public class LineDrawView extends View
20{
21 private float currentX;
22 private float currentY;
23 private float startX=0;
24 private float startY=0;
25
26
27 private int drawType = 3;
28 private Paint paintStroke;
29
30 public LineDrawView(Context context)
31 {
32 // call the super class constructor
33 super(context);
34
35 // The Paint class holds the style and color information about how to draw geometries, text and bitmaps.
36 // For efficiency create the paint objects in the constructor, not in draw
37 // paint.setStrokeWidth(10); // works on lines
38 // You can change the color of Paint without effecting objects already drawn
39 // You can NOT change the style of Paint without effecting objects already drawn
40 // The Style, TextSize apply to all objects drawn with the paint.
41
42 new Paint();
43
44 // added for lab12
45 // Create a Paint Object with Style=Stroke
46 paintStroke = new Paint();
47 paintStroke.setStyle(Paint.Style.STROKE);
48
49 // set the background color when the view is created
50 this.setBackgroundColor(Color.LTGRAY);
51 }
52
53
54
55 protected void onDraw(Canvas canvas, Paint paint)
56 {
57 // Draw a Red Diagonal line from upper left corner to bottom right corner
58
59
60 // added for lab12
61 switch(drawType){
62 case 1:
63 MyDrawable MyLine = (MyDrawable) new Object();
64 MyLine.draw(canvas, paint);
65 return;
66 case 2:
67
68 MyDrawable MyCircle = (MyDrawable) new Object();
69 MyCircle.draw(canvas, paint);
70
71
72 return;
73
74 case 3:
75
76 MyDrawable MyRectangle = (MyDrawable) new Object();
77 MyRectangle.draw(canvas, paint);
78
79 return;}
80 }
81
82 @Override
83 public boolean onTouchEvent(MotionEvent event)
84 {
85 // get the current X and Y position from the event
86 currentX = event.getX();
87 currentY = event.getY();
88
89 // switch on the touch event action
90 switch(event.getAction())
91 {
92 case MotionEvent.ACTION_DOWN:
93
94 // start the line
95 startX = currentX;
96 startY = currentY;
97 invalidate();
98 return true;
99 case MotionEvent.ACTION_UP:
100
101 invalidate();
102 return true;
103 case MotionEvent.ACTION_MOVE:
104
105
106 invalidate();
107 return true;
108 default:
109 return super.onTouchEvent(event);
110 }
111 }
112
113 // Called back when the view is first created or its size changes.
114 @Override
115 public void onSizeChanged(int width, int height, int oldWidth, int oldHeight)
116 {
117 }
118
119 // added for lab 12
120 public void setDrawType(int aDrawType)
121 {
122 drawType = aDrawType;
123 }
124}

MyCircle.java
1package com.lcc.java2final_lefelhocz;
2
3import android.graphics.Canvas;
4import android.graphics.Color;
5import android.graphics.Paint;
6
7public class MyCircle extends MyDrawable {
8 Canvas canvas = new Canvas();
9 Paint paint = new Paint();
10 private float startX;
11 private float startY;
12 private float endX;
13 private float endY;
14 static int color = Color.BLACK;
15
16
17 public MyCircle(float aStartX, float aStartY, float aStopX, float aStopY, int aColor)
18 {
19 startX = aStartX;
20 startY = aStartY;
21 endX = aStopX;
22 endY = aStopY;
23 color = aColor;
24 }
25 public void draw(Canvas canvas, Paint paint) {
26 // TODO Auto-generated method stub
27 paint.setColor(color);
28 canvas.drawCircle(startX, endX, (float) Math.sqrt((Math.pow(endX - startX,2) + Math.pow(endY - startY, 2))), paint);
29 }
30
31 }

— modified on Apr 29, 2013, 5:22:02 PM

Reply
Joshua Lefelhocz
  • Forum posts: 3

Apr 29, 2013, 5:22:51 PM via Website

Can someone help me please? Any help would be gladly appreciated.

— modified on May 1, 2013, 3:51:26 PM

Reply