撤消绘制程序的方法

时间:2014-05-12 11:45:32

标签: java graphics awt

我正在尝试在我的某个程序中创建一个小绘图板。这是班级:

class DrawPad extends JComponent {

Image image;
Graphics2D graphics;
int currentX, currentY, oldX, oldY;

public DrawPad() {
    setDoubleBuffered(false);        
    addMouseListener(new MouseAdapter() {
        @Override
        public void mousePressed(MouseEvent e) {
            oldX = e.getX();
            oldY = e.getY();
        }
    });

    addMouseMotionListener(new MouseMotionAdapter() {
        @Override
        public void mouseDragged(MouseEvent e) {
            currentX = e.getX();
            currentY = e.getY();
            if (graphics != null) {
                graphics.drawLine(oldX, oldY, currentX, currentY);
            }
            repaint();
            oldX = currentX;
            oldY = currentY;
        }           
    });
}

@Override
public void paintComponent(Graphics g) {
    super.paintComponent(g);
    if (image == null) {
        image = createImage(getSize().width, getSize().height);
        graphics = (Graphics2D) image.getGraphics();
        graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        clear();
    }
    g.drawImage(image, 0, 0, null);
}

public void clear() {
    graphics.setPaint(Color.white);
    graphics.fillRect(0, 0, getSize().width, getSize().height);
    graphics.setPaint(Color.black);
    repaint();
}

public void undo() {
    // restore previous graphics here
}

public void red() {
    graphics.setPaint(Color.red);
    repaint();
}

public void black() {
    graphics.setPaint(Color.black);
    repaint();
}

public void magenta() {
    graphics.setPaint(Color.magenta);
    repaint();
}

public void blue() {
    graphics.setPaint(Color.blue);
    repaint();
}

public void green() {
    graphics.setPaint(Color.green);
    repaint();
}

}

我将它添加到JPanel。绘图本身工作正常。现在我想实现一个撤消方法。我想我可以将当​​前的graphics2D对象复制到备份变量中,单击undo-button我会用备份对象替换当前对象。 不幸的是,这没有用。

我尝试了以下内容:

在mousePressed方法中,我将图形的当前值分配给备份变量。

addMouseListener(new MouseAdapter() {
    @Override
    public void mousePressed(MouseEvent e) {
        graphicsBackup = graphics.create();
        oldX = e.getX();
        oldY = e.getY();
    }
});

在撤消方法中,我尝试将备份变量的引用分配给原始对象。

public void undo() {
    graphics = graphicsBackup;
    repaint();
}

在我的第二次尝试中,我使用了AffineTransform对象。我在keyPressed方法中调用getTransform来获取当前状态,然后在undo方法中调用setTransform。 这也没有用。你有什么建议吗?


解决方案:(为背景添加了一些其他方法等)

class DrawPad extends JComponent {

private Image image;
private Image background;
private Graphics2D graphics;
private int currentX, currentY, oldX, oldY;
private final SizedStack<Image> undoStack = new SizedStack<>(12);

public DrawPad() {
    setDoubleBuffered(false);
    addMouseListener(new MouseAdapter() {
        @Override
        public void mousePressed(MouseEvent e) {
            saveToStack(image);
            oldX = e.getX();
            oldY = e.getY();
        }
    });

    addMouseMotionListener(new MouseMotionAdapter() {
        @Override
        public void mouseDragged(MouseEvent e) {
            currentX = e.getX();
            currentY = e.getY();
            if (graphics != null) {
                graphics.drawLine(oldX, oldY, currentX, currentY);
            }
            repaint();
            oldX = currentX;
            oldY = currentY;
        }
    });
}

@Override
public void paintComponent(Graphics g) {
    super.paintComponent(g);
    if (image == null) {
        image = createImage(getSize().width, getSize().height);
        graphics = (Graphics2D) image.getGraphics();
        graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        clear();
    }
    g.drawImage(image, 0, 0, getWidth(), getHeight(), null);
}

public void clear() {
    if (background != null) {
        setImage(copyImage(background));
    } else {
        graphics.setPaint(Color.white);
        graphics.fillRect(0, 0, getSize().width, getSize().height);
        graphics.setPaint(Color.black);
    }
    repaint();
}

public void undo() {
    if (undoStack.size() > 0) {
        setImage(undoStack.pop());
    }
}

private void setImage(Image img) {
    graphics = (Graphics2D) img.getGraphics();
    graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    graphics.setPaint(Color.black);
    image = img;
    repaint();
}

public void setBackground(Image img) {
    background = copyImage(img);
    setImage(copyImage(img));
}

private BufferedImage copyImage(Image img) {
    BufferedImage copyOfImage = new BufferedImage(getSize().width, getSize().height, BufferedImage.TYPE_INT_RGB);
    Graphics g = copyOfImage.createGraphics();
    g.drawImage(img, 0, 0, getWidth(), getHeight(), null);
    return copyOfImage;
}

private void saveToStack(Image img) {
    undoStack.push(copyImage(img));
}

public void red() {
    graphics.setPaint(Color.red);
    repaint();
}

public void black() {
    graphics.setPaint(Color.black);
    repaint();
}

public void magenta() {
    graphics.setPaint(Color.magenta);
    repaint();
}

public void blue() {
    graphics.setPaint(Color.blue);
    repaint();
}

public void green() {
    graphics.setPaint(Color.green);
    repaint();
}
}

SizedStack:

public class SizedStack<T> extends Stack<T> {

private final int maxSize;

public SizedStack(int size) {
    super();
    this.maxSize = size;
}

@Override
public Object push(Object object) {
    while (this.size() > maxSize) {
        this.remove(0);
    }
    return super.push((T) object);
}
}

1 个答案:

答案 0 :(得分:4)

我自己实施了一个绘图程序。以下是我实现undo / redo的方法:

我有两个堆栈。 '撤消'堆栈和'重做'堆栈。每次用户绘制内容时,我都会这样做:

  1. 应用绘制到图像的图形之前,我将图像保存到“撤消”堆栈。
  2. 应用绘制的图形后,我将图像再次保存到“撤消”堆栈。
  3. 例如,如果用户想要绘制一条线,我会在将线应用到图像之前将当前图像保存到堆栈。在应用该行后,我将其再次保存到堆栈中。

    当用户按下“撤消”时,我只需从“撤销”堆栈中弹出顶部图像,然后将其绘制到屏幕上。然后我把它放在'重做'堆栈中。

    当用户按下“重做”时,“重做”堆栈中的顶部图像会弹出并应用于绘图区域,并置于“撤消”堆栈的顶部(反向操作)。

    编辑:为了将图像推入堆栈(堆栈中的所有对象都不是对同一对象的引用),您需要先将图像复制到新图像。这样的事情(细节可能不准确):

    void saveToStack(BufferedImage img){ // makes a copy of img and puts on stack.
        BufferedImage imageForStack = new BufferedImage(img.getWidth(), img.getHeight(), img.getType());
        Graphics2D g2d = imageForStack.createGraphics();
        g2d.drawImage(img, 0, 0, null);
        undoStack.push(imageForStack);
    }
    

    希望这有帮助。