Skip to content Skip to sidebar Skip to footer

Fill The Complete Canvas But Keep The Bound Fill Area As It Is Like Circle, Rectangle

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 backgroun

Solution 1:

finalPointp1=newPoint();
                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  newTheTask(yourbitmap, p1, sourceColor,targetColor).execute();// use asyntask for efficiencyclassTheTaskextendsAsyncTask<Void, Integer, Void> {

    Bitmap bmp;
    Point pt;
    int replacementColor,targetColor;
    ProgressDialog pd;
 publicTheTask(Bitmap bm,Point p, int sc, int tc)
 {
this.bmp=bm;
this.pt=p;
this.replacementColor=tc;
this.targetColor=sc;
pd= newProgressDialog(context);
pd.setMessage("Filling....");
    }
    @OverrideprotectedvoidonPreExecute() {
            pd.show();

    }

    @OverrideprotectedvoidonProgressUpdate(Integer... values) {

    }

    @Overrideprotected Void doInBackground(Void... params) {
        FloodFill f= newFloodFill();
        f.floodFill(bmp,pt,targetColor,replacementColor);
        returnnull;
    }

    @OverrideprotectedvoidonPostExecute(Void result) { 
pd.dismiss();
invalidate();
    }

Finally use a FloodFill algorithm to fill a closed area

publicclassFloodFill {
publicvoidfloodFill(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 = newLinkedList<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(newPoint(x, y - 1));
                    spanUp = true;
                } elseif (spanUp && y > 0
                        && image.getPixel(x, y - 1) != target) {
                    spanUp = false;
                }
                if (!spanDown && y < height - 1
                        && image.getPixel(x, y + 1) == target) {
                    queue.add(newPoint(x, y + 1));
                    spanDown = true;
                } elseif (spanDown && y < height - 1
                        && image.getPixel(x, y + 1) != target) {
                    spanDown = false;
                }
                x++;
            }
        } while ((node = queue.poll()) != null);
    }
}
}

enter image description here

Solution 2:

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

publicclasstttextendsView {
MyShape myShape;
publicttt(Context context) {
    super(context);
    // TODO Auto-generated constructor stub
    myShape = newMyShape();
    Paintpaint=newPaint();
    paint.setColor(Color.WHITE);
    myShape.setPaint(paint);
}

@OverrideprotectedvoidonDraw(Canvas canvas) {
    // TODO Auto-generated method stubsuper.onDraw(canvas);
    myShape.onDraw(canvas);     
}

@OverridepublicbooleanonTouchEvent(MotionEvent event) {
    // TODO Auto-generated method stubintx= (int) event.getX();
    inty= (int) event.getY();

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

    default:
        break;
    }

    returnsuper.onTouchEvent(event);
}

classMyShape {
    private Paint paint;
    publicMyShape() {
        // TODO Auto-generated constructor stub
    }
    publicvoidonDraw(Canvas canvas){
        canvas.drawCircle(15, 15, 30, getPaint());
    }
    /**
     * @param paint the paint to set
     */publicvoidsetPaint(Paint paint) {
        this.paint = paint;
    }
    /**
     * @return the paint
     */public Paint getPaint() {
        return paint;
    }

}

}

Solution 3:

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.

Post a Comment for "Fill The Complete Canvas But Keep The Bound Fill Area As It Is Like Circle, Rectangle"