Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8643729
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T12:07:22+00:00 2026-06-12T12:07:22+00:00

I would like to implement undo/redo in a small paint application . It seems

  • 0

I would like to implement undo/redo in a small paint application. It seems the Command Pattern fits the use nicely, but I am unsure how to best implement it.

As I understand the pattern, it is necessary to include in each command:

  1. The details of the paint operation for purposes of redo (e.g. Line -> start & end points, free form line -> GeneralPath)
  2. The state of the component prior to the change for undo. In this case, that will be a small snapshot image of the area affected by the command.

My understanding based on that is that each command needs to be ‘atomic’ or self contained, with all the information needed for undo/redo that operation.

Unfortunately that would require storing more information than I’d first anticipated. For a line we must also account for things like the Color, Stroke and RenderingHints used to draw it initially. This turns my ‘simple little commands’ into something ..more bulky in memory, and with more boiler-plate code to churn out (each will be a serializable bean1).

For reasons of memory conservation (mostly) I was wanting to ‘cheat’ on the specification of the commands. Perhaps take a backup of the entire drawing area every 100th update, but otherwise store no part of the changed image, and simply rebuild the last (up to) 100 commands for each new paint operation. But that seems problematic to ensure that the state of the Graphics object is right before painting each part – this part might require a line, but the RenderingHints were changed 4 commands ago, the Color was changed 98 commands ago, while the Stroke has remained the same for the last 227 commands.

Pursuing a more memory efficient command seems to throw the pattern right out the window in terms of being ‘atomic’. That in turn leads to difficulties in determining the earliest command that might affect the rendering.

Should I:

  • Look for a new pattern?
  • Attempt to implement my peculiar needs by tweaking the pattern?
  • Toss all this in the waste bin as premature optimization and code it in the simplest (and most memory consuming) way that sticks to the command pattern as defined?

Update

  1. "each will be a serializable bean" On 2nd thoughts, no. I did dome checks to find that a Graphics2D (which neatly encapsulates many parameters used when drawing) is not serializable. Further, a BasicStroke is serializable, but the thickness of the stroke is not stored. I could create serializable versions of many of the attributes but it seems to make for a lot more code, so I’m going to abandon that spec. as well. I will only attempt to store a reference to a BufferedImage at run-time.
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T12:07:24+00:00Added an answer on June 12, 2026 at 12:07 pm

    I would stick with command pattern and first try a naive solution (=the most memory-hungry). For some graphical operations it may be even necessary to keep a copy of the entire image in the command object (eg. think of filters). This is a common problem also in professional image editing applications, they often have a memory or step limit of last commands that are remembered. And if the memory consumption is really large you may think of swapping the oldest entries in command-history to file system. I think user will not mind waiting a second until the change is undone.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to implement an undo/redo feature into my application, using the Command Pattern
I need to implement Undo/Redo frame work for my window application(editor like powerpoint), what
I have a TextBox which I would like to implement undo/redo functionality for. I
I would like to implement a data access object pattern in C++, but preferably
I would like to implement a command line interface for a Java application. This
I would like to implement a simple AR desktop application. This application should first
I would like to implement some Drag-select functionality into a project of mine but
I would like to implement a Layer Supertype pattern ((PoEAA) when using EF 4.x.
I would like to implement an application to receive a file from a Bluetooth
I would like to implement ABAP Unit tests in my ABAP programs, but my

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.