Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Issues with undo functions

  1. #1
    Junior Member
    Join Date
    Apr 2013
    Posts
    21
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Issues with undo functions

    I'm trying to make a redo feature for my app, but I can't figure out what is wrong :/ The idea is to be able to undo up to 10 times, and each time you make a new modification to the image (when you pick you finger up to stop drawing) it shifts all previous images one index to make room for the newest edit and then it saves the edit in the last index of an array of bitmaps. for now I'm just drawing the 3 most recent bitmaps to see if it is working, but all the saved bitmaps are the same. Could I get some advice? Thanks!

    float lastPosX;
    float lastPosY;
    Bitmap[] previousEdits = new Bitmap[10]
     public MyView(Context c) {
                super(c);
                mBitmap = Bitmap.createBitmap(screenWidth, screenHeight, Bitmap.Config.ARGB_8888);
                mCanvas = new Canvas(mBitmap);
                currentPath = new Path();
                mBitmapPaint = new Paint(Paint.DITHER_FLAG);
                for(int i=0;i<previousEdits.length;i++){  //initialize all 10 steps previous edits to be blank bitmaps instead of null
        	        previousEdits[i]=Bitmap.createBitmap(screenWidth, screenHeight, Bitmap.Config.ARGB_8888);
            	}
            }
     
    protected void onDraw(Canvas canvas) {
                    canvas.drawColor(Color.WHITE);
                	canvas.drawBitmap(previousEdits[previousEdits.length-1], 0, 0, mBitmapPaint);  //draw the most current image
                	canvas.drawBitmap(previousEdits[previousEdits.length-2], 500, 0, mBitmapPaint); //draw second most current 
                	canvas.drawBitmap(previousEdits[previousEdits.length-3], 500, 0, mBitmapPaint); //draw third most current
            }
     public boolean onTouchEvent(MotionEvent event) {
                      float x = event.getX();
                      float y = event.getY();
                     switch (event.getAction()) {
                       case MotionEvent.ACTION_DOWN:
                               touch_start(x, y);
                                invalidate();
                                break;
                        case MotionEvent.ACTION_MOVE:
                                touch_move(x, y);
                               invalidate();
                                break;
                         case MotionEvent.ACTION_UP:
                                touch_up();
                               invalidate();
                                break;
                 }
                   return true;
            }
     private void touch_start(float x, float y) {
                currentPath.moveTo(x, y);
                lastPosX = x;
                lastPosY = y;
            }
    private void touch_move(float x, float y) {
                float dx = Math.abs(x - lastPosX);
                float dy = Math.abs(y - lastPosY);
                    currentPath.quadTo(lastPosX, lastPosY, (x + lastPosX)/2, (y + lastPosY)/2);
                	lastPosX = x;
                    lastPosY = y;        
            }
    private void touch_up() {
            	 // commit the path to our offscreen
            	if(canvasInUse) {
            		mCanvas.drawPath(currentPath, drawingPaint);
            	}
                // kill this so we don't double draw
                currentPath.reset();
               //update the previous bitmaps
                updateLatestEdits(mBitmap);
            }
     
    void updateLatestEdits(Bitmap latestEdit){    	
        		for(int i =0;i<previousEdits.length-1;i++){ //shift bitmaps to make room for new edit
        			previousEdits[i]=previousEdits[i+1];
        		}
        		previousEdits[previousEdits.length-1]=latestEdit; //put in new edit at the end
        	}


  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Issues with undo functions

    From the look of it the only Bitmap instance you add to the array is mBitmap so it ends up occupying every slot in the array. (Or, to speak more correctly, a reference to that bitmap occupies every slot.)

  3. #3
    Junior Member
    Join Date
    Apr 2013
    Posts
    21
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Issues with undo functions

    that makes sense... man I'm slipping. So if I wanted to put an actual unique bitmap in each slot in the array rather than only the reference, what should I do? something like
    previousEdits[previousEdits.length-1]=Bitmap.createBitmap(mBitmap); //?
    Like this?

    --- Update ---

    Solved it!!
    Instead of having the last line of the updateLatestEdits method be
    previousEdits[previousEdits.length-1]=latestEdit;
    I changed it to
    previousEdits[previousEdits.length-1]=latestEdit.copy(Bitmap.Config.ARGB_8888, true);
    So now each slot in the array has it's own unique bitmap, instead of all having the references to one. Thanks!!

  4. #4
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Issues with undo functions

    Try it and see. But from the sound of the name createBitmap() is good: you are still dealing with a reference but now it's a reference to a newly created bitmap.

    If mBitmap is not being used as an instance variable you could remove it.

    ---

    [edit] Just saw the update. Copy also sounds good. I'm glad you've got it sorted out.

Similar Threads

  1. Help implementing an undo function
    By JaeDuck in forum Object Oriented Programming
    Replies: 2
    Last Post: February 18th, 2013, 07:31 AM
  2. Problem implementing an undo function.
    By JaeDuck in forum Object Oriented Programming
    Replies: 0
    Last Post: February 17th, 2013, 09:15 PM
  3. undo delete method
    By Trunk Monkeey in forum Object Oriented Programming
    Replies: 7
    Last Post: February 7th, 2012, 03:49 PM
  4. Disabling undo button in Word.
    By Silversurfer21 in forum Java Theory & Questions
    Replies: 0
    Last Post: September 5th, 2011, 09:03 AM
  5. [SOLVED] how do I undo an AlphaComposite?
    By gib65 in forum AWT / Java Swing
    Replies: 3
    Last Post: October 21st, 2010, 04:23 PM

Tags for this Thread