fork download
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import java.util.*;
  4. import javax.swing.*;
  5. import static java.lang.Math.*;
  6.  
  7. class Log {
  8. int shape = -1;
  9. int startX = -1, startY = -1, endX = -1, endY = -1, rad = -1, width = -1, height = -1;
  10. public Log(int shape, int startX, int startY, int endX, int endY, int rad, int width, int height) {
  11. this.shape = shape;
  12. this.startX = startX;
  13. this.startY = startY;
  14. this.endX = endX;
  15. this.endY = endY;
  16. this.rad = rad;
  17. this.width = width;
  18. this.height = height;
  19. }
  20. }
  21.  
  22. class MyPanel extends JPanel implements ActionListener {
  23. int shape = -3;
  24. int startX, startY, endX, endY, rad, width, height;
  25. JButton btnUndo, btnRedo;
  26. ArrayList<Log> logs;
  27. int now = 0;
  28.  
  29. public MyPanel() {
  30. JPanel p = new JPanel();
  31. this.btnUndo = new JButton("Undo");
  32. this.btnUndo.setEnabled(false);
  33. this.btnUndo.addActionListener(this);
  34. this.btnRedo = new JButton("Redo");
  35. this.btnRedo.setEnabled(false);
  36. this.btnRedo.addActionListener(this);
  37. p.add(btnUndo);
  38. p.add(btnRedo);
  39. this.add(p);
  40. this.logs = new ArrayList<Log>();
  41. }
  42.  
  43. @Override public void actionPerformed(ActionEvent e) {
  44. JButton source = (JButton)e.getSource();
  45. if(source == btnUndo) {
  46. shape = -2;
  47. repaint();
  48. } else if(source == btnRedo) {
  49. shape = -1;
  50. repaint();
  51. }
  52. }
  53.  
  54. @Override public void paint(Graphics g) {
  55. super.paint(g);
  56. if(shape == -2) {
  57. now -= 1;
  58. } else if(shape == -1) {
  59. now += 1;
  60. }
  61. for(int i=0; i<now; i++) {
  62. Log log = logs.get(i);
  63. if(log.shape == 0) {
  64. g.fillArc(log.startX, log.startY, log.rad, log.rad, 0, 360);
  65. } else if(log.shape == 1) {
  66. g.fillRect(log.startX, log.startY, log.width, log.height);
  67. } else if(log.shape == 2) {
  68. g.drawLine(log.startX, log.startY, log.endX, log.endY);
  69. }
  70. }
  71. if(shape == 0) {
  72. g.fillArc(startX, startY, rad, rad, 0, 360);
  73. if(now + 1 == logs.size() - 1) {
  74. logs.add(new Log(0, startX, startY, -1, -1, rad, -1, -1));
  75. } else {
  76. for(int i=now; i<logs.size(); ) {
  77. logs.remove(i);
  78. }
  79. logs.add(new Log(0, startX, startY, -1, -1, rad, -1, -1));
  80. }
  81. now = logs.size();
  82. } else if(shape == 1) {
  83. g.fillRect(startX, startY, width, height);
  84. if(now + 1 == logs.size() - 1) {
  85. logs.add(new Log(1, startX, startY, -1, -1, -1, width, height));
  86. } else {
  87. for(int i=now; i<logs.size(); ) {
  88. logs.remove(i);
  89. }
  90. logs.add(new Log(1, startX, startY, -1, -1, -1, width, height));
  91. }
  92. now = logs.size();
  93. } else if(shape == 2) {
  94. g.drawLine(startX, startY, endX, endY);
  95. if(now + 1 == logs.size() - 1) {
  96. logs.add(new Log(2, startX, startY, endX, endY, -1, -1, -1));
  97. } else {
  98. for(int i=now; i<logs.size(); ) {
  99. logs.remove(i);
  100. }
  101. logs.add(new Log(2, startX, startY, endX, endY, -1, -1, -1));
  102. }
  103. now = logs.size();
  104. }
  105. if(now > 0) { this.btnUndo.setEnabled(true); }
  106. else { this.btnUndo.setEnabled(false); }
  107. if(now == logs.size()) { this.btnRedo.setEnabled(false); }
  108. else { this.btnRedo.setEnabled(true); }
  109. }
  110. }
  111.  
  112. class MyFrame extends JFrame implements MouseListener {
  113. JPanel pnlNorth;
  114. MyPanel pnlCenter;
  115. JRadioButton radCircle, radRectangle, radLine;
  116. int startX, startY, endX, endY;
  117. public MyFrame() {
  118. this.setLayout(new BorderLayout());
  119. this.setSize(800, 600);
  120. this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  121. this.pnlCenter = new MyPanel();
  122. this.pnlCenter.addMouseListener(this);
  123. ButtonGroup group = new ButtonGroup();
  124. this.radCircle = new JRadioButton("Circle");
  125. this.radCircle.setSelected(true);
  126. this.radRectangle = new JRadioButton("Rectangle");
  127. this.radLine = new JRadioButton("Line");
  128. group.add(radCircle);
  129. group.add(radRectangle);
  130. group.add(radLine);
  131. pnlNorth = new JPanel();
  132. pnlNorth.add(radCircle);
  133. pnlNorth.add(radRectangle);
  134. pnlNorth.add(radLine);
  135. this.add(pnlNorth, BorderLayout.NORTH);
  136. this.add(pnlCenter, BorderLayout.CENTER);
  137. }
  138.  
  139. @Override public void mouseClicked(MouseEvent e) {}
  140. @Override public void mouseExited(MouseEvent e) {}
  141. @Override public void mouseEntered(MouseEvent e) {}
  142. @Override public void mousePressed(MouseEvent e) {
  143. startX = e.getX();
  144. startY = e.getY();
  145. }
  146. @Override public void mouseReleased(MouseEvent e) {
  147. endX = e.getX();
  148. endY = e.getY();
  149. if(radCircle.isSelected()) {
  150. pnlCenter.shape = 0;
  151. if(endY < startY || startX < startY) { return; }
  152. pnlCenter.startX = startX;
  153. pnlCenter.startY = startY;
  154. pnlCenter.rad = endX - startX;
  155. } else if(radRectangle.isSelected()) {
  156. pnlCenter.shape = 1;
  157. if(endY < startY || startX < startY) { return; }
  158. pnlCenter.startX = startX;
  159. pnlCenter.startY = startY;
  160. pnlCenter.width = endX - startX;
  161. pnlCenter.height = endY - startY;
  162. } else if(radLine.isSelected()) {
  163. pnlCenter.shape = 2;
  164. pnlCenter.startX = startX;
  165. pnlCenter.startY = startY;
  166. pnlCenter.endX = endX;
  167. pnlCenter.endY = endY;
  168. }
  169. repaint();
  170. }
  171. }
  172.  
  173. public class Main {
  174. public static void main(String[] args) {
  175. new MyFrame().setVisible(true);
  176. }
  177. }
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty