5

possible duplicate

Hello friends,

I creating paint application, I have problem in that. If I draw the rectangle without fill and or another like bound area and change the background color then rectangle fill area also change means whole canvas color will be filled with the new background color. How to keep the background or fill the canvas area which was not bound, here is the image

this is the initial image

enter image description here

after change the background color getting this result

enter image description here

but how to getting like this way

enter image description here

Charles
  • 50,943
  • 13
  • 104
  • 142
Pratik
  • 30,639
  • 18
  • 84
  • 159

3 Answers3

3
final Point p1 = new Point();
                p1.x=(int) x; //x co-ordinate where the user touches on the screen
                p1.y=(int) y; //y co-ordinate where the user touches on the screen  
 new TheTask(yourbitmap, p1, sourceColor,targetColor).execute();// use asyntask for efficiency

 class TheTask extends AsyncTask<Void, Integer, Void> {

    Bitmap bmp;
    Point pt;
    int replacementColor,targetColor;
    ProgressDialog pd;
 public TheTask(Bitmap bm,Point p, int sc, int tc)
 {
this.bmp=bm;
this.pt=p;
this.replacementColor=tc;
this.targetColor=sc;
pd= new ProgressDialog(context);
pd.setMessage("Filling....");
    }
    @Override
    protected void onPreExecute() {
            pd.show();

    }

    @Override
    protected void onProgressUpdate(Integer... values) {

    }

    @Override
    protected Void doInBackground(Void... params) {
        FloodFill f= new FloodFill();
        f.floodFill(bmp,pt,targetColor,replacementColor);
        return null;
    }

    @Override
    protected void onPostExecute(Void result) { 
pd.dismiss();
invalidate();
    }

Finally use a FloodFill algorithm to fill a closed area

    public class FloodFill {
public void floodFill(Bitmap  image, Point node, int targetColor,
        int replacementColor) {
    int width = image.getWidth();
    int height = image.getHeight();
    int target = targetColor;
    int replacement = replacementColor;
    if (target != replacement) {
        Queue<Point> queue = new LinkedList<Point>();
        do {
            int x = node.x;
            int y = node.y;
            while (x > 0 && image.getPixel(x - 1, y) == target) {
                x--;
            }
            boolean spanUp = false;
            boolean spanDown = false;
            while (x < width && image.getPixel(x, y) == target) {
                image.setPixel(x, y, replacement);
                if (!spanUp && y > 0 && image.getPixel(x, y - 1) == target) {
                    queue.add(new Point(x, y - 1));
                    spanUp = true;
                } else if (spanUp && y > 0
                        && image.getPixel(x, y - 1) != target) {
                    spanUp = false;
                }
                if (!spanDown && y < height - 1
                        && image.getPixel(x, y + 1) == target) {
                    queue.add(new Point(x, y + 1));
                    spanDown = true;
                } else if (spanDown && y < height - 1
                        && image.getPixel(x, y + 1) != target) {
                    spanDown = false;
                }
                x++;
            }
        } while ((node = queue.poll()) != null);
    }
}
}

enter image description here

Raghunandan
  • 132,755
  • 26
  • 225
  • 256
  • Link to screen shot with background changed http://tinypic.com/view.php?pic=2a9a891&s=6. Hope this solves your problem. – Raghunandan Oct 08 '12 at 08:27
  • You can change background and fill the closed area with the color you want. – Raghunandan Oct 08 '12 at 08:31
  • @TalhaQ you can't this answer and code is to solve a specific problem. If you are looking for code sorry that is not how this site works. – Raghunandan Apr 05 '14 at 18:07
  • you are right but i cant find the mainactivity from where you initialize this code – Talha Q Apr 05 '14 at 18:10
  • @TalhaQ this code is not for you. its for solving specific problem posted by the question user. And nobody is going to give you complete code. that's not how stackoverflow works sorry – Raghunandan Apr 05 '14 at 18:12
  • @TalhaQ try posting your problem as a new question and you might find an answer. But do not ask for complete code you will not get. you can dicuss how the site works @ meta.stackoverflow.com – Raghunandan Apr 05 '14 at 18:16
  • it not able to fill the particular object , it filling full screen @Raghunathan – Vigneshwaran T Oct 11 '17 at 10:42
  • @VigneshwaranT how am i suppose to know without looking at the code. Secondly you post your code – Raghunandan Oct 11 '17 at 11:26
0

You might also want to look at using the clip region as in these 2 questions:

Depending on what you want to do, they may be useful.

I found your question because I didn't know what to google for, and the answer I wanted was not using Flood Fill.

Community
  • 1
  • 1
Richard Le Mesurier
  • 29,432
  • 22
  • 140
  • 255
0

here is the code (you have to bound the shape on touch event otherwise it change the color of shape works):

public class ttt extends View {
MyShape myShape;
public ttt(Context context) {
    super(context);
    // TODO Auto-generated constructor stub
    myShape = new MyShape();
    Paint paint = new Paint();
    paint.setColor(Color.WHITE);
    myShape.setPaint(paint);
}

@Override
protected void onDraw(Canvas canvas) {
    // TODO Auto-generated method stub
    super.onDraw(canvas);
    myShape.onDraw(canvas);     
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    // TODO Auto-generated method stub
    int x = (int) event.getX();
    int y = (int) event.getY();

    switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN:
        Paint paint = new Paint();
        paint.setColor(Color.BLUE);
        myShape.setPaint(paint);
        invalidate();
        break;

    default:
        break;
    }

    return super.onTouchEvent(event);
}

class MyShape {
    private Paint paint;
    public MyShape() {
        // TODO Auto-generated constructor stub
    }
    public void onDraw(Canvas canvas){
        canvas.drawCircle(15, 15, 30, getPaint());
    }
    /**
     * @param paint the paint to set
     */
    public void setPaint(Paint paint) {
        this.paint = paint;
    }
    /**
     * @return the paint
     */
    public Paint getPaint() {
        return paint;
    }

}

}

Hiren Dabhi
  • 3,693
  • 5
  • 37
  • 59
  • I have set the paint object like this way and whatever shape I have drawn that style was set to only Stroke not the fill. If I set as fill and stroke then both color are same – Pratik Jan 04 '12 at 09:43