path(s) on canvas -> different colors?

Put your problem here if it does not fit any of the other categories.

path(s) on canvas -> different colors?

Postby relsieg » Wed Feb 03, 2010 10:18 pm

Hello,

i was wondering how one can draw on a canvas and use different colors?
I.e. you draw a yellow path and after clickling a button you draw again but with another color.
Currently, after changing the color (by button click) the old path(es) change color, too.
This confuses me because I create a new Paint() object (with a new color) after button click.

Here are the important code snippets:

The paint object
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.         public void setMpaint(int color) {
  3.  
  4.         mPaint = new Paint();
  5.  
  6.         mPaint.setDither(false);
  7.  
  8.         mPaint.setColor(color);
  9.  
  10.         mPaint.setStyle(Paint.Style.STROKE);
  11.  
  12.         mPaint.setStrokeJoin(Paint.Join.ROUND);
  13.  
  14.         mPaint.setStrokeCap(Paint.Cap.ROUND);
  15.  
  16.         mPaint.setStrokeWidth(6);
  17.  
  18.        
  19.  
  20.         }
  21.  
  22.  
Parsed in 0.031 seconds, using GeSHi 1.0.8.4

gets called at the beginning of the Activity and everytime the button is clicked.

the onDraw method
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.                 @Override
  3.  
  4.         public void onDraw(final Canvas canvas) {
  5.  
  6.            
  7.  
  8.                         for (Path path : _graphics) {
  9.  
  10.                                 canvas.drawPath(path, mPaint);
  11.  
  12.                         }
  13.  
  14.                 }
  15.  
  16.  
Parsed in 0.031 seconds, using GeSHi 1.0.8.4

I have a couple of coords and draw a (couple of ...) path.

I thought when creating a new Paint() object (after button click), the old path (which was drawn with the old Paint() object) is not influenced. Am I wrong?

Thank you,
R.
relsieg
Freshman
Freshman
 
Posts: 9
Joined: Fri Jul 24, 2009 10:06 pm

Top

Postby BUKsAPPs » Wed Feb 03, 2010 10:32 pm

Each time you call "mPaint = new Paint();" you reassign the mPaint variable to the newly created Paint object. This removes the reference you previously had to the older paint object, and it goes to the garbage collector.

In your onDraw method, each path is drawn with the mPaint object. The variable mPaint only points to a single object, the last one created. So all paths get drawn with the same paint object and therefor the same color.

If you want each path to be drawn with its paint object, you need to do something to keep the paint object around. One option would be to create a collection or array of paint objets just like you do for the path objects, and retrieve the associated paint object for each path when it is drawn. But another option would be to create a new class to hold your paint object and path together, and keep a collection/array of your new class.
BUKsAPPs
Claustrum Lite - Area capture game
R:P:S Academy - play Rock/Paper/Scissors online
BUKsAPPs
Experienced Developer
Experienced Developer
 
Posts: 71
Joined: Sat Jan 16, 2010 9:01 pm
Location: USA

Postby relsieg » Wed Feb 03, 2010 11:18 pm

Hi BUKsAPPs,

thank you for your quick reply.

I've made a quick test but got the same result:

The list for the Paint() objects:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. private List<Paint> paintList = new ArrayList<Paint>();
  3.  
  4.  
Parsed in 0.034 seconds, using GeSHi 1.0.8.4


add the object to the list everytime the method is called
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.         public void setMpaint(int color) {
  3.  
  4.         mPaint = new Paint();
  5.  
  6.         mPaint.setDither(false);
  7.  
  8.         mPaint.setColor(color);
  9.  
  10.         mPaint.setStyle(Paint.Style.STROKE);
  11.  
  12.         mPaint.setStrokeJoin(Paint.Join.ROUND);
  13.  
  14.         mPaint.setStrokeCap(Paint.Cap.ROUND);
  15.  
  16.         mPaint.setStrokeWidth(6);
  17.  
  18.         paintList.add(mPaint);
  19.  
  20.         }
  21.  
  22.  
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


use the latest Paint() object
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.                 @Override
  3.  
  4.         public void onDraw(final Canvas canvas) {
  5.  
  6.                         int listLength = paintList.size();
  7.  
  8.                         listLength = listLength - 1;
  9.  
  10.                        
  11.  
  12.                         Paint currentPaintObject = paintList.get(listLength);
  13.  
  14.                        
  15.  
  16.                         for (Path path : _graphics) {
  17.  
  18.                                 canvas.drawPath(path, currentPaintObject);
  19.  
  20.                         }
  21.  
  22.                 }
  23.  
  24.  
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


I guess the reason is - as you've said - because Paint() is a single object.


But another option would be to create a new class to hold your paint object and path together, and keep a collection/array of your new class.

I'm not quite sure how to realize that (in conjunction with the iterator).


regards,
R.
relsieg
Freshman
Freshman
 
Posts: 9
Joined: Fri Jul 24, 2009 10:06 pm

Postby BUKsAPPs » Thu Feb 04, 2010 1:43 am

Your onDraw is still dawing all of the paths with the last paint object(currentPaintObject). Try something more like:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.         @Override
  3.  
  4.         public void onDraw(final Canvas canvas) {
  5.  
  6.                int iIndex = 0
  7.  
  8.                Paint currentPaintObject;
  9.  
  10.                
  11.  
  12.                for (Path path : _graphics) {
  13.  
  14.                     currentPaintObject = paintList.get(iIndex);
  15.  
  16.                     canvas.drawPath(path, currentPaintObject);
  17.  
  18.                     iIndex++;
  19.  
  20.                }
  21.  
  22.           }
Parsed in 0.037 seconds, using GeSHi 1.0.8.4

That way each path will be redrawn each onDraw with the paint object that was created for it. This code does not do any checking to make sure that paintList length is the same as the path count, but it should be if I understand how you are creating your paint objects. This is why I recomend creating a class to hold a path and its paint object, and keeping a list of the class. That way each path will always have its own paint object.
BUKsAPPs
Claustrum Lite - Area capture game
R:P:S Academy - play Rock/Paper/Scissors online
BUKsAPPs
Experienced Developer
Experienced Developer
 
Posts: 71
Joined: Sat Jan 16, 2010 9:01 pm
Location: USA

Postby relsieg » Thu Feb 04, 2010 8:39 am

Ah okay, it makes sense now :-)
I'll try it as soon as I'm back from work.

Thank you.
relsieg
Freshman
Freshman
 
Posts: 9
Joined: Fri Jul 24, 2009 10:06 pm

Postby relsieg » Thu Feb 04, 2010 9:30 pm

Sooo ...
It's done :-)

Everytime I add a Path oject to the pathlist, I create a Paint object and add it to another list:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.         public boolean onTouchEvent(MotionEvent event) {
  3.  
  4.             synchronized (_thread.getSurfaceHolder()) {
  5.  
  6.                 if(event.getAction() == MotionEvent.ACTION_DOWN){
  7.  
  8.                         path = new Path();
  9.  
  10.                         path.moveTo(event.getX(), event.getY());
  11.  
  12.                         path.lineTo(event.getX(), event.getY());
  13.  
  14.                 }else if(event.getAction() == MotionEvent.ACTION_MOVE){
  15.  
  16.                         setMpaint(); // create Paint object and add to the paintlist
  17.  
  18.                         path.lineTo(event.getX(), event.getY());
  19.  
  20.                         _graphics.add(path);
  21.  
  22.                         path = new Path();
  23.  
  24.                         path.moveTo(event.getX(), event.getY());
  25.  
  26. ..
  27.  
  28.  
Parsed in 0.038 seconds, using GeSHi 1.0.8.4


Thank you very much for your help :-)
relsieg
Freshman
Freshman
 
Posts: 9
Joined: Fri Jul 24, 2009 10:06 pm

Top

Re: path(s) on canvas -> different colors?

Postby thanhtam_it » Mon Jan 14, 2013 6:46 am

Can you share the source code with me?
I have the same problem but i can solve it.

Thansk in advance!
thanhtam_it
Once Poster
Once Poster
 
Posts: 1
Joined: Mon Jan 14, 2013 6:43 am

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: Google [Bot] and 33 guests