Advertisement
Guest User

Untitled

a guest
Mar 25th, 2017
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.25 KB | None | 0 0
  1. package eg.edu.alexu.cse.oop.draw.project;
  2.  
  3. hatla2e goz2 el plugin f goz2 zorar el install plugin w goz2 zorar el ezternal shape
  4.  
  5. import javax.swing.*;
  6. import java.awt.*;
  7. import java.awt.event.ActionEvent;
  8. import java.io.File;
  9. import java.lang.reflect.Constructor;
  10. import java.lang.reflect.InvocationTargetException;
  11. import java.net.MalformedURLException;
  12. import java.net.URL;
  13. import java.net.URLClassLoader;
  14. import java.util.*;
  15. import java.util.List;
  16.  
  17.  
  18. /**
  19. * Created by Mohamed Abdelrehim on 3/16/2017.
  20. */
  21. public class GUI extends JFrame {
  22. private JPanel root;
  23. private JPanel shapeButtons;
  24. private JButton externalShapeButton;
  25. private JButton redoButton;
  26. private JButton undoButton;
  27. private JPanel shapeActions;
  28. private JPanel actionMemory;
  29. private JPanel actions;
  30. private JPanel canvasArea;
  31. private JComboBox comboBox1;
  32. private JButton deleteButton;
  33. private JButton editButton;
  34. private JButton copyButton;
  35. private JButton moveButton;
  36. private JButton colorButton;
  37. private JButton circleButton;
  38. private JButton ellipseButton;
  39. private JButton lineSegmentButton;
  40. private JButton rectangleButton;
  41. private JButton triangleButton;
  42. private JButton squareButton;
  43. private DrawingEngine drawingEngine;
  44. private JButton installPluginButton;
  45.  
  46. private Shape c;
  47. private Input dialog;
  48. private MoveInput move;
  49. private ArrayList<Class<? extends Shape>> supportedShapes;
  50. private int i;
  51.  
  52. private static GUI gui = new GUI();
  53.  
  54. private GUI() {
  55. drawingEngine.setVisible(true);
  56.  
  57. move = new MoveInput();
  58. i = 0;
  59.  
  60. deleteButton.addActionListener((ActionEvent e) -> {
  61. c = (Shape)comboBox1.getSelectedItem();
  62. drawingEngine.removeShape(c);
  63. comboBox1.removeAllItems();
  64. updateComboBox();
  65.  
  66. });
  67.  
  68. editButton.addActionListener((ActionEvent e) -> {
  69.  
  70. c = (Shape)comboBox1.getSelectedItem();
  71. dialog = new Input(c);
  72. getUserInput();
  73. Shape d = dialog.getShapeVal();
  74. drawingEngine.removeShape(c);
  75. drawingEngine.addShape(d);
  76. comboBox1.removeAllItems();
  77. updateComboBox();
  78. });
  79.  
  80. copyButton.addActionListener((ActionEvent e) -> {
  81. c = (Shape)comboBox1.getSelectedItem();
  82. try {
  83. Shape d = (Shape)c.clone();
  84. d.setFillColor(c.getFillColor());
  85. drawingEngine.addShape(d);
  86. comboBox1.removeAllItems();
  87. updateComboBox();
  88. }
  89. catch (CloneNotSupportedException e1) {
  90. e1.printStackTrace();
  91. }
  92. });
  93.  
  94. moveButton.addActionListener((ActionEvent e) -> {
  95. c = (Shape)comboBox1.getSelectedItem();
  96. getMoveUserInput();
  97. c.setPosition(move.getPoint());
  98. Shape d = c;
  99. drawingEngine.removeShape(c);
  100. drawingEngine.addShape(d);
  101. comboBox1.removeAllItems();
  102. updateComboBox();
  103. });
  104.  
  105. colorButton.addActionListener((ActionEvent e) -> {
  106. c = (Shape)comboBox1.getSelectedItem();
  107. Color selectedColor = JColorChooser.showDialog(null, "Pick Color", c.getFillColor());
  108. if (selectedColor != null) {
  109. c.setFillColor(selectedColor);
  110. Shape temp = c;
  111. drawingEngine.removeShape(c);
  112. drawingEngine.addShape(temp);
  113. drawingEngine.refresh(drawingEngine.getGraphics());
  114. }
  115.  
  116. });
  117.  
  118. circleButton.addActionListener((ActionEvent e) -> {
  119. c = new Circle();
  120. dialog = new Input(c);
  121. getUserInput();
  122. if(dialog.getIsCancel()) {
  123. c = dialog.getShapeVal();
  124. drawingEngine.addShape(c);
  125. comboBox1.removeAllItems();
  126. updateComboBox();
  127. }
  128.  
  129. });
  130.  
  131. ellipseButton.addActionListener((ActionEvent e) -> {
  132. c = new Ellipse();
  133. dialog = new Input(c);
  134. getUserInput();
  135. if(dialog.getIsCancel()) {
  136. c = dialog.getShapeVal();
  137. drawingEngine.addShape(c);
  138. comboBox1.removeAllItems();
  139. updateComboBox();
  140. }
  141.  
  142. });
  143.  
  144. lineSegmentButton.addActionListener((ActionEvent e) -> {
  145.  
  146. c = new LineSegment();
  147. dialog = new Input(c);
  148. getUserInput();
  149. if(dialog.getIsCancel()) {
  150. c = dialog.getShapeVal();
  151. drawingEngine.addShape(c);
  152. comboBox1.removeAllItems();
  153. updateComboBox();
  154. }
  155.  
  156. });
  157.  
  158. rectangleButton.addActionListener((ActionEvent e) -> {
  159.  
  160. c = new Rectangle();
  161. dialog = new Input(c);
  162. getUserInput();
  163. if(dialog.getIsCancel()) {
  164. c = dialog.getShapeVal();
  165. drawingEngine.addShape(c);
  166. comboBox1.removeAllItems();
  167. updateComboBox();
  168. }
  169.  
  170. });
  171.  
  172. triangleButton.addActionListener((ActionEvent e) -> {
  173.  
  174. c = new Triangle();
  175. dialog = new Input(c);
  176. getUserInput();
  177. if(dialog.getIsCancel()) {
  178. c = dialog.getShapeVal();
  179. drawingEngine.addShape(c);
  180. comboBox1.removeAllItems();
  181. updateComboBox();
  182. }
  183.  
  184. });
  185.  
  186. squareButton.addActionListener((ActionEvent e) -> {
  187.  
  188. c = new Square();
  189. dialog = new Input(c);
  190. getUserInput();
  191.  
  192. if(dialog.getIsCancel()) {
  193. c = dialog.getShapeVal();
  194. drawingEngine.addShape(c);
  195. comboBox1.removeAllItems();
  196. updateComboBox();
  197. }
  198.  
  199. });
  200.  
  201. externalShapeButton.addActionListener((ActionEvent e) -> {
  202. ArrayList supportedShapes = new ArrayList<>();
  203. supportedShapes =(ArrayList<Class<? extends Shape>>) drawingEngine.getSupportedShapes();
  204. Class b = (Class)supportedShapes.get(0);
  205. try {
  206. Constructor constructor = b.getConstructor(null);
  207. dialog = new Input(c);
  208. getUserInput();
  209. if(dialog.getIsCancel()) {
  210. c = dialog.getShapeVal();
  211. drawingEngine.addShape(c);
  212. comboBox1.removeAllItems();
  213. updateComboBox();
  214. }
  215. try {
  216. c = (Shape) constructor.newInstance();
  217. } catch (InstantiationException e1) {
  218. e1.printStackTrace();
  219. } catch (IllegalAccessException e1) {
  220. e1.printStackTrace();
  221. } catch (InvocationTargetException e1) {
  222. e1.printStackTrace();
  223. }
  224. } catch (NoSuchMethodException e1) {
  225. e1.printStackTrace();
  226. }
  227. });
  228.  
  229. undoButton.addActionListener((ActionEvent e) -> {
  230. drawingEngine.undo();
  231. drawingEngine.refresh(drawingEngine.getGraphics());
  232. comboBox1.removeAllItems();
  233.  
  234. updateComboBox();
  235. });
  236.  
  237. redoButton.addActionListener((ActionEvent e) -> {
  238. drawingEngine.redo();
  239. drawingEngine.refresh(drawingEngine.getGraphics());
  240. comboBox1.removeAllItems();
  241.  
  242. updateComboBox();
  243. });
  244. installPluginButton.addActionListener((ActionEvent e) -> {
  245. List<URL> urls = new ArrayList<>();
  246. JFileChooser fileChooser = new JFileChooser();
  247. int retVal = fileChooser.showOpenDialog(null);
  248. if (retVal == JFileChooser.APPROVE_OPTION) {
  249. File file = fileChooser.getSelectedFile();
  250. try {
  251. URL url = file.toURI().toURL();
  252. urls.add(url);
  253. URLClassLoader classLoader = new URLClassLoader(urls.toArray(new URL[urls.size()]));
  254. String className = JOptionPane.showInputDialog(null, "Enter proper class name");
  255. try {
  256. Class importedClass = classLoader.loadClass(className);
  257. drawingEngine.installPluginShape(importedClass);
  258. }
  259. catch (ClassNotFoundException e1) {
  260. e1.printStackTrace();
  261. }
  262. }
  263. catch (MalformedURLException e1) {
  264. e1.printStackTrace();
  265. }
  266. }
  267.  
  268. });
  269. }
  270.  
  271. public static GUI getInstance() {
  272. return gui;
  273. }
  274.  
  275. private void updateComboBox() {
  276. Shape[] arr = drawingEngine.getShapes();
  277. for (int i = 0; i < drawingEngine.getNumOfShapes(); i++) {
  278. comboBox1.addItem(arr[i]);
  279. }
  280. }
  281.  
  282. private void getUserInput() {
  283. dialog.pack();
  284. dialog.setVisible(true);
  285. }
  286.  
  287. private void getMoveUserInput() {
  288. move.pack();
  289. move.setVisible(true);
  290. }
  291.  
  292.  
  293. public static void main(String[] args) {
  294. JFrame frame = new JFrame("Shape Drawing App");
  295. frame.setContentPane(new GUI().root);
  296. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  297. frame.pack();
  298. frame.setVisible(true);
  299. }
  300. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement