Advertisement
Guest User

Untitled

a guest
Mar 30th, 2017
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 40.58 KB | None | 0 0
  1. package Program;
  2.  
  3. import java.awt.BasicStroke;
  4. import java.awt.Color;
  5. import java.awt.Graphics2D;
  6. import java.awt.Rectangle;
  7. import java.awt.event.KeyEvent;
  8. import java.io.FileInputStream;
  9. import java.io.FileNotFoundException;
  10. import java.io.FileOutputStream;
  11. import java.io.IOException;
  12. import java.io.ObjectInputStream;
  13. import java.io.ObjectOutputStream;
  14. import java.util.ArrayList;
  15. import java.util.Timer;
  16. import java.util.logging.Level;
  17. import java.util.logging.Logger;
  18. import javax.swing.JColorChooser;
  19. import javax.swing.JFileChooser;
  20. import javax.swing.JOptionPane;
  21. import javax.swing.filechooser.FileNameExtensionFilter;
  22.  
  23.  
  24. public class GUI extends javax.swing.JFrame {
  25.  
  26.     public GUI() {
  27.         initComponents();
  28.         GlobalVars.jPanelObjectColor = jPanelObjectColor;
  29.         GlobalVars.jSliderRotation = jSliderRotation;
  30.         GlobalVars.jListFigures = jList1;
  31.     }
  32.  
  33.     /** This method is called from within the constructor to
  34.      * initialize the form.
  35.      * WARNING: Do NOT modify this code. The content of this method is
  36.      * always regenerated by the Form Editor.
  37.      */
  38.     @SuppressWarnings("unchecked")
  39.     // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
  40.     private void initComponents() {
  41.  
  42.         buttonGroup1 = new javax.swing.ButtonGroup();
  43.         jPanel1 = new javax.swing.JPanel();
  44.         jToggleButtonSelect = new javax.swing.JToggleButton();
  45.         jToggleButtonRectangle = new javax.swing.JToggleButton();
  46.         jToggleButtonEllipse = new javax.swing.JToggleButton();
  47.         jToggleButtonMove = new javax.swing.JToggleButton();
  48.         jLabel1 = new javax.swing.JLabel();
  49.         jLabel2 = new javax.swing.JLabel();
  50.         jPanelObjectColor = new javax.swing.JPanel();
  51.         jLabel3 = new javax.swing.JLabel();
  52.         jSliderRotation = new javax.swing.JSlider();
  53.         jButton1 = new javax.swing.JButton();
  54.         jButton2 = new javax.swing.JButton();
  55.         jButton4 = new javax.swing.JButton();
  56.         jScrollPane1 = new javax.swing.JScrollPane();
  57.         jList1 = new javax.swing.JList();
  58.         jToggleButtonPentagram = new javax.swing.JToggleButton();
  59.         jPanelCurrentColor = new javax.swing.JPanel();
  60.         jToggleButton1 = new javax.swing.JToggleButton();
  61.         jPanelCanvas = new javax.swing.JPanel();
  62.         jMenuBar1 = new javax.swing.JMenuBar();
  63.         jMenu1 = new javax.swing.JMenu();
  64.         jMenuItem3 = new javax.swing.JMenuItem();
  65.         jMenuItem4 = new javax.swing.JMenuItem();
  66.         jMenuItem1 = new javax.swing.JMenuItem();
  67.         jMenu2 = new javax.swing.JMenu();
  68.         jMenuItem2 = new javax.swing.JMenuItem();
  69.  
  70.         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  71.         setTitle("Trayangle - trekto.info");
  72.         setBackground(java.awt.Color.white);
  73.         addWindowListener(new java.awt.event.WindowAdapter() {
  74.             public void windowActivated(java.awt.event.WindowEvent evt) {
  75.                 formWindowActivated(evt);
  76.             }
  77.         });
  78.         addFocusListener(new java.awt.event.FocusAdapter() {
  79.             public void focusGained(java.awt.event.FocusEvent evt) {
  80.                 formFocusGained(evt);
  81.             }
  82.         });
  83.  
  84.         buttonGroup1.add(jToggleButtonSelect);
  85.         jToggleButtonSelect.setText("Избиране");
  86.         jToggleButtonSelect.addActionListener(new java.awt.event.ActionListener() {
  87.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  88.                 jToggleButtonSelectActionPerformed(evt);
  89.             }
  90.         });
  91.  
  92.         buttonGroup1.add(jToggleButtonRectangle);
  93.         jToggleButtonRectangle.setSelected(true);
  94.         jToggleButtonRectangle.setText("Правоъгълник");
  95.         jToggleButtonRectangle.addActionListener(new java.awt.event.ActionListener() {
  96.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  97.                 jToggleButtonRectangleActionPerformed(evt);
  98.             }
  99.         });
  100.  
  101.         buttonGroup1.add(jToggleButtonEllipse);
  102.         jToggleButtonEllipse.setText("Елипса");
  103.  
  104.         buttonGroup1.add(jToggleButtonMove);
  105.         jToggleButtonMove.setText("Преместване");
  106.  
  107.         jLabel1.setText("Текущ цвят");
  108.  
  109.         jLabel2.setText("Избран обект");
  110.  
  111.         jPanelObjectColor.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
  112.         jPanelObjectColor.setPreferredSize(new java.awt.Dimension(25, 25));
  113.         jPanelObjectColor.addMouseListener(new java.awt.event.MouseAdapter() {
  114.             public void mousePressed(java.awt.event.MouseEvent evt) {
  115.                 jPanelObjectColorMousePressed(evt);
  116.             }
  117.         });
  118.  
  119.         javax.swing.GroupLayout jPanelObjectColorLayout = new javax.swing.GroupLayout(jPanelObjectColor);
  120.         jPanelObjectColor.setLayout(jPanelObjectColorLayout);
  121.         jPanelObjectColorLayout.setHorizontalGroup(
  122.             jPanelObjectColorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  123.             .addGap(0, 23, Short.MAX_VALUE)
  124.         );
  125.         jPanelObjectColorLayout.setVerticalGroup(
  126.             jPanelObjectColorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  127.             .addGap(0, 23, Short.MAX_VALUE)
  128.         );
  129.  
  130.         jLabel3.setText("Завъртане");
  131.  
  132.         jSliderRotation.setMaximum(90);
  133.         jSliderRotation.setMinimum(-90);
  134.         jSliderRotation.setValue(0);
  135.         jSliderRotation.addMouseListener(new java.awt.event.MouseAdapter() {
  136.             public void mousePressed(java.awt.event.MouseEvent evt) {
  137.                 jSliderRotationMousePressed(evt);
  138.             }
  139.             public void mouseReleased(java.awt.event.MouseEvent evt) {
  140.                 jSliderRotationMouseReleased(evt);
  141.             }
  142.         });
  143.         jSliderRotation.addChangeListener(new javax.swing.event.ChangeListener() {
  144.             public void stateChanged(javax.swing.event.ChangeEvent evt) {
  145.                 jSliderRotationStateChanged(evt);
  146.             }
  147.         });
  148.  
  149.         jButton1.setText("Напред");
  150.         jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
  151.             public void mouseReleased(java.awt.event.MouseEvent evt) {
  152.                 jButton1MouseReleased(evt);
  153.             }
  154.         });
  155.         jButton1.addActionListener(new java.awt.event.ActionListener() {
  156.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  157.                 jButton1ActionPerformed(evt);
  158.             }
  159.         });
  160.  
  161.         jButton2.setText("Назад");
  162.         jButton2.addMouseListener(new java.awt.event.MouseAdapter() {
  163.             public void mouseReleased(java.awt.event.MouseEvent evt) {
  164.                 jButton2MouseReleased(evt);
  165.             }
  166.         });
  167.         jButton2.addActionListener(new java.awt.event.ActionListener() {
  168.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  169.                 jButton2ActionPerformed(evt);
  170.             }
  171.         });
  172.  
  173.         jButton4.setText("Премахване");
  174.         jButton4.addMouseListener(new java.awt.event.MouseAdapter() {
  175.             public void mouseReleased(java.awt.event.MouseEvent evt) {
  176.                 jButton4MouseReleased(evt);
  177.             }
  178.         });
  179.         jButton4.addActionListener(new java.awt.event.ActionListener() {
  180.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  181.                 jButton4ActionPerformed(evt);
  182.             }
  183.         });
  184.  
  185.         jList1.setModel(new javax.swing.AbstractListModel() {
  186.             String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
  187.             public int getSize() { return strings.length; }
  188.             public Object getElementAt(int i) { return strings[i]; }
  189.         });
  190.         jList1.addMouseListener(new java.awt.event.MouseAdapter() {
  191.             public void mouseClicked(java.awt.event.MouseEvent evt) {
  192.                 jList1MouseClicked(evt);
  193.             }
  194.             public void mouseReleased(java.awt.event.MouseEvent evt) {
  195.                 jList1MouseReleased(evt);
  196.             }
  197.         });
  198.         jScrollPane1.setViewportView(jList1);
  199.  
  200.         buttonGroup1.add(jToggleButtonPentagram);
  201.         jToggleButtonPentagram.setText("Петолъчка");
  202.  
  203.         jPanelCurrentColor.setBackground(new java.awt.Color(79, 220, 48));
  204.         jPanelCurrentColor.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
  205.         jPanelCurrentColor.setPreferredSize(new java.awt.Dimension(25, 25));
  206.         jPanelCurrentColor.addMouseListener(new java.awt.event.MouseAdapter() {
  207.             public void mouseClicked(java.awt.event.MouseEvent evt) {
  208.                 jPanelCurrentColorMouseClicked(evt);
  209.             }
  210.         });
  211.  
  212.         javax.swing.GroupLayout jPanelCurrentColorLayout = new javax.swing.GroupLayout(jPanelCurrentColor);
  213.         jPanelCurrentColor.setLayout(jPanelCurrentColorLayout);
  214.         jPanelCurrentColorLayout.setHorizontalGroup(
  215.             jPanelCurrentColorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  216.             .addGap(0, 23, Short.MAX_VALUE)
  217.         );
  218.         jPanelCurrentColorLayout.setVerticalGroup(
  219.             jPanelCurrentColorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  220.             .addGap(0, 23, Short.MAX_VALUE)
  221.         );
  222.  
  223.         jToggleButton1.setText("NEW");
  224.         jToggleButton1.addActionListener(new java.awt.event.ActionListener() {
  225.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  226.                 jToggleButton1ActionPerformed(evt);
  227.             }
  228.         });
  229.  
  230.         javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
  231.         jPanel1.setLayout(jPanel1Layout);
  232.         jPanel1Layout.setHorizontalGroup(
  233.             jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  234.             .addGroup(jPanel1Layout.createSequentialGroup()
  235.                 .addContainerGap()
  236.                 .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  237.                     .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
  238.                         .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  239.                             .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
  240.                                 .addComponent(jButton1)
  241.                                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  242.                                 .addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  243.                             .addComponent(jButton4, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  244.                             .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
  245.                                 .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  246.                                     .addComponent(jLabel3)
  247.                                     .addComponent(jLabel2))
  248.                                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  249.                                 .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  250.                                     .addComponent(jPanelCurrentColor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  251.                                     .addComponent(jPanelObjectColor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  252.                                 .addGap(14, 14, 14)))
  253.                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  254.                         .addComponent(jLabel1))
  255.                     .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
  256.                         .addComponent(jToggleButtonSelect, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  257.                         .addComponent(jToggleButtonMove, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  258.                         .addComponent(jToggleButtonRectangle, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 149, Short.MAX_VALUE)
  259.                         .addComponent(jToggleButtonEllipse, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  260.                         .addComponent(jToggleButtonPentagram, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  261.                         .addComponent(jToggleButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  262.                     .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
  263.                         .addComponent(jSliderRotation, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 199, Short.MAX_VALUE)
  264.                         .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING))))
  265.         );
  266.         jPanel1Layout.setVerticalGroup(
  267.             jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  268.             .addGroup(jPanel1Layout.createSequentialGroup()
  269.                 .addContainerGap()
  270.                 .addComponent(jToggleButtonSelect)
  271.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  272.                 .addComponent(jToggleButtonMove)
  273.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  274.                 .addComponent(jToggleButtonRectangle, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE)
  275.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  276.                 .addComponent(jToggleButtonEllipse)
  277.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  278.                 .addComponent(jToggleButtonPentagram)
  279.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  280.                 .addComponent(jToggleButton1)
  281.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  282.                 .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  283.                     .addComponent(jButton1)
  284.                     .addComponent(jButton2))
  285.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  286.                 .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  287.                     .addGroup(jPanel1Layout.createSequentialGroup()
  288.                         .addGap(11, 11, 11)
  289.                         .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 12, javax.swing.GroupLayout.PREFERRED_SIZE))
  290.                     .addComponent(jButton4))
  291.                 .addGap(8, 8, 8)
  292.                 .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
  293.                     .addComponent(jLabel2)
  294.                     .addComponent(jPanelCurrentColor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  295.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  296.                 .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  297.                     .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.TRAILING)
  298.                     .addComponent(jPanelObjectColor, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  299.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  300.                 .addComponent(jSliderRotation, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  301.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  302.                 .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 143, Short.MAX_VALUE))
  303.         );
  304.  
  305.         jPanelCanvas.setBackground(java.awt.Color.white);
  306.         jPanelCanvas.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
  307.         jPanelCanvas.addMouseListener(new java.awt.event.MouseAdapter() {
  308.             public void mousePressed(java.awt.event.MouseEvent evt) {
  309.                 jPanelCanvasMousePressed(evt);
  310.             }
  311.             public void mouseReleased(java.awt.event.MouseEvent evt) {
  312.                 jPanelCanvasMouseReleased(evt);
  313.             }
  314.         });
  315.         jPanelCanvas.addComponentListener(new java.awt.event.ComponentAdapter() {
  316.             public void componentResized(java.awt.event.ComponentEvent evt) {
  317.                 jPanelCanvasComponentResized(evt);
  318.             }
  319.         });
  320.         jPanelCanvas.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
  321.             public void mouseDragged(java.awt.event.MouseEvent evt) {
  322.                 jPanelCanvasMouseDragged(evt);
  323.             }
  324.         });
  325.         jPanelCanvas.addKeyListener(new java.awt.event.KeyAdapter() {
  326.             public void keyPressed(java.awt.event.KeyEvent evt) {
  327.                 jPanelCanvasKeyPressed(evt);
  328.             }
  329.             public void keyReleased(java.awt.event.KeyEvent evt) {
  330.                 jPanelCanvasKeyReleased(evt);
  331.             }
  332.         });
  333.  
  334.         javax.swing.GroupLayout jPanelCanvasLayout = new javax.swing.GroupLayout(jPanelCanvas);
  335.         jPanelCanvas.setLayout(jPanelCanvasLayout);
  336.         jPanelCanvasLayout.setHorizontalGroup(
  337.             jPanelCanvasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  338.             .addGap(0, 414, Short.MAX_VALUE)
  339.         );
  340.         jPanelCanvasLayout.setVerticalGroup(
  341.             jPanelCanvasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  342.             .addGap(0, 511, Short.MAX_VALUE)
  343.         );
  344.  
  345.         jMenu1.setText("Файл");
  346.  
  347.         jMenuItem3.setText("Отваряне");
  348.         jMenuItem3.addActionListener(new java.awt.event.ActionListener() {
  349.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  350.                 jMenuItem3ActionPerformed(evt);
  351.             }
  352.         });
  353.         jMenu1.add(jMenuItem3);
  354.  
  355.         jMenuItem4.setText("Запазване");
  356.         jMenuItem4.addActionListener(new java.awt.event.ActionListener() {
  357.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  358.                 jMenuItem4ActionPerformed(evt);
  359.             }
  360.         });
  361.         jMenu1.add(jMenuItem4);
  362.  
  363.         jMenuItem1.setText("Изход");
  364.         jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
  365.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  366.                 jMenuItem1ActionPerformed(evt);
  367.             }
  368.         });
  369.         jMenu1.add(jMenuItem1);
  370.  
  371.         jMenuBar1.add(jMenu1);
  372.  
  373.         jMenu2.setText("Помощ");
  374.         jMenu2.addMenuDragMouseListener(new javax.swing.event.MenuDragMouseListener() {
  375.             public void menuDragMouseDragged(javax.swing.event.MenuDragMouseEvent evt) {
  376.                 jMenu2MenuDragMouseDragged(evt);
  377.             }
  378.             public void menuDragMouseEntered(javax.swing.event.MenuDragMouseEvent evt) {
  379.             }
  380.             public void menuDragMouseExited(javax.swing.event.MenuDragMouseEvent evt) {
  381.             }
  382.             public void menuDragMouseReleased(javax.swing.event.MenuDragMouseEvent evt) {
  383.             }
  384.         });
  385.  
  386.         jMenuItem2.setText("За програмата");
  387.         jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
  388.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  389.                 jMenuItem2ActionPerformed(evt);
  390.             }
  391.         });
  392.         jMenu2.add(jMenuItem2);
  393.  
  394.         jMenuBar1.add(jMenu2);
  395.  
  396.         setJMenuBar(jMenuBar1);
  397.  
  398.         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  399.         getContentPane().setLayout(layout);
  400.         layout.setHorizontalGroup(
  401.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  402.             .addGroup(layout.createSequentialGroup()
  403.                 .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 209, javax.swing.GroupLayout.PREFERRED_SIZE)
  404.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  405.                 .addComponent(jPanelCanvas, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  406.                 .addContainerGap())
  407.         );
  408.         layout.setVerticalGroup(
  409.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  410.             .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
  411.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
  412.                     .addGroup(layout.createSequentialGroup()
  413.                         .addGap(12, 12, 12)
  414.                         .addComponent(jPanelCanvas, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  415.                     .addGroup(layout.createSequentialGroup()
  416.                         .addGap(42, 42, 42)
  417.                         .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
  418.                 .addContainerGap())
  419.         );
  420.  
  421.         pack();
  422.     }// </editor-fold>                        
  423.  
  424.     private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {                                          
  425.         System.exit(0);
  426.     }                                          
  427.  
  428.     private void jPanelCanvasMousePressed(java.awt.event.MouseEvent evt) {                                          
  429.         GlobalVars.startPoint = evt.getPoint();
  430.         GlobalVars.moveLastPoint = evt.getPoint();
  431.     }                                        
  432.  
  433.     private void jPanelCanvasMouseDragged(java.awt.event.MouseEvent evt) {                                          
  434.         GlobalVars.drawFigures();
  435.  
  436.         int x, y;
  437.         int width = Math.abs(evt.getX() - GlobalVars.startPoint.x);
  438.         int height = Math.abs(evt.getY() - GlobalVars.startPoint.y);
  439.  
  440.         if (GlobalVars.startPoint.x < evt.getX())
  441.             x = GlobalVars.startPoint.x;
  442.         else x = evt.getX();
  443.  
  444.         if (GlobalVars.startPoint.y < evt.getY())
  445.             y = GlobalVars.startPoint.y;
  446.         else y = evt.getY();
  447.  
  448.         GlobalVars.lastX = x;
  449.         GlobalVars.lastY = y;
  450.         GlobalVars.lastWidth = width;
  451.         GlobalVars.lastHeight = height;
  452.  
  453.         if (jToggleButtonSelect.isSelected()) {
  454.             GlobalVars.graphics2D.setColor(Color.BLACK);
  455.             GlobalVars.graphics2D.setStroke(new BasicStroke(
  456.                     1,
  457.                     BasicStroke.CAP_BUTT,
  458.                     BasicStroke.JOIN_BEVEL,
  459.                     0,
  460.                     new float[]{6, 3},
  461.                     0));
  462.  
  463.             GlobalVars.graphics2D.drawRect(x, y, width, height);
  464.         } else if (jToggleButtonRectangle.isSelected()) {
  465.             (new Rect(
  466.                     x, y,
  467.                     width, height,
  468.                     jPanelCurrentColor.getBackground())
  469.                     ).draw(GlobalVars.graphics2D);
  470.         } else if (jToggleButton1.isSelected()) {
  471.             (new Rect2(
  472.                 x, y,
  473.                 width, height,
  474.                 jPanelCurrentColor.getBackground())
  475.                 ).draw(GlobalVars.graphics2D);
  476.         } else if (jToggleButtonEllipse.isSelected()) {
  477.             (new Ellipse(
  478.                     x, y,
  479.                     width, height,
  480.                     jPanelCurrentColor.getBackground())
  481.                     ).draw(GlobalVars.graphics2D);
  482.         } else if (jToggleButtonPentagram.isSelected()) {
  483.             (new Pentagram(
  484.                     x, y,
  485.                     width, height,
  486.                     jPanelCurrentColor.getBackground())
  487.                     ).draw(GlobalVars.graphics2D);
  488.         } else if (jToggleButtonMove.isSelected()) {
  489.             for (int i = 0; i < GlobalVars.figures.size(); i++) {
  490.                 Primitive primitive = GlobalVars.figures.get(i);
  491.                 if(primitive.isSelected()) {
  492.                     ((Rectangle)primitive).translate(
  493.                             evt.getX() - GlobalVars.moveLastPoint.x,
  494.                             evt.getY() - GlobalVars.moveLastPoint.y
  495.                             );
  496.                 }
  497.             }
  498.             GlobalVars.moveLastPoint = evt.getPoint();
  499.         }
  500.     }                                        
  501.  
  502.     private void jPanelCanvasMouseReleased(java.awt.event.MouseEvent evt) {                                          
  503.         GlobalVars.clearLastRect();
  504.         GlobalVars.graphics2D.setStroke(new BasicStroke());
  505.  
  506.         int x, y;
  507.         int width = Math.abs(evt.getX() - GlobalVars.startPoint.x);
  508.         int height = Math.abs(evt.getY() - GlobalVars.startPoint.y);
  509.  
  510.         if (GlobalVars.startPoint.x < evt.getX())
  511.             x = GlobalVars.startPoint.x;
  512.         else x = evt.getX();
  513.  
  514.         if (GlobalVars.startPoint.y < evt.getY())
  515.             y = GlobalVars.startPoint.y;
  516.         else y = evt.getY();
  517.  
  518.         if (jToggleButtonSelect.isSelected()) {
  519.             GlobalVars.select(new Rectangle(x, y, width, height));
  520.         } else if (jToggleButtonRectangle.isSelected()) {
  521.             GlobalVars.deselectAll();
  522.             Primitive primitive = new Rect(
  523.                     x, y,
  524.                     width, height,
  525.                     jPanelCurrentColor.getBackground());
  526.             primitive.setSelected(true);
  527.             jPanelObjectColor.setBackground(jPanelCurrentColor.getBackground());
  528.             jSliderRotation.setValue(0);
  529.             GlobalVars.sliderLastPosition = 0;
  530.             GlobalVars.figures.add(primitive);
  531.         } else if (jToggleButton1.isSelected()) {
  532.             GlobalVars.deselectAll();
  533.             Primitive primitive = new Rect2(
  534.                     x, y,
  535.                     width, height,
  536.                     jPanelCurrentColor.getBackground());
  537.             primitive.setSelected(true);
  538.             jPanelObjectColor.setBackground(jPanelCurrentColor.getBackground());
  539.             jSliderRotation.setValue(0);
  540.             GlobalVars.sliderLastPosition = 0;
  541.             GlobalVars.figures.add(primitive);
  542.         } else if (jToggleButtonEllipse.isSelected()) {
  543.             GlobalVars.deselectAll();
  544.             Primitive primitive = new Ellipse(
  545.                     x, y,
  546.                     width, height,
  547.                     jPanelCurrentColor.getBackground());
  548.             primitive.setSelected(true);
  549.             jPanelObjectColor.setBackground(jPanelCurrentColor.getBackground());
  550.             jSliderRotation.setValue(0);
  551.             GlobalVars.sliderLastPosition = 0;
  552.             GlobalVars.figures.add(primitive);
  553.         } else if (jToggleButtonPentagram.isSelected()) {
  554.             GlobalVars.deselectAll();
  555.             Primitive primitive = new Pentagram(
  556.                     x, y,
  557.                     width, height,
  558.                     jPanelCurrentColor.getBackground());
  559.             primitive.setSelected(true);
  560.             jPanelObjectColor.setBackground(jPanelCurrentColor.getBackground());
  561.             jSliderRotation.setValue(0);
  562.             GlobalVars.sliderLastPosition = 0;
  563.             GlobalVars.figures.add(primitive);
  564.         }
  565.         jPanelCanvas.repaint();
  566.         GlobalVars.drawTimer = new Timer();
  567.         GlobalVars.drawTimer.schedule(new DrawTask(), GlobalVars.drawInterval);
  568.     }                                          
  569.  
  570.     private void jPanelCurrentColorMouseClicked(java.awt.event.MouseEvent evt) {                                                
  571.         Color color = JColorChooser.showDialog(
  572.                 jPanelCurrentColor,
  573.                 "Изберете цвят",
  574.                 jPanelCurrentColor.getBackground()
  575.                 );
  576.         if(color != null) { jPanelCurrentColor.setBackground(color); }
  577.     }                                              
  578.  
  579.     private void jPanelCanvasComponentResized(java.awt.event.ComponentEvent evt) {                                              
  580.         GlobalVars.graphics2D = (Graphics2D)jPanelCanvas.getGraphics();
  581.         GlobalVars.canvasSize.x = jPanelCanvas.getWidth();
  582.         GlobalVars.canvasSize.y = jPanelCanvas.getHeight();
  583.         GlobalVars.drawFigures();
  584.         GlobalVars.drawTimer = new Timer();
  585.         GlobalVars.drawTimer.schedule(new DrawTask(), GlobalVars.drawInterval);
  586. }                                            
  587.  
  588.     private void formFocusGained(java.awt.event.FocusEvent evt) {                                
  589.         jPanelCanvasComponentResized(evt);
  590.     }                                
  591.  
  592.     private void formWindowActivated(java.awt.event.WindowEvent evt) {                                    
  593.         jPanelCanvasComponentResized(evt);
  594.     }                                    
  595.  
  596.     private void jPanelCanvasKeyPressed(java.awt.event.KeyEvent evt) {                                        
  597.         if(evt.getKeyCode() == KeyEvent.VK_CONTROL) {
  598.             GlobalVars.ctrlPressed = true;
  599.         }
  600.     }                                      
  601.  
  602.     private void jPanelCanvasKeyReleased(java.awt.event.KeyEvent evt) {                                        
  603.         GlobalVars.ctrlPressed = false;
  604.     }                                        
  605.  
  606.     private void jToggleButtonSelectActionPerformed(java.awt.event.ActionEvent evt) {                                                    
  607.         jPanelCanvas.requestFocusInWindow();
  608.     }                                                  
  609.  
  610.     private void jPanelObjectColorMousePressed(java.awt.event.MouseEvent evt) {                                              
  611.         Color color = JColorChooser.showDialog(
  612.                 jPanelObjectColor,
  613.                 "Изберете цвят",
  614.                 jPanelObjectColor.getBackground()
  615.                 );
  616.  
  617.         if(color != null) {
  618.             jPanelObjectColor.setBackground(color);
  619.        
  620.             for (int i = 0; i < GlobalVars.figures.size(); i++) {
  621.                 if(GlobalVars.figures.get(i).isSelected()) {
  622.                     GlobalVars.figures.get(i).setColor(
  623.                             jPanelObjectColor.getBackground()
  624.                             );
  625.                 }
  626.             }
  627.         }
  628.     }                                              
  629.  
  630.     private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {                                          
  631.         JOptionPane.showMessageDialog(
  632.                 this,
  633.                 "Проект по комютърна графика "
  634.                       );
  635.     }                                          
  636.  
  637.     private void jSliderRotationStateChanged(javax.swing.event.ChangeEvent evt) {                                            
  638.         for (int i = 0; i < GlobalVars.figures.size(); i++) {
  639.             Primitive primitive = GlobalVars.figures.get(i);
  640.             if(primitive.isSelected()) {
  641.                     primitive.addToAngleOfRotation(
  642.                         jSliderRotation.getValue() - GlobalVars.sliderLastPosition
  643.                         );
  644.             }
  645.         }
  646.         GlobalVars.sliderLastPosition = jSliderRotation.getValue();
  647.         jLabel3.setText("Завъртане " + jSliderRotation.getValue());
  648.         GlobalVars.drawFigures();
  649.     }                                            
  650.  
  651.     private void jSliderRotationMousePressed(java.awt.event.MouseEvent evt) {                                            
  652.         if(jToggleButtonSelect.isSelected()) {
  653.             jPanelCanvas.requestFocusInWindow();
  654.         }
  655.     }                                            
  656.  
  657.     private void jSliderRotationMouseReleased(java.awt.event.MouseEvent evt) {                                              
  658.         jPanelCanvas.repaint();
  659.         GlobalVars.drawTimer = new Timer();
  660.         GlobalVars.drawTimer.schedule(new DrawTask(), GlobalVars.drawInterval);
  661.     }                                            
  662.  
  663.     private void jMenuItem4ActionPerformed(java.awt.event.ActionEvent evt) {                                          
  664.         JFileChooser fileChooser = new JFileChooser();
  665.         fileChooser.setFileFilter(new FileNameExtensionFilter(
  666.                 "Векторни изображения - KGraf (*.kgraf)",
  667.                 "kgraf")
  668.                 );
  669.  
  670.         fileChooser.showSaveDialog(this);
  671.         if(fileChooser.getSelectedFile() == null) return;
  672.         String fileName = fileChooser.getSelectedFile().getPath();
  673.  
  674.         if(!fileName.endsWith(".kgraf")) {
  675.             fileName += ".kgraf";
  676.         }
  677.  
  678.         FileOutputStream fos = null;
  679.         try {
  680.             fos = new FileOutputStream(fileName);
  681.         } catch (FileNotFoundException ex) {
  682.             Logger.getLogger(GUI.class.getName()).log(Level.SEVERE, null, ex);
  683.         }
  684.  
  685.         try {
  686.             ObjectOutputStream objectOutputStream = new ObjectOutputStream(fos);
  687.             objectOutputStream.writeObject(GlobalVars.figures);
  688.             objectOutputStream.close();
  689.         } catch (IOException ex) {
  690.             Logger.getLogger(GUI.class.getName()).log(Level.SEVERE, null, ex);
  691.         }
  692.     }                                          
  693.  
  694.     private void jMenuItem3ActionPerformed(java.awt.event.ActionEvent evt) {                                          
  695.         JFileChooser fileChooser = new JFileChooser();
  696.         fileChooser.setFileFilter(new FileNameExtensionFilter(
  697.                 "Векторни изображения - kgraf (*.kgraf)",
  698.                 "kgraf")
  699.                 );
  700.  
  701.         fileChooser.showOpenDialog(this);
  702.         if(fileChooser.getSelectedFile() == null) return;
  703.         String fileName = fileChooser.getSelectedFile().getPath();
  704.  
  705.         FileInputStream fis = null;
  706.         try {
  707.             fis = new FileInputStream(fileName);
  708.         } catch (FileNotFoundException ex) {
  709.             Logger.getLogger(GUI.class.getName()).log(Level.SEVERE, null, ex);
  710.         }
  711.  
  712.         try {
  713.             ObjectInputStream objectInputStream = new ObjectInputStream(fis);
  714.             try {
  715.                 GlobalVars.figures =
  716.                         (ArrayList<Primitive>) objectInputStream.readObject();
  717.             } catch (ClassNotFoundException ex) {
  718.                 Logger.getLogger(GUI.class.getName()).log(Level.SEVERE, null, ex);
  719.             }
  720.         } catch (IOException ex) {
  721.             Logger.getLogger(GUI.class.getName()).log(Level.SEVERE, null, ex);
  722.         }
  723.     }                                          
  724.  
  725.     private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                        
  726.         for (int i = 1; i < GlobalVars.figures.size(); i++) {
  727.             if(GlobalVars.figures.get(i).isSelected()) {
  728.                 CommonPrimitive tempFigure =
  729.                         (CommonPrimitive) GlobalVars.figures.get(i);
  730.                 GlobalVars.figures.set(i, GlobalVars.figures.get(i-1));
  731.                 GlobalVars.figures.set(i-1, (Primitive) tempFigure);
  732.             }
  733.         }
  734.         GlobalVars.drawFigures();
  735.     }                                        
  736.  
  737.     private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                        
  738.         for (int i = GlobalVars.figures.size()-2; i>=0; i--) {
  739.             if(GlobalVars.figures.get(i).isSelected()) {
  740.                 CommonPrimitive tempFigure =
  741.                         (CommonPrimitive) GlobalVars.figures.get(i);
  742.                 GlobalVars.figures.set(i, GlobalVars.figures.get(i+1));
  743.                 GlobalVars.figures.set(i+1, (Primitive) tempFigure);
  744.             }
  745.         }
  746.         GlobalVars.drawFigures();
  747. }                                        
  748.  
  749.     private void jButton1MouseReleased(java.awt.event.MouseEvent evt) {                                      
  750.         jSliderRotationMousePressed(evt);
  751. }                                      
  752.  
  753.     private void jButton2MouseReleased(java.awt.event.MouseEvent evt) {                                      
  754.         jSliderRotationMousePressed(evt);
  755. }                                      
  756.  
  757.     private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {                                        
  758.         int i = GlobalVars.figures.size() - 1;
  759.         while(i >= 0) {
  760.             if(GlobalVars.figures.get(i).isSelected()) {
  761.                 GlobalVars.figures.remove(i);
  762.             }
  763.             i--;
  764.         }
  765.         GlobalVars.drawFigures();
  766.     }                                        
  767.  
  768.     private void jButton4MouseReleased(java.awt.event.MouseEvent evt) {                                      
  769.         jSliderRotationMousePressed(evt);
  770.     }                                      
  771.  
  772.     private void jList1MouseClicked(java.awt.event.MouseEvent evt) {                                    
  773.         if(!evt.isControlDown()) GlobalVars.deselectAll();
  774.         int selectedIndices[] = jList1.getSelectedIndices();
  775.         for(int i = 0; i < selectedIndices.length; i++) {
  776.             GlobalVars.figures.get(selectedIndices[i]).setSelected(true);
  777.             jPanelObjectColor.setBackground(
  778.                     GlobalVars.figures.get(selectedIndices[i]).getColor()
  779.                     );
  780.         }
  781.         GlobalVars.drawFigures();
  782.     }                                  
  783.  
  784.     private void jList1MouseReleased(java.awt.event.MouseEvent evt) {                                    
  785.         jSliderRotationMousePressed(evt);
  786.     }                                    
  787.  
  788.     private void jMenu2MenuDragMouseDragged(javax.swing.event.MenuDragMouseEvent evt) {                                            
  789.         // TODO add your handling code here:
  790.     }                                          
  791.  
  792.     private void jToggleButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                              
  793.         // TODO add your handling code here:
  794.     }                                              
  795.  
  796.     private void jToggleButtonRectangleActionPerformed(java.awt.event.ActionEvent evt) {                                                      
  797.         // TODO add your handling code here:
  798.     }                                                      
  799.  
  800.     /**
  801.      * @param args the command line arguments
  802.      */
  803.     public static void main(String args[]) {
  804.         java.awt.EventQueue.invokeLater(new Runnable() {
  805.  
  806.             public void run() {
  807.                 new GUI().setVisible(true);
  808.             }
  809.         });
  810.     }
  811.     // Variables declaration - do not modify                    
  812.     private javax.swing.ButtonGroup buttonGroup1;
  813.     private javax.swing.JButton jButton1;
  814.     private javax.swing.JButton jButton2;
  815.     private javax.swing.JButton jButton4;
  816.     private javax.swing.JLabel jLabel1;
  817.     private javax.swing.JLabel jLabel2;
  818.     private javax.swing.JLabel jLabel3;
  819.     private javax.swing.JList jList1;
  820.     private javax.swing.JMenu jMenu1;
  821.     private javax.swing.JMenu jMenu2;
  822.     private javax.swing.JMenuBar jMenuBar1;
  823.     private javax.swing.JMenuItem jMenuItem1;
  824.     private javax.swing.JMenuItem jMenuItem2;
  825.     private javax.swing.JMenuItem jMenuItem3;
  826.     private javax.swing.JMenuItem jMenuItem4;
  827.     private javax.swing.JPanel jPanel1;
  828.     private javax.swing.JPanel jPanelCanvas;
  829.     private javax.swing.JPanel jPanelCurrentColor;
  830.     private javax.swing.JPanel jPanelObjectColor;
  831.     private javax.swing.JScrollPane jScrollPane1;
  832.     private javax.swing.JSlider jSliderRotation;
  833.     private javax.swing.JToggleButton jToggleButton1;
  834.     private javax.swing.JToggleButton jToggleButtonEllipse;
  835.     private javax.swing.JToggleButton jToggleButtonMove;
  836.     private javax.swing.JToggleButton jToggleButtonPentagram;
  837.     private javax.swing.JToggleButton jToggleButtonRectangle;
  838.     private javax.swing.JToggleButton jToggleButtonSelect;
  839.     // End of variables declaration                  
  840. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement