Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package eg.edu.alexu.cse.oop.draw.project;
- hatla2e goz2 el plugin f goz2 zorar el install plugin w goz2 zorar el ezternal shape
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.ActionEvent;
- import java.io.File;
- import java.lang.reflect.Constructor;
- import java.lang.reflect.InvocationTargetException;
- import java.net.MalformedURLException;
- import java.net.URL;
- import java.net.URLClassLoader;
- import java.util.*;
- import java.util.List;
- /**
- * Created by Mohamed Abdelrehim on 3/16/2017.
- */
- public class GUI extends JFrame {
- private JPanel root;
- private JPanel shapeButtons;
- private JButton externalShapeButton;
- private JButton redoButton;
- private JButton undoButton;
- private JPanel shapeActions;
- private JPanel actionMemory;
- private JPanel actions;
- private JPanel canvasArea;
- private JComboBox comboBox1;
- private JButton deleteButton;
- private JButton editButton;
- private JButton copyButton;
- private JButton moveButton;
- private JButton colorButton;
- private JButton circleButton;
- private JButton ellipseButton;
- private JButton lineSegmentButton;
- private JButton rectangleButton;
- private JButton triangleButton;
- private JButton squareButton;
- private DrawingEngine drawingEngine;
- private JButton installPluginButton;
- private Shape c;
- private Input dialog;
- private MoveInput move;
- private ArrayList<Class<? extends Shape>> supportedShapes;
- private int i;
- private static GUI gui = new GUI();
- private GUI() {
- drawingEngine.setVisible(true);
- move = new MoveInput();
- i = 0;
- deleteButton.addActionListener((ActionEvent e) -> {
- c = (Shape)comboBox1.getSelectedItem();
- drawingEngine.removeShape(c);
- comboBox1.removeAllItems();
- updateComboBox();
- });
- editButton.addActionListener((ActionEvent e) -> {
- c = (Shape)comboBox1.getSelectedItem();
- dialog = new Input(c);
- getUserInput();
- Shape d = dialog.getShapeVal();
- drawingEngine.removeShape(c);
- drawingEngine.addShape(d);
- comboBox1.removeAllItems();
- updateComboBox();
- });
- copyButton.addActionListener((ActionEvent e) -> {
- c = (Shape)comboBox1.getSelectedItem();
- try {
- Shape d = (Shape)c.clone();
- d.setFillColor(c.getFillColor());
- drawingEngine.addShape(d);
- comboBox1.removeAllItems();
- updateComboBox();
- }
- catch (CloneNotSupportedException e1) {
- e1.printStackTrace();
- }
- });
- moveButton.addActionListener((ActionEvent e) -> {
- c = (Shape)comboBox1.getSelectedItem();
- getMoveUserInput();
- c.setPosition(move.getPoint());
- Shape d = c;
- drawingEngine.removeShape(c);
- drawingEngine.addShape(d);
- comboBox1.removeAllItems();
- updateComboBox();
- });
- colorButton.addActionListener((ActionEvent e) -> {
- c = (Shape)comboBox1.getSelectedItem();
- Color selectedColor = JColorChooser.showDialog(null, "Pick Color", c.getFillColor());
- if (selectedColor != null) {
- c.setFillColor(selectedColor);
- Shape temp = c;
- drawingEngine.removeShape(c);
- drawingEngine.addShape(temp);
- drawingEngine.refresh(drawingEngine.getGraphics());
- }
- });
- circleButton.addActionListener((ActionEvent e) -> {
- c = new Circle();
- dialog = new Input(c);
- getUserInput();
- if(dialog.getIsCancel()) {
- c = dialog.getShapeVal();
- drawingEngine.addShape(c);
- comboBox1.removeAllItems();
- updateComboBox();
- }
- });
- ellipseButton.addActionListener((ActionEvent e) -> {
- c = new Ellipse();
- dialog = new Input(c);
- getUserInput();
- if(dialog.getIsCancel()) {
- c = dialog.getShapeVal();
- drawingEngine.addShape(c);
- comboBox1.removeAllItems();
- updateComboBox();
- }
- });
- lineSegmentButton.addActionListener((ActionEvent e) -> {
- c = new LineSegment();
- dialog = new Input(c);
- getUserInput();
- if(dialog.getIsCancel()) {
- c = dialog.getShapeVal();
- drawingEngine.addShape(c);
- comboBox1.removeAllItems();
- updateComboBox();
- }
- });
- rectangleButton.addActionListener((ActionEvent e) -> {
- c = new Rectangle();
- dialog = new Input(c);
- getUserInput();
- if(dialog.getIsCancel()) {
- c = dialog.getShapeVal();
- drawingEngine.addShape(c);
- comboBox1.removeAllItems();
- updateComboBox();
- }
- });
- triangleButton.addActionListener((ActionEvent e) -> {
- c = new Triangle();
- dialog = new Input(c);
- getUserInput();
- if(dialog.getIsCancel()) {
- c = dialog.getShapeVal();
- drawingEngine.addShape(c);
- comboBox1.removeAllItems();
- updateComboBox();
- }
- });
- squareButton.addActionListener((ActionEvent e) -> {
- c = new Square();
- dialog = new Input(c);
- getUserInput();
- if(dialog.getIsCancel()) {
- c = dialog.getShapeVal();
- drawingEngine.addShape(c);
- comboBox1.removeAllItems();
- updateComboBox();
- }
- });
- externalShapeButton.addActionListener((ActionEvent e) -> {
- ArrayList supportedShapes = new ArrayList<>();
- supportedShapes =(ArrayList<Class<? extends Shape>>) drawingEngine.getSupportedShapes();
- Class b = (Class)supportedShapes.get(0);
- try {
- Constructor constructor = b.getConstructor(null);
- dialog = new Input(c);
- getUserInput();
- if(dialog.getIsCancel()) {
- c = dialog.getShapeVal();
- drawingEngine.addShape(c);
- comboBox1.removeAllItems();
- updateComboBox();
- }
- try {
- c = (Shape) constructor.newInstance();
- } catch (InstantiationException e1) {
- e1.printStackTrace();
- } catch (IllegalAccessException e1) {
- e1.printStackTrace();
- } catch (InvocationTargetException e1) {
- e1.printStackTrace();
- }
- } catch (NoSuchMethodException e1) {
- e1.printStackTrace();
- }
- });
- undoButton.addActionListener((ActionEvent e) -> {
- drawingEngine.undo();
- drawingEngine.refresh(drawingEngine.getGraphics());
- comboBox1.removeAllItems();
- updateComboBox();
- });
- redoButton.addActionListener((ActionEvent e) -> {
- drawingEngine.redo();
- drawingEngine.refresh(drawingEngine.getGraphics());
- comboBox1.removeAllItems();
- updateComboBox();
- });
- installPluginButton.addActionListener((ActionEvent e) -> {
- List<URL> urls = new ArrayList<>();
- JFileChooser fileChooser = new JFileChooser();
- int retVal = fileChooser.showOpenDialog(null);
- if (retVal == JFileChooser.APPROVE_OPTION) {
- File file = fileChooser.getSelectedFile();
- try {
- URL url = file.toURI().toURL();
- urls.add(url);
- URLClassLoader classLoader = new URLClassLoader(urls.toArray(new URL[urls.size()]));
- String className = JOptionPane.showInputDialog(null, "Enter proper class name");
- try {
- Class importedClass = classLoader.loadClass(className);
- drawingEngine.installPluginShape(importedClass);
- }
- catch (ClassNotFoundException e1) {
- e1.printStackTrace();
- }
- }
- catch (MalformedURLException e1) {
- e1.printStackTrace();
- }
- }
- });
- }
- public static GUI getInstance() {
- return gui;
- }
- private void updateComboBox() {
- Shape[] arr = drawingEngine.getShapes();
- for (int i = 0; i < drawingEngine.getNumOfShapes(); i++) {
- comboBox1.addItem(arr[i]);
- }
- }
- private void getUserInput() {
- dialog.pack();
- dialog.setVisible(true);
- }
- private void getMoveUserInput() {
- move.pack();
- move.setVisible(true);
- }
- public static void main(String[] args) {
- JFrame frame = new JFrame("Shape Drawing App");
- frame.setContentPane(new GUI().root);
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.pack();
- frame.setVisible(true);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement