Java:Rectangle类的撤销和重做方法

huangapple go评论65阅读模式
英文:

Java: Undo and Redo methods for a Rectangle class

问题

我有一个自定义的矩形(Rectangle)类:

public class Rectangle {
    private int height, width, x, y;
    private Color color;

    public Rectangle() {
        this.height = 0;
        this.width = 0;
        this.x = 0;
        this.y = 0;
        this.color = null;
    }

    public void setHeight(int h) { this.height = h; }

    public void setWidth(int w) { this.width = w; }

    public void setX(int x) { this.x = x; }

    public void setY(int y) { this.y = y; }

    public void setColor(Color c) { this.color = c; }

    public int getWidth() { return this.width; }

    public int getHeight() { return this.height; }

    public int getX() { return this.x; }

    public int getY() { return this.y; }

    public Color getColor() { return this.color; }

    public void undo() {   }

    public void redo() {   }
}

如何在不要求用户提供上一步使用的方法的情况下,实现此类的撤销(undo)和重做(redo)功能,使得矩形能够回到之前的状态?我有一个模糊的想法,涉及使用栈(stack),但我在如何编写实际代码方面遇到了困难。我的第二个问题是,我不确定我的构造函数是否正确,我将所有变量初始化为 null,而没有提供任何参数,因为我希望用户使用 getters 和 setters 来设置值。请帮忙。

英文:

I have a custom Rectangle class:

public class Rectangle () {
private int height, width, x, y;
private Color color;
public Rectangle () {
this.height = null;
this.width = null;
this.x = null;
this.y = null;
this.color = null;
}
public void setHeight(int h) { this.height = h; }
public void setWidth(int w) { this.width = w; }
public void setX(int x) { this.x = x; }
public void setY(int y) { this.y = y; }
public void setColor(Color c) { this.color = c; }
public int getWidth() { return this.width; }
public int getHeight() { return this.height; }
public int getX() { return this.x; }
public int getY() { return this.y; }
public Color getColor() { return this.color; }
public void undo() {   }
public void redo() {   }
}

How would I go about implementing the undo and redo functions for this class in a way that it should be able to revert the rectangle to its previous state without the user mentioning what method was last used. I have a vague idea that involves using a stack but I'm stuck on how to actually code it. My second question is I'm not sure if my constructor is correct, I initialize everything to null without giving any parameters because I want people to use the getters/setters instead. Please help.

答案1

得分: 1

你可以将状态保存在一个数组中,并从中恢复矩形。

public class Rectangle () {

    private states: List<Rectangle> = new ArrayList(); // 我们在每次更新任何属性时都会保存矩形的状态
    private int stateIndex = 0; // 这是当前矩形上活动状态的索引

    private int height, width, x, y;
    private Color color;

    public Rectangle () {
        this.height = null;
        this.width = null;
        this.x = null;
        this.y = null;
        this.color = null;
    }

    public void setHeight(int h) { 
        this.stateIndex = states.size() + 1; // 当添加新状态到状态数组时,我们将状态索引增加一
        this.height = h; 
        this.states.add(this); // 在设置任何属性值后添加状态
    }

    public void undo() { 
        if(this.stateIndex > 0){ // 仅在当前状态之前有可用状态时才进行撤消操作
            this.stateIndex--; // 将当前索引减少一
            this.height = this.states.get(stateIndex).height; // 从状态中设置属性
            ...
        }
    }

    public void redo() {    
        if(this.stateIndex < states.size()){ // 可以在缓存中的可用状态之上继续进行
            this.stateIndex++; // 增加当前状态索引
            this.height = this.states.get(stateIndex).height; // 更新属性值
            ...
        }
    }

}
英文:

you can saave the state in an array and restore the rectangle from it

public class Rectangle () {
private states: List&lt;Rectangle&gt; = new ArrayList(); // we save the state of rectangle on every update to any property
private int stateIndex = 0; // this is the index of the state which is active on this rectangle
private int height, width, x, y;
private Color color;
public Rectangle () {
this.height = null;
this.width = null;
this.x = null;
this.y = null;
this.color = null;
}
public void setHeight(int h) { 
this.stateIndex = states.size() + 1; // we ll increase one in state index as new state is added to the states
this.height = h; 
this.states.add(this); // add the state after you set the value of any property
}
public void undo() { 
if(this.stateIndex &gt; 0){ // only undo when there is state available before the current state
this.stateIndex--; // reduce the current index by one
this.height = this.states.get(stateIndex).height; // set the properties from state
...
}
}
public void redo() {    
if(this.stateIndex &lt; states.size()){ // can go more the available states in cache
this.stateIndex++; // increase the current state index
this.height = this.states.get(stateIndex).height; // update the values of the properties
...
}
}
}

huangapple
  • 本文由 发表于 2020年10月16日 13:26:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/64383350.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定