Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
12 kB
3
Indexable
Never
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.util.ArrayList;

public class MyMenu extends JPanel {
    private String shapeSelected;
    private static ArrayList<Shape> shapeList = new ArrayList<>();//static gia na to vlepei kai h drawing panel
    private String buttonPressed;
    private boolean loadFile = false;
    private boolean deletePressed = false;
    private String colorSelected;
    private Color borderColor = null ;
    private Color fillColor;
    private static boolean isColorSelected = true;

    public MyMenu() {

        JToolBar toolBar = new JToolBar();
        JButton rectangle = new JButton();
        JButton square = new JButton();
        JButton triangle = new JButton();
        JButton oval = new JButton();
        JButton star = new JButton();
        JButton circle = new JButton();
        JButton line = new JButton();
        JButton copy = new JButton();
        JButton move = new JButton();
        JButton delete = new JButton();
        JButton undo = new JButton();
        JButton red =  new JButton();
        JButton green =  new JButton();
        JButton orange =  new JButton();
        JButton yellow =  new JButton();
        JButton turquoise=  new JButton();

        ImageIcon rectangleImage = new ImageIcon("Rectangle.png");
        ImageIcon squareImage = new ImageIcon("Square.png");
        ImageIcon triangleImage = new ImageIcon("Triangle.png");
        ImageIcon ovalImage = new ImageIcon("Oval.png");
        ImageIcon starImage = new ImageIcon("Star.png");
        ImageIcon circleImage = new ImageIcon("Circle.png");
        ImageIcon lineImage = new ImageIcon("Line.png");
        ImageIcon copyImage = new ImageIcon("Copy.png");
        ImageIcon moveImage = new ImageIcon("Move.png");
        ImageIcon deleteImage = new ImageIcon("Delete.png");
        ImageIcon undoImage = new ImageIcon("Undo.png");
        ImageIcon redImage = new ImageIcon("Red.png");
        ImageIcon greenImage = new ImageIcon("Green.png");
        ImageIcon yellowImage = new ImageIcon("Yellow.png");
        ImageIcon turquoiseImage = new ImageIcon("Turquoise.jpg");
        ImageIcon orangeImage = new ImageIcon("Orange.png");


        //Rescaling all the images
        Image rectScaled = rectangleImage.getImage().getScaledInstance(20, 20, Image.SCALE_SMOOTH);
        rectangle.setIcon(new ImageIcon(rectScaled));
        rectangle.setToolTipText("Rectangle");

        Image sqrScaled = squareImage.getImage().getScaledInstance(20, 20, Image.SCALE_SMOOTH);
        square.setIcon(new ImageIcon(sqrScaled));
        square.setToolTipText("Square");

        Image triScaled = triangleImage.getImage().getScaledInstance(20, 20, Image.SCALE_SMOOTH);
        triangle.setIcon(new ImageIcon(triScaled));
        triangle.setToolTipText("Triangle");

        Image strScaled = starImage.getImage().getScaledInstance(20, 20, Image.SCALE_SMOOTH);
        star.setIcon(new ImageIcon(strScaled));
        star.setToolTipText("Star");

        Image crcScaled = circleImage.getImage().getScaledInstance(20, 20, Image.SCALE_SMOOTH);
        circle.setIcon(new ImageIcon(crcScaled));
        circle.setToolTipText("Circle");

        Image ovlScaled = ovalImage.getImage().getScaledInstance(20, 20, Image.SCALE_SMOOTH);
        oval.setIcon(new ImageIcon(ovlScaled));
        oval.setToolTipText("Oval");

        Image lnScaled = lineImage.getImage().getScaledInstance(20, 20, Image.SCALE_SMOOTH);
        line.setIcon(new ImageIcon(lnScaled));
        line.setToolTipText("Line");

        Image cpScaled = copyImage.getImage().getScaledInstance(20, 20, Image.SCALE_SMOOTH);
        copy.setIcon(new ImageIcon(cpScaled));
        copy.setText("Copy");

        Image redScaled = redImage.getImage().getScaledInstance(20, 20, Image.SCALE_SMOOTH);
        red.setIcon(new ImageIcon(redScaled));
        red.setToolTipText("Red");

        Image greenScaled = greenImage.getImage().getScaledInstance(20, 20, Image.SCALE_SMOOTH);
        green.setIcon(new ImageIcon(greenScaled));
        green.setToolTipText("Green");

        Image yellowScaled = yellowImage.getImage().getScaledInstance(20, 20, Image.SCALE_SMOOTH);
        yellow.setIcon(new ImageIcon(yellowScaled));
        yellow.setToolTipText("Yellow");

        Image turquoiseScaled = turquoiseImage.getImage().getScaledInstance(20, 20, Image.SCALE_SMOOTH);
        turquoise.setIcon(new ImageIcon(turquoiseScaled));
        turquoise.setToolTipText("Turquoise");

        Image orangeScaled = orangeImage.getImage().getScaledInstance(20, 20, Image.SCALE_SMOOTH);
        orange.setIcon(new ImageIcon(orangeScaled));
        orange.setToolTipText("Orange");

        Image mvScaled = moveImage.getImage().getScaledInstance(20, 20, Image.SCALE_SMOOTH);
        move.setIcon(new ImageIcon(mvScaled));
        move.setText("Move");

        Image delScaled = deleteImage.getImage().getScaledInstance(20, 20, Image.SCALE_SMOOTH);
        delete.setIcon(new ImageIcon(delScaled));
        delete.setText("Delete");
        delete.setToolTipText("Select the shape and press Delete key");

        Image undScaled = undoImage.getImage().getScaledInstance(20, 20, Image.SCALE_SMOOTH);
        undo.setIcon(new ImageIcon(undScaled));
        undo.setText("Undo");
        undo.setToolTipText("Ctrl+Z to restore your last action");

        JMenu file = new JMenu("File");
        JMenuBar menuBar = new JMenuBar();
        toolBar.setFloatable(false);
        toolBar.add(line);
        toolBar.add(oval);
        toolBar.add(rectangle);
        toolBar.add(triangle);
        toolBar.add(square);
        toolBar.add(star);
        toolBar.add(circle);
        toolBar.add(red);
        toolBar.add(orange);
        toolBar.add(turquoise);
        toolBar.add(yellow);
        toolBar.add(green);
        toolBar.add(turquoise);

        //Making buttons transparent
        copy.setOpaque(false);
        copy.setBorderPainted(false);

        move.setOpaque(false);
        move.setBorderPainted(false);

        delete.setOpaque(false);
        delete.setBorderPainted(false);

        undo.setOpaque(false);
        undo.setBorderPainted(false);

        red.setOpaque(false);
        red.setBorderPainted(false);
        //red.setContentAreaFilled(false);

        green.setOpaque(false);
        green.setBorderPainted(false);

        yellow.setOpaque(false);
        yellow.setBorderPainted(false);

        turquoise.setOpaque(false);
        turquoise.setBorderPainted(false);

        orange.setOpaque(false);
        orange.setBorderPainted(false);

        toolBar.add(copy);
        toolBar.add(move);
        toolBar.add(delete);
        toolBar.add(undo);

        JMenuItem load = new JMenuItem("Load");
        JMenuItem save = new JMenuItem("Save");
        JMenuItem exit = new JMenuItem("Exit");

        ImageIcon loadImage = new ImageIcon("Load.png");
        Image ldScaled = loadImage.getImage().getScaledInstance(20, 20, Image.SCALE_SMOOTH);
        load.setIcon(new ImageIcon(ldScaled));

        ImageIcon saveImage = new ImageIcon("Save.png");
        Image svScaled = saveImage.getImage().getScaledInstance(20, 20, Image.SCALE_SMOOTH);
        save.setIcon(new ImageIcon(svScaled));

        ImageIcon exitImage = new ImageIcon("Exit.png");
        Image exScaled = exitImage.getImage().getScaledInstance(20, 20, Image.SCALE_SMOOTH);
        exit.setIcon(new ImageIcon(exScaled));

        file.add(load);
        file.add(save);
        file.add(exit);
        menuBar.add(file);

        //Adding action listeners for each button
        line.addActionListener(new ButtonClicked());
        square.addActionListener(new ButtonClicked());
        rectangle.addActionListener(new ButtonClicked());
        triangle.addActionListener(new ButtonClicked());
        oval.addActionListener(new ButtonClicked());
        circle.addActionListener(new ButtonClicked());
        star.addActionListener(new ButtonClicked());
        save.addActionListener(new ButtonClicked());
        load.addActionListener(new ButtonClicked());
        copy.addActionListener(new ButtonClicked());
        move.addActionListener(new ButtonClicked());
        delete.addActionListener(new ButtonClicked());
        undo.addActionListener(new ButtonClicked());
        red.addActionListener(new ButtonClicked());
        orange.addActionListener(new ButtonClicked());
        turquoise.addActionListener(new ButtonClicked());
        green.addActionListener(new ButtonClicked());
        yellow.addActionListener(new ButtonClicked());

        //save.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, ActionEvent.CTRL_MASK));
        setLayout(new BorderLayout());
        add(menuBar,BorderLayout.PAGE_START);
        add(toolBar);
    }

