import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import static java.
lang.
Math.
*;
class Log {
int shape = -1;
int startX = -1, startY = -1, endX = -1, endY = -1, rad = -1, width = -1, height = -1;
public Log(int shape, int startX, int startY, int endX, int endY, int rad, int width, int height) {
this.shape = shape;
this.startX = startX;
this.startY = startY;
this.endX = endX;
this.endY = endY;
this.rad = rad;
this.width = width;
this.height = height;
}
}
int shape = -3;
int startX, startY, endX, endY, rad, width, height;
ArrayList<Log> logs;
int now = 0;
public MyPanel() {
this.
btnUndo = new JButton("Undo"); this.btnUndo.setEnabled(false);
this.btnUndo.addActionListener(this);
this.
btnRedo = new JButton("Redo"); this.btnRedo.setEnabled(false);
this.btnRedo.addActionListener(this);
p.add(btnUndo);
p.add(btnRedo);
this.add(p);
this.logs = new ArrayList<Log>();
}
if(source == btnUndo) {
shape = -2;
repaint();
} else if(source == btnRedo) {
shape = -1;
repaint();
}
}
@Override
public void paint
(Graphics g
) { super.paint(g);
if(shape == -2) {
now -= 1;
} else if(shape == -1) {
now += 1;
}
for(int i=0; i<now; i++) {
Log log = logs.get(i);
if(log.shape == 0) {
g.fillArc(log.startX, log.startY, log.rad, log.rad, 0, 360);
} else if(log.shape == 1) {
g.fillRect(log.startX, log.startY, log.width, log.height);
} else if(log.shape == 2) {
g.drawLine(log.startX, log.startY, log.endX, log.endY);
}
}
if(shape == 0) {
g.fillArc(startX, startY, rad, rad, 0, 360);
if(now + 1 == logs.size() - 1) {
logs.add(new Log(0, startX, startY, -1, -1, rad, -1, -1));
} else {
for(int i=now; i<logs.size(); ) {
logs.remove(i);
}
logs.add(new Log(0, startX, startY, -1, -1, rad, -1, -1));
}
now = logs.size();
} else if(shape == 1) {
g.fillRect(startX, startY, width, height);
if(now + 1 == logs.size() - 1) {
logs.add(new Log(1, startX, startY, -1, -1, -1, width, height));
} else {
for(int i=now; i<logs.size(); ) {
logs.remove(i);
}
logs.add(new Log(1, startX, startY, -1, -1, -1, width, height));
}
now = logs.size();
} else if(shape == 2) {
g.drawLine(startX, startY, endX, endY);
if(now + 1 == logs.size() - 1) {
logs.add(new Log(2, startX, startY, endX, endY, -1, -1, -1));
} else {
for(int i=now; i<logs.size(); ) {
logs.remove(i);
}
logs.add(new Log(2, startX, startY, endX, endY, -1, -1, -1));
}
now = logs.size();
}
if(now > 0) { this.btnUndo.setEnabled(true); }
else { this.btnUndo.setEnabled(false); }
if(now == logs.size()) { this.btnRedo.setEnabled(false); }
else { this.btnRedo.setEnabled(true); }
}
}
MyPanel pnlCenter;
int startX, startY, endX, endY;
public MyFrame() {
this.setSize(800, 600);
this.
setDefaultCloseOperation(JFrame.
EXIT_ON_CLOSE); this.pnlCenter = new MyPanel();
this.pnlCenter.addMouseListener(this);
this.radCircle.setSelected(true);
group.add(radCircle);
group.add(radRectangle);
group.add(radLine);
pnlNorth.add(radCircle);
pnlNorth.add(radRectangle);
pnlNorth.add(radLine);
}
@Override
public void mouseClicked
(MouseEvent e
) {} @Override
public void mouseExited
(MouseEvent e
) {} @Override
public void mouseEntered
(MouseEvent e
) {} @Override
public void mousePressed
(MouseEvent e
) { startX = e.getX();
startY = e.getY();
}
@Override
public void mouseReleased
(MouseEvent e
) { endX = e.getX();
endY = e.getY();
if(radCircle.isSelected()) {
pnlCenter.shape = 0;
if(endY < startY || startX < startY) { return; }
pnlCenter.startX = startX;
pnlCenter.startY = startY;
pnlCenter.rad = endX - startX;
} else if(radRectangle.isSelected()) {
pnlCenter.shape = 1;
if(endY < startY || startX < startY) { return; }
pnlCenter.startX = startX;
pnlCenter.startY = startY;
pnlCenter.width = endX - startX;
pnlCenter.height = endY - startY;
} else if(radLine.isSelected()) {
pnlCenter.shape = 2;
pnlCenter.startX = startX;
pnlCenter.startY = startY;
pnlCenter.endX = endX;
pnlCenter.endY = endY;
}
repaint();
}
}
public class Main {
public static void main
(String[] args
) { new MyFrame().setVisible(true);
}
}
aW1wb3J0IGphdmEuYXd0Lio7CmltcG9ydCBqYXZhLmF3dC5ldmVudC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KCJVbmRvIik7CiAgICAgICAgdGhpcy5idG5VbmRvLnNldEVuYWJsZWQoZmFsc2UpOwogICAgICAgIHRoaXMuYnRuVW5kby5hZGRBY3Rpb25MaXN0ZW5lcih0aGlzKTsKICAgICAgICB0aGlzLmJ0blJlZG8gPSBuZXcgSkJ1dHRvbigiUmVkbyIpOwogICAgICAgIHRoaXMuYnRuUmVkby5zZXRFbmFibGVkKGZhbHNlKTsKICAgICAgICB0aGlzLmJ0blJlZG8uYWRkQWN0aW9uTGlzdGVuZXIodGhpcyk7CiAgICAgICAgcC5hZGQoYnRuVW5kbyk7CiAgICAgICAgcC5hZGQoYnRuUmVkbyk7CiAgICAgICAgdGhpcy5hZGQocCk7CiAgICAgICAgdGhpcy5sb2dzID0gbmV3IEFycmF5TGlzdDxMb2c+KCk7CiAgICB9CgogICAgQE92ZXJyaWRlIHB1YmxpYyB2b2lkIGFjdGlvblBlcmZvcm1lZChBY3Rpb25FdmVudCBlKSB7CiAgICAgICAgSkJ1dHRvbiBzb3VyY2UgPSAoSkJ1dHRvbillLmdldFNvdXJjZSgpOwogICAgICAgIGlmKHNvdXJjZSA9PSBidG5VbmRvKSB7CiAgICAgICAgICAgIHNoYXBlID0gLTI7CiAgICAgICAgICAgIHJlcGFpbnQoKTsKICAgICAgICB9IGVsc2UgaWYoc291cmNlID09IGJ0blJlZG8pIHsKICAgICAgICAgICAgc2hhcGUgPSAtMTsKICAgICAgICAgICAgcmVwYWludCgpOwogICAgICAgIH0KICAgIH0KCiAgICBAT3ZlcnJpZGUgcHVibGljIHZvaWQgcGFpbnQoR3JhcGhpY3MgZykgewogICAgICAgIHN1cGVyLnBhaW50KGcpOwogICAgICAgIGlmKHNoYXBlID09IC0yKSB7CiAgICAgICAgICAgIG5vdyAtPSAxOwogICAgICAgIH0gZWxzZSBpZihzaGFwZSA9PSAtMSkgewogICAgICAgICAgICBub3cgKz0gMTsKICAgICAgICB9CiAgICAgICAgZm9yKGludCBpPTA7IGk8bm93OyBpKyspIHsKICAgICAgICAgICAgTG9nIGxvZyA9IGxvZ3MuZ2V0KGkpOwogICAgICAgICAgICBpZihsb2cuc2hhcGUgPT0gMCkgewogICAgICAgICAgICAgICAgZy5maWxsQXJjKGxvZy5zdGFydFgsIGxvZy5zdGFydFksIGxvZy5yYWQsIGxvZy5yYWQsIDAsIDM2MCk7CiAgICAgICAgICAgIH0gZWxzZSBpZihsb2cuc2hhcGUgPT0gMSkgewogICAgICAgICAgICAgICAgZy5maWxsUmVjdChsb2cuc3RhcnRYLCBsb2cuc3RhcnRZLCBsb2cud2lkdGgsIGxvZy5oZWlnaHQpOwogICAgICAgICAgICB9IGVsc2UgaWYobG9nLnNoYXBlID09IDIpIHsKICAgICAgICAgICAgICAgIGcuZHJhd0xpbmUobG9nLnN0YXJ0WCwgbG9nLnN0YXJ0WSwgbG9nLmVuZFgsIGxvZy5lbmRZKTsKICAgICAgICAgICAgfQogICAgICAgIH0KICAgICAgICBpZihzaGFwZSA9PSAwKSB7CiAgICAgICAgICAgIGcuZmlsbEFyYyhzdGFydFgsIHN0YXJ0WSwgcmFkLCByYWQsIDAsIDM2MCk7CiAgICAgICAgICAgIGlmKG5vdyArIDEgPT0gbG9ncy5zaXplKCkgLSAxKSB7CiAgICAgICAgICAgICAgICBsb2dzLmFkZChuZXcgTG9nKDAsIHN0YXJ0WCwgc3RhcnRZLCAtMSwgLTEsIHJhZCwgLTEsIC0xKSk7CiAgICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgICBmb3IoaW50IGk9bm93OyBpPGxvZ3Muc2l6ZSgpOyApIHsKICAgICAgICAgICAgICAgICAgICBsb2dzLnJlbW92ZShpKTsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIGxvZ3MuYWRkKG5ldyBMb2coMCwgc3RhcnRYLCBzdGFydFksIC0xLCAtMSwgcmFkLCAtMSwgLTEpKTsKICAgICAgICAgICAgfQogICAgICAgICAgICBub3cgPSBsb2dzLnNpemUoKTsKICAgICAgICB9IGVsc2UgaWYoc2hhcGUgPT0gMSkgewogICAgICAgICAgICBnLmZpbGxSZWN0KHN0YXJ0WCwgc3RhcnRZLCB3aWR0aCwgaGVpZ2h0KTsKICAgICAgICAgICAgaWYobm93ICsgMSA9PSBsb2dzLnNpemUoKSAtIDEpIHsKICAgICAgICAgICAgICAgIGxvZ3MuYWRkKG5ldyBMb2coMSwgc3RhcnRYLCBzdGFydFksIC0xLCAtMSwgLTEsIHdpZHRoLCBoZWlnaHQpKTsKICAgICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgICAgIGZvcihpbnQgaT1ub3c7IGk8bG9ncy5zaXplKCk7ICkgewogICAgICAgICAgICAgICAgICAgIGxvZ3MucmVtb3ZlKGkpOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgbG9ncy5hZGQobmV3IExvZygxLCBzdGFydFgsIHN0YXJ0WSwgLTEsIC0xLCAtMSwgd2lkdGgsIGhlaWdodCkpOwogICAgICAgICAgICB9CiAgICAgICAgICAgIG5vdyA9IGxvZ3Muc2l6ZSgpOwogICAgICAgIH0gZWxzZSBpZihzaGFwZSA9PSAyKSB7CiAgICAgICAgICAgIGcuZHJhd0xpbmUoc3RhcnRYLCBzdGFydFksIGVuZFgsIGVuZFkpOwogICAgICAgICAgICBpZihub3cgKyAxID09IGxvZ3Muc2l6ZSgpIC0gMSkgewogICAgICAgICAgICAgICAgbG9ncy5hZGQobmV3IExvZygyLCBzdGFydFgsIHN0YXJ0WSwgZW5kWCwgZW5kWSwgLTEsIC0xLCAtMSkpOwogICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgICAgZm9yKGludCBpPW5vdzsgaTxsb2dzLnNpemUoKTsgKSB7CiAgICAgICAgICAgICAgICAgICAgbG9ncy5yZW1vdmUoaSk7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBsb2dzLmFkZChuZXcgTG9nKDIsIHN0YXJ0WCwgc3RhcnRZLCBlbmRYLCBlbmRZLCAtM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AgICBlbmRYID0gZS5nZXRYKCk7CiAgICAgICAgZW5kWSA9IGUuZ2V0WSgpOwogICAgICAgIGlmKHJhZENpcmNsZS5pc1NlbGVjdGVkKCkpIHsKICAgICAgICAgICAgcG5sQ2VudGVyLnNoYXBlID0gMDsKICAgICAgICAgICAgaWYoZW5kWSA8IHN0YXJ0WSB8fCBzdGFydFggPCBzdGFydFkpIHsgcmV0dXJuOyB9CiAgICAgICAgICAgIHBubENlbnRlci5zdGFydFggPSBzdGFydFg7CiAgICAgICAgICAgIHBubENlbnRlci5zdGFydFkgPSBzdGFydFk7CiAgICAgICAgICAgIHBubENlbnRlci5yYWQgPSBlbmRYIC0gc3RhcnRYOwogICAgICAgIH0gZWxzZSBpZihyYWRSZWN0YW5nbGUuaXNTZWxlY3RlZCgpKSB7CiAgICAgICAgICAgIHBubENlbnRlci5zaGFwZSA9IDE7CiAgICAgICAgICAgIGlmKGVuZFkgPCBzdGFydFkgfHwgc3RhcnRYIDwgc3RhcnRZKSB7IHJldHVybjsgfQogICAgICAgICAgICBwbmxDZW50ZXIuc3RhcnRYID0gc3RhcnRYOwogICAgICAgICAgICBwbmxDZW50ZXIuc3RhcnRZID0gc3RhcnRZOwogICAgICAgICAgICBwbmxDZW50ZXIud2lkdGggPSBlbmRYIC0gc3RhcnRYOwogICAgICAgICAgICBwbmxDZW50ZXIuaGVpZ2h0ID0gZW5kWSAtIHN0YXJ0WTsKICAgICAgICB9IGVsc2UgaWYocmFkTGluZS5pc1NlbGVjdGVkKCkpIHsKICAgICAgICAgICAgcG5sQ2VudGVyLnNoYXBlID0gMjsKICAgICAgICAgICAgcG5sQ2VudGVyLnN0YXJ0WCA9IHN0YXJ0WDsKICAgICAgICAgICAgcG5sQ2VudGVyLnN0YXJ0WSA9IHN0YXJ0WTsKICAgICAgICAgICAgcG5sQ2VudGVyLmVuZFggPSBlbmRYOwogICAgICAgICAgICBwbmxDZW50ZXIuZW5kWSA9IGVuZFk7CiAgICAgICAgfQogICAgICAgIHJlcGFpbnQoKTsKICAgIH0KfQoKcHVibGljIGNsYXNzIE1haW4gewogICAgcHVibGljIHN0YXRpYyB2b2lkIG1haW4oU3RyaW5nW10gYXJncykgewogICAgICAgIG5ldyBNeUZyYW1lKCkuc2V0VmlzaWJsZSh0cnVlKTsKICAgIH0KfQ==