I'm new on anddev and i'm start programing on Android. I want to write somethings like Paint, but i have a big promblem with filling pixels on bitmap. I want to fill some space on bitmap using Bitmap.getPixel() and Bitmap.setPixel() methods.
There is my code:
Using java Syntax Highlighting
- public class Test extends View {
- private float firstX;
- private float firstY;
- private float currX;
- private float currY;
- private Path path;
- private Canvas c;
- private Paint p;
- private RectF buttonRect;
- private Paint buttonPaint;
- private boolean isFilling;
- public Test(Context c) {
- super(c);
- init();
- }
- public Test(Context c, AttributeSet as) {
- super(c, as);
- init();
- }
- public Test(Context c, AttributeSet as, int style) {
- super(c, as, style);
- init();
- }
- void init()
- {
- setFocusable(true);
- setFocusableInTouchMode(true);
- b = Bitmap.createBitmap(320, 480, Bitmap.Config.RGB_565);
- c = new Canvas(b);
- c.drawColor(Color.WHITE);
- p = new Paint();
- p.setColor(Color.BLACK);
- p.setStyle(Paint.Style.STROKE);
- p.setStrokeWidth(4);
- isFilling = false;
- buttonRect = new RectF(10, 10, 310, 40);
- buttonPaint = new Paint();
- buttonPaint.setColor(Color.GREEN);
- }
- private Bitmap b;
- @Override
- protected void onDraw(Canvas canvas) {
- Paint pp = new Paint();
- canvas.drawBitmap(b, 0, 0, pp);
- canvas.drawRect(buttonRect, buttonPaint);
- }
- @Override
- public boolean onTouchEvent(MotionEvent e) {
- if(!isFilling) {
- if(e.getAction()==MotionEvent.ACTION_DOWN) {
- firstX = e.getX();
- firstY = e.getY();
- path = new Path();
- path.moveTo(firstX, firstY);
- }
- else if(e.getAction()==MotionEvent.ACTION_MOVE) {
- currX = e.getX();
- currY = e.getY();
- path.lineTo(currX, currY);
- }
- c.drawPath(path, p);
- } else {
- //========here is problem
- fill_seed((int)e.getX(), (int)e.getY(), Color.RED, Color.WHITE);
- //=======================
- }
- if( buttonRect.contains(e.getX(), e.getY()) ) {
- buttonPaint.setColor(Color.RED);
- if(e.getAction() == MotionEvent.ACTION_UP) {
- isFilling = true;
- }
- } else {
- buttonPaint.setColor(Color.GREEN);
- }
- invalidate();
- return true;
- }
- void fill_seed(int x, int y, int colorNew,int colorOld)
- {
- {
- int ccc=b.getPixel(x, y);
- if (ccc==colorOld)
- {
- b.setPixel(x, y, colorNew);
- fill_seed (x, y-1, colorNew, colorOld);
- fill_seed (x, y+1,colorNew, colorOld);
- fill_seed (x-1, y, colorNew, colorOld);
- fill_seed (x+1, y, colorNew, colorOld);
- invalidate();
- }
- }
- }
- }
Parsed in 0.042 seconds, using GeSHi 1.0.8.4
When I try use it i get :The application <name> has stopped unexpectedly. Please try again.
Has anybody any hint how to do this??
thanks! :]
Sorry for my poor english