    public static ArrayList<Shape> getShapeList() {
        return shapeList;
    }
    public String getButtonPressed() { return buttonPressed; }
    public boolean isDeletePressed() { return deletePressed; }
    public String getShapeSelected() {
        return shapeSelected;
    }
    public  boolean isLoadFile() { return loadFile; }
    public Color getBorderColor() { return borderColor; }
    public static boolean isColorSelected() {return isColorSelected;}

    public void setDeletePressed(boolean deletePressed) {
        this.deletePressed = deletePressed;
    }

    public void save(ArrayList<Shape> shapeList) {
        System.out.println("Saving:");
        ObjectOutputStream out;
        try {
            out =  new ObjectOutputStream(new FileOutputStream("Shapes.txt"));

            for (Shape s : shapeList) {
                out.writeObject(s);
            }
            System.out.println(colorSelected);
            out.flush();
            out.close();

        } catch (FileNotFoundException fnf) {
            System.out.println("File not found");
            fnf.printStackTrace();
        } catch(IOException ioe) {
            System.out.println("Error with I/O processes") ;
            ioe.printStackTrace();
        }
    }

        class ButtonClicked implements ActionListener {
        //Getting the value from each JButton
        @Override
        public void actionPerformed(ActionEvent e) {
            Object o = e.getSource();
            if(o instanceof JButton) {
                colorSelected = ((JButton) o).getToolTipText(); {//toDO: if color selected is shape
                    switch(colorSelected) {
                        case"Red":
                            borderColor = Color.RED;
                            break;
                        case"Green":
                            borderColor = Color.GREEN;
                            break;
                        case"Yellow":
                            borderColor = Color.YELLOW;
                            break;
                        case"Orange":
                            borderColor = Color.ORANGE;
                            break;
                        case"Turquoise":
                            borderColor = Color.CYAN;
                            break;
                        default:
                            //borderColor = null;
                            System.out.println();
                    }
                }
                shapeSelected = ((JButton) o).getToolTipText();
//                    if(shapeSelected.equals("Red"))
//                        shapeSelected = null;//toDO:fix if shape selected is color
                buttonPressed = ((JButton) o).getText();
                    if(buttonPressed.equals("Delete"))
                        deletePressed = true;
            }
            if(o instanceof JMenuItem) {
                buttonPressed = ((JMenuItem) o).getText();
                if(buttonPressed.equals("Save"))
                    save(MyMenu.shapeList);
                if(buttonPressed.equals("Load"))
                    loadFile = true;
            }
        }
    }
}