Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Devon McCoy
30 PointsHow to set fingerpaint to a different color using this Dialog interface?
public class DrawingView extends View
{
Paint mPaint;
//MaskFilter mEmboss;
//MaskFilter mBlur;
Bitmap mBitmap;
Canvas mCanvas;
Path mPath;
Paint mBitmapPaint;
public DrawingView(Context context) {
super(context);
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setDither(true);
mPaint.setColor(0xFFFF0000);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeJoin(Paint.Join.ROUND);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mPaint.setStrokeWidth(20);
mPath = new Path();
mBitmapPaint = new Paint();
mBitmapPaint.setColor(Color.RED);
}
public void newColor(int color) {
mPaint.setColor(color);
}
What the above code does is basically sets up the preferences of finger paint. The newColor method will be used to change the paint color.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_paint_image);
DrawingView mDrawingView=new DrawingView(this);
mDrawingView.newColor(Color.GREEN); //Currently setting the color in onCreate, but i want to set it in the Dialog interface
LinearLayout mDrawingPad=(LinearLayout)findViewById(R.id.view_drawing_pad);
mDrawingPad.addView(mDrawingView);
}
The onCreate method sets the color when the activity is created
public DialogInterface.OnClickListener mDialClickListener = new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch(which)
{
case 0: //red
}
}
};
But i want to set the color using the above Dialog Interface. In case you are wondering the Dialog is set in onOptionItemSelcted.
3 Answers
Wesley Seago
10,424 PointsHi Devin. It looks like your Paint class already has a color property, and you have a setter method for it. All you would need to do is call the setColor() method on the object and pass in the value as a param for the new color. Without seeing your Paint object, I can't tell you for sure how it is setup, but I assume from looking at how it was set above that you are using the Hex Value plus the Alpha channel value. If that is the case, your switch would be case x, setColor (Hex and Alpha Param); case y, setColor (Hex and Alpha Param) etc.
Wesley Seago
10,424 PointsIn onCreate, you are ultimately adding the view. In your switch statement you are creating a new view, and setting the color, but you are not adding the view. Also, mDrawingView is a member variable. You should use a local variable to create the new view, then set mDrawingView equal to the local variable, instead of recreating the member variable with each change.
Devon McCoy
30 PointsSorry for bothering you again. But i have to tell you THANK YOU
THANK YOU , THANK YOU , THANK YOU ,THANK YOU!!!!!!
I Wish i could give you a million dollars. But i can only tell you thank you some many times. I can't tell you how long i've been working on this.
Wesley Seago you are the man!! Thank you so much
Wesley Seago
10,424 PointsYou are very welcome.
Devon McCoy
30 PointsDevon McCoy
30 Pointsi stuck code 1 in the onCreate method , it worked code 1 :
DrawingView mDrawingView=new DrawingView(this); mDrawingView.newColor(Color.GREEN); //works
But when i stick the same code in my in my Dialog.interface nothing happens
switch(witch) Code 2 case 0: { DrawingView mDrawingView=new DrawingView(PaintImageActivity.this);
mDrawingView.newColor(Color.GREEN);break; }