Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Program;
- import java.awt.BasicStroke;
- import java.awt.Color;
- import java.awt.Graphics2D;
- import java.awt.Rectangle;
- import java.awt.event.KeyEvent;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.ObjectInputStream;
- import java.io.ObjectOutputStream;
- import java.util.ArrayList;
- import java.util.Timer;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- import javax.swing.JColorChooser;
- import javax.swing.JFileChooser;
- import javax.swing.JOptionPane;
- import javax.swing.filechooser.FileNameExtensionFilter;
- public class GUI extends javax.swing.JFrame {
- public GUI() {
- initComponents();
- GlobalVars.jPanelObjectColor = jPanelObjectColor;
- GlobalVars.jSliderRotation = jSliderRotation;
- GlobalVars.jListFigures = jList1;
- }
- /** This method is called from within the constructor to
- * initialize the form.
- * WARNING: Do NOT modify this code. The content of this method is
- * always regenerated by the Form Editor.
- */
- @SuppressWarnings("unchecked")
- // <editor-fold defaultstate="collapsed" desc="Generated Code">
- private void initComponents() {
- buttonGroup1 = new javax.swing.ButtonGroup();
- jPanel1 = new javax.swing.JPanel();
- jToggleButtonSelect = new javax.swing.JToggleButton();
- jToggleButtonRectangle = new javax.swing.JToggleButton();
- jToggleButtonEllipse = new javax.swing.JToggleButton();
- jToggleButtonMove = new javax.swing.JToggleButton();
- jLabel1 = new javax.swing.JLabel();
- jLabel2 = new javax.swing.JLabel();
- jPanelObjectColor = new javax.swing.JPanel();
- jLabel3 = new javax.swing.JLabel();
- jSliderRotation = new javax.swing.JSlider();
- jButton1 = new javax.swing.JButton();
- jButton2 = new javax.swing.JButton();
- jButton4 = new javax.swing.JButton();
- jScrollPane1 = new javax.swing.JScrollPane();
- jList1 = new javax.swing.JList();
- jToggleButtonPentagram = new javax.swing.JToggleButton();
- jPanelCurrentColor = new javax.swing.JPanel();
- jToggleButton1 = new javax.swing.JToggleButton();
- jPanelCanvas = new javax.swing.JPanel();
- jMenuBar1 = new javax.swing.JMenuBar();
- jMenu1 = new javax.swing.JMenu();
- jMenuItem3 = new javax.swing.JMenuItem();
- jMenuItem4 = new javax.swing.JMenuItem();
- jMenuItem1 = new javax.swing.JMenuItem();
- jMenu2 = new javax.swing.JMenu();
- jMenuItem2 = new javax.swing.JMenuItem();
- setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
- setTitle("Trayangle - trekto.info");
- setBackground(java.awt.Color.white);
- addWindowListener(new java.awt.event.WindowAdapter() {
- public void windowActivated(java.awt.event.WindowEvent evt) {
- formWindowActivated(evt);
- }
- });
- addFocusListener(new java.awt.event.FocusAdapter() {
- public void focusGained(java.awt.event.FocusEvent evt) {
- formFocusGained(evt);
- }
- });
- buttonGroup1.add(jToggleButtonSelect);
- jToggleButtonSelect.setText("Избиране");
- jToggleButtonSelect.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- jToggleButtonSelectActionPerformed(evt);
- }
- });
- buttonGroup1.add(jToggleButtonRectangle);
- jToggleButtonRectangle.setSelected(true);
- jToggleButtonRectangle.setText("Правоъгълник");
- jToggleButtonRectangle.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- jToggleButtonRectangleActionPerformed(evt);
- }
- });
- buttonGroup1.add(jToggleButtonEllipse);
- jToggleButtonEllipse.setText("Елипса");
- buttonGroup1.add(jToggleButtonMove);
- jToggleButtonMove.setText("Преместване");
- jLabel1.setText("Текущ цвят");
- jLabel2.setText("Избран обект");
- jPanelObjectColor.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
- jPanelObjectColor.setPreferredSize(new java.awt.Dimension(25, 25));
- jPanelObjectColor.addMouseListener(new java.awt.event.MouseAdapter() {
- public void mousePressed(java.awt.event.MouseEvent evt) {
- jPanelObjectColorMousePressed(evt);
- }
- });
- javax.swing.GroupLayout jPanelObjectColorLayout = new javax.swing.GroupLayout(jPanelObjectColor);
- jPanelObjectColor.setLayout(jPanelObjectColorLayout);
- jPanelObjectColorLayout.setHorizontalGroup(
- jPanelObjectColorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGap(0, 23, Short.MAX_VALUE)
- );
- jPanelObjectColorLayout.setVerticalGroup(
- jPanelObjectColorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGap(0, 23, Short.MAX_VALUE)
- );
- jLabel3.setText("Завъртане");
- jSliderRotation.setMaximum(90);
- jSliderRotation.setMinimum(-90);
- jSliderRotation.setValue(0);
- jSliderRotation.addMouseListener(new java.awt.event.MouseAdapter() {
- public void mousePressed(java.awt.event.MouseEvent evt) {
- jSliderRotationMousePressed(evt);
- }
- public void mouseReleased(java.awt.event.MouseEvent evt) {
- jSliderRotationMouseReleased(evt);
- }
- });
- jSliderRotation.addChangeListener(new javax.swing.event.ChangeListener() {
- public void stateChanged(javax.swing.event.ChangeEvent evt) {
- jSliderRotationStateChanged(evt);
- }
- });
- jButton1.setText("Напред");
- jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
- public void mouseReleased(java.awt.event.MouseEvent evt) {
- jButton1MouseReleased(evt);
- }
- });
- jButton1.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- jButton1ActionPerformed(evt);
- }
- });
- jButton2.setText("Назад");
- jButton2.addMouseListener(new java.awt.event.MouseAdapter() {
- public void mouseReleased(java.awt.event.MouseEvent evt) {
- jButton2MouseReleased(evt);
- }
- });
- jButton2.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- jButton2ActionPerformed(evt);
- }
- });
- jButton4.setText("Премахване");
- jButton4.addMouseListener(new java.awt.event.MouseAdapter() {
- public void mouseReleased(java.awt.event.MouseEvent evt) {
- jButton4MouseReleased(evt);
- }
- });
- jButton4.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- jButton4ActionPerformed(evt);
- }
- });
- jList1.setModel(new javax.swing.AbstractListModel() {
- String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
- public int getSize() { return strings.length; }
- public Object getElementAt(int i) { return strings[i]; }
- });
- jList1.addMouseListener(new java.awt.event.MouseAdapter() {
- public void mouseClicked(java.awt.event.MouseEvent evt) {
- jList1MouseClicked(evt);
- }
- public void mouseReleased(java.awt.event.MouseEvent evt) {
- jList1MouseReleased(evt);
- }
- });
- jScrollPane1.setViewportView(jList1);
- buttonGroup1.add(jToggleButtonPentagram);
- jToggleButtonPentagram.setText("Петолъчка");
- jPanelCurrentColor.setBackground(new java.awt.Color(79, 220, 48));
- jPanelCurrentColor.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
- jPanelCurrentColor.setPreferredSize(new java.awt.Dimension(25, 25));
- jPanelCurrentColor.addMouseListener(new java.awt.event.MouseAdapter() {
- public void mouseClicked(java.awt.event.MouseEvent evt) {
- jPanelCurrentColorMouseClicked(evt);
- }
- });
- javax.swing.GroupLayout jPanelCurrentColorLayout = new javax.swing.GroupLayout(jPanelCurrentColor);
- jPanelCurrentColor.setLayout(jPanelCurrentColorLayout);
- jPanelCurrentColorLayout.setHorizontalGroup(
- jPanelCurrentColorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGap(0, 23, Short.MAX_VALUE)
- );
- jPanelCurrentColorLayout.setVerticalGroup(
- jPanelCurrentColorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGap(0, 23, Short.MAX_VALUE)
- );
- jToggleButton1.setText("NEW");
- jToggleButton1.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- jToggleButton1ActionPerformed(evt);
- }
- });
- javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
- jPanel1.setLayout(jPanel1Layout);
- jPanel1Layout.setHorizontalGroup(
- jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(jPanel1Layout.createSequentialGroup()
- .addContainerGap()
- .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
- .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
- .addComponent(jButton1)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
- .addComponent(jButton4, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
- .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
- .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addComponent(jLabel3)
- .addComponent(jLabel2))
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
- .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addComponent(jPanelCurrentColor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addComponent(jPanelObjectColor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
- .addGap(14, 14, 14)))
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(jLabel1))
- .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
- .addComponent(jToggleButtonSelect, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
- .addComponent(jToggleButtonMove, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
- .addComponent(jToggleButtonRectangle, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 149, Short.MAX_VALUE)
- .addComponent(jToggleButtonEllipse, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
- .addComponent(jToggleButtonPentagram, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
- .addComponent(jToggleButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
- .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
- .addComponent(jSliderRotation, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 199, Short.MAX_VALUE)
- .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING))))
- );
- jPanel1Layout.setVerticalGroup(
- jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(jPanel1Layout.createSequentialGroup()
- .addContainerGap()
- .addComponent(jToggleButtonSelect)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(jToggleButtonMove)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(jToggleButtonRectangle, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(jToggleButtonEllipse)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(jToggleButtonPentagram)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(jToggleButton1)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(jButton1)
- .addComponent(jButton2))
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(jPanel1Layout.createSequentialGroup()
- .addGap(11, 11, 11)
- .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 12, javax.swing.GroupLayout.PREFERRED_SIZE))
- .addComponent(jButton4))
- .addGap(8, 8, 8)
- .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
- .addComponent(jLabel2)
- .addComponent(jPanelCurrentColor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.TRAILING)
- .addComponent(jPanelObjectColor, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(jSliderRotation, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 143, Short.MAX_VALUE))
- );
- jPanelCanvas.setBackground(java.awt.Color.white);
- jPanelCanvas.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
- jPanelCanvas.addMouseListener(new java.awt.event.MouseAdapter() {
- public void mousePressed(java.awt.event.MouseEvent evt) {
- jPanelCanvasMousePressed(evt);
- }
- public void mouseReleased(java.awt.event.MouseEvent evt) {
- jPanelCanvasMouseReleased(evt);
- }
- });
- jPanelCanvas.addComponentListener(new java.awt.event.ComponentAdapter() {
- public void componentResized(java.awt.event.ComponentEvent evt) {
- jPanelCanvasComponentResized(evt);
- }
- });
- jPanelCanvas.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
- public void mouseDragged(java.awt.event.MouseEvent evt) {
- jPanelCanvasMouseDragged(evt);
- }
- });
- jPanelCanvas.addKeyListener(new java.awt.event.KeyAdapter() {
- public void keyPressed(java.awt.event.KeyEvent evt) {
- jPanelCanvasKeyPressed(evt);
- }
- public void keyReleased(java.awt.event.KeyEvent evt) {
- jPanelCanvasKeyReleased(evt);
- }
- });
- javax.swing.GroupLayout jPanelCanvasLayout = new javax.swing.GroupLayout(jPanelCanvas);
- jPanelCanvas.setLayout(jPanelCanvasLayout);
- jPanelCanvasLayout.setHorizontalGroup(
- jPanelCanvasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGap(0, 414, Short.MAX_VALUE)
- );
- jPanelCanvasLayout.setVerticalGroup(
- jPanelCanvasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGap(0, 511, Short.MAX_VALUE)
- );
- jMenu1.setText("Файл");
- jMenuItem3.setText("Отваряне");
- jMenuItem3.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- jMenuItem3ActionPerformed(evt);
- }
- });
- jMenu1.add(jMenuItem3);
- jMenuItem4.setText("Запазване");
- jMenuItem4.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- jMenuItem4ActionPerformed(evt);
- }
- });
- jMenu1.add(jMenuItem4);
- jMenuItem1.setText("Изход");
- jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- jMenuItem1ActionPerformed(evt);
- }
- });
- jMenu1.add(jMenuItem1);
- jMenuBar1.add(jMenu1);
- jMenu2.setText("Помощ");
- jMenu2.addMenuDragMouseListener(new javax.swing.event.MenuDragMouseListener() {
- public void menuDragMouseDragged(javax.swing.event.MenuDragMouseEvent evt) {
- jMenu2MenuDragMouseDragged(evt);
- }
- public void menuDragMouseEntered(javax.swing.event.MenuDragMouseEvent evt) {
- }
- public void menuDragMouseExited(javax.swing.event.MenuDragMouseEvent evt) {
- }
- public void menuDragMouseReleased(javax.swing.event.MenuDragMouseEvent evt) {
- }
- });
- jMenuItem2.setText("За програмата");
- jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- jMenuItem2ActionPerformed(evt);
- }
- });
- jMenu2.add(jMenuItem2);
- jMenuBar1.add(jMenu2);
- setJMenuBar(jMenuBar1);
- javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
- getContentPane().setLayout(layout);
- layout.setHorizontalGroup(
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 209, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(jPanelCanvas, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
- .addContainerGap())
- );
- layout.setVerticalGroup(
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
- .addGroup(layout.createSequentialGroup()
- .addGap(12, 12, 12)
- .addComponent(jPanelCanvas, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
- .addGroup(layout.createSequentialGroup()
- .addGap(42, 42, 42)
- .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
- .addContainerGap())
- );
- pack();
- }// </editor-fold>
- private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {
- System.exit(0);
- }
- private void jPanelCanvasMousePressed(java.awt.event.MouseEvent evt) {
- GlobalVars.startPoint = evt.getPoint();
- GlobalVars.moveLastPoint = evt.getPoint();
- }
- private void jPanelCanvasMouseDragged(java.awt.event.MouseEvent evt) {
- GlobalVars.drawFigures();
- int x, y;
- int width = Math.abs(evt.getX() - GlobalVars.startPoint.x);
- int height = Math.abs(evt.getY() - GlobalVars.startPoint.y);
- if (GlobalVars.startPoint.x < evt.getX())
- x = GlobalVars.startPoint.x;
- else x = evt.getX();
- if (GlobalVars.startPoint.y < evt.getY())
- y = GlobalVars.startPoint.y;
- else y = evt.getY();
- GlobalVars.lastX = x;
- GlobalVars.lastY = y;
- GlobalVars.lastWidth = width;
- GlobalVars.lastHeight = height;
- if (jToggleButtonSelect.isSelected()) {
- GlobalVars.graphics2D.setColor(Color.BLACK);
- GlobalVars.graphics2D.setStroke(new BasicStroke(
- 1,
- BasicStroke.CAP_BUTT,
- BasicStroke.JOIN_BEVEL,
- 0,
- new float[]{6, 3},
- 0));
- GlobalVars.graphics2D.drawRect(x, y, width, height);
- } else if (jToggleButtonRectangle.isSelected()) {
- (new Rect(
- x, y,
- width, height,
- jPanelCurrentColor.getBackground())
- ).draw(GlobalVars.graphics2D);
- } else if (jToggleButton1.isSelected()) {
- (new Rect2(
- x, y,
- width, height,
- jPanelCurrentColor.getBackground())
- ).draw(GlobalVars.graphics2D);
- } else if (jToggleButtonEllipse.isSelected()) {
- (new Ellipse(
- x, y,
- width, height,
- jPanelCurrentColor.getBackground())
- ).draw(GlobalVars.graphics2D);
- } else if (jToggleButtonPentagram.isSelected()) {
- (new Pentagram(
- x, y,
- width, height,
- jPanelCurrentColor.getBackground())
- ).draw(GlobalVars.graphics2D);
- } else if (jToggleButtonMove.isSelected()) {
- for (int i = 0; i < GlobalVars.figures.size(); i++) {
- Primitive primitive = GlobalVars.figures.get(i);
- if(primitive.isSelected()) {
- ((Rectangle)primitive).translate(
- evt.getX() - GlobalVars.moveLastPoint.x,
- evt.getY() - GlobalVars.moveLastPoint.y
- );
- }
- }
- GlobalVars.moveLastPoint = evt.getPoint();
- }
- }
- private void jPanelCanvasMouseReleased(java.awt.event.MouseEvent evt) {
- GlobalVars.clearLastRect();
- GlobalVars.graphics2D.setStroke(new BasicStroke());
- int x, y;
- int width = Math.abs(evt.getX() - GlobalVars.startPoint.x);
- int height = Math.abs(evt.getY() - GlobalVars.startPoint.y);
- if (GlobalVars.startPoint.x < evt.getX())
- x = GlobalVars.startPoint.x;
- else x = evt.getX();
- if (GlobalVars.startPoint.y < evt.getY())
- y = GlobalVars.startPoint.y;
- else y = evt.getY();
- if (jToggleButtonSelect.isSelected()) {
- GlobalVars.select(new Rectangle(x, y, width, height));
- } else if (jToggleButtonRectangle.isSelected()) {
- GlobalVars.deselectAll();
- Primitive primitive = new Rect(
- x, y,
- width, height,
- jPanelCurrentColor.getBackground());
- primitive.setSelected(true);
- jPanelObjectColor.setBackground(jPanelCurrentColor.getBackground());
- jSliderRotation.setValue(0);
- GlobalVars.sliderLastPosition = 0;
- GlobalVars.figures.add(primitive);
- } else if (jToggleButton1.isSelected()) {
- GlobalVars.deselectAll();
- Primitive primitive = new Rect2(
- x, y,
- width, height,
- jPanelCurrentColor.getBackground());
- primitive.setSelected(true);
- jPanelObjectColor.setBackground(jPanelCurrentColor.getBackground());
- jSliderRotation.setValue(0);
- GlobalVars.sliderLastPosition = 0;
- GlobalVars.figures.add(primitive);
- } else if (jToggleButtonEllipse.isSelected()) {
- GlobalVars.deselectAll();
- Primitive primitive = new Ellipse(
- x, y,
- width, height,
- jPanelCurrentColor.getBackground());
- primitive.setSelected(true);
- jPanelObjectColor.setBackground(jPanelCurrentColor.getBackground());
- jSliderRotation.setValue(0);
- GlobalVars.sliderLastPosition = 0;
- GlobalVars.figures.add(primitive);
- } else if (jToggleButtonPentagram.isSelected()) {
- GlobalVars.deselectAll();
- Primitive primitive = new Pentagram(
- x, y,
- width, height,
- jPanelCurrentColor.getBackground());
- primitive.setSelected(true);
- jPanelObjectColor.setBackground(jPanelCurrentColor.getBackground());
- jSliderRotation.setValue(0);
- GlobalVars.sliderLastPosition = 0;
- GlobalVars.figures.add(primitive);
- }
- jPanelCanvas.repaint();
- GlobalVars.drawTimer = new Timer();
- GlobalVars.drawTimer.schedule(new DrawTask(), GlobalVars.drawInterval);
- }
- private void jPanelCurrentColorMouseClicked(java.awt.event.MouseEvent evt) {
- Color color = JColorChooser.showDialog(
- jPanelCurrentColor,
- "Изберете цвят",
- jPanelCurrentColor.getBackground()
- );
- if(color != null) { jPanelCurrentColor.setBackground(color); }
- }
- private void jPanelCanvasComponentResized(java.awt.event.ComponentEvent evt) {
- GlobalVars.graphics2D = (Graphics2D)jPanelCanvas.getGraphics();
- GlobalVars.canvasSize.x = jPanelCanvas.getWidth();
- GlobalVars.canvasSize.y = jPanelCanvas.getHeight();
- GlobalVars.drawFigures();
- GlobalVars.drawTimer = new Timer();
- GlobalVars.drawTimer.schedule(new DrawTask(), GlobalVars.drawInterval);
- }
- private void formFocusGained(java.awt.event.FocusEvent evt) {
- jPanelCanvasComponentResized(evt);
- }
- private void formWindowActivated(java.awt.event.WindowEvent evt) {
- jPanelCanvasComponentResized(evt);
- }
- private void jPanelCanvasKeyPressed(java.awt.event.KeyEvent evt) {
- if(evt.getKeyCode() == KeyEvent.VK_CONTROL) {
- GlobalVars.ctrlPressed = true;
- }
- }
- private void jPanelCanvasKeyReleased(java.awt.event.KeyEvent evt) {
- GlobalVars.ctrlPressed = false;
- }
- private void jToggleButtonSelectActionPerformed(java.awt.event.ActionEvent evt) {
- jPanelCanvas.requestFocusInWindow();
- }
- private void jPanelObjectColorMousePressed(java.awt.event.MouseEvent evt) {
- Color color = JColorChooser.showDialog(
- jPanelObjectColor,
- "Изберете цвят",
- jPanelObjectColor.getBackground()
- );
- if(color != null) {
- jPanelObjectColor.setBackground(color);
- for (int i = 0; i < GlobalVars.figures.size(); i++) {
- if(GlobalVars.figures.get(i).isSelected()) {
- GlobalVars.figures.get(i).setColor(
- jPanelObjectColor.getBackground()
- );
- }
- }
- }
- }
- private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {
- JOptionPane.showMessageDialog(
- this,
- "Проект по комютърна графика "
- );
- }
- private void jSliderRotationStateChanged(javax.swing.event.ChangeEvent evt) {
- for (int i = 0; i < GlobalVars.figures.size(); i++) {
- Primitive primitive = GlobalVars.figures.get(i);
- if(primitive.isSelected()) {
- primitive.addToAngleOfRotation(
- jSliderRotation.getValue() - GlobalVars.sliderLastPosition
- );
- }
- }
- GlobalVars.sliderLastPosition = jSliderRotation.getValue();
- jLabel3.setText("Завъртане " + jSliderRotation.getValue());
- GlobalVars.drawFigures();
- }
- private void jSliderRotationMousePressed(java.awt.event.MouseEvent evt) {
- if(jToggleButtonSelect.isSelected()) {
- jPanelCanvas.requestFocusInWindow();
- }
- }
- private void jSliderRotationMouseReleased(java.awt.event.MouseEvent evt) {
- jPanelCanvas.repaint();
- GlobalVars.drawTimer = new Timer();
- GlobalVars.drawTimer.schedule(new DrawTask(), GlobalVars.drawInterval);
- }
- private void jMenuItem4ActionPerformed(java.awt.event.ActionEvent evt) {
- JFileChooser fileChooser = new JFileChooser();
- fileChooser.setFileFilter(new FileNameExtensionFilter(
- "Векторни изображения - KGraf (*.kgraf)",
- "kgraf")
- );
- fileChooser.showSaveDialog(this);
- if(fileChooser.getSelectedFile() == null) return;
- String fileName = fileChooser.getSelectedFile().getPath();
- if(!fileName.endsWith(".kgraf")) {
- fileName += ".kgraf";
- }
- FileOutputStream fos = null;
- try {
- fos = new FileOutputStream(fileName);
- } catch (FileNotFoundException ex) {
- Logger.getLogger(GUI.class.getName()).log(Level.SEVERE, null, ex);
- }
- try {
- ObjectOutputStream objectOutputStream = new ObjectOutputStream(fos);
- objectOutputStream.writeObject(GlobalVars.figures);
- objectOutputStream.close();
- } catch (IOException ex) {
- Logger.getLogger(GUI.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- private void jMenuItem3ActionPerformed(java.awt.event.ActionEvent evt) {
- JFileChooser fileChooser = new JFileChooser();
- fileChooser.setFileFilter(new FileNameExtensionFilter(
- "Векторни изображения - kgraf (*.kgraf)",
- "kgraf")
- );
- fileChooser.showOpenDialog(this);
- if(fileChooser.getSelectedFile() == null) return;
- String fileName = fileChooser.getSelectedFile().getPath();
- FileInputStream fis = null;
- try {
- fis = new FileInputStream(fileName);
- } catch (FileNotFoundException ex) {
- Logger.getLogger(GUI.class.getName()).log(Level.SEVERE, null, ex);
- }
- try {
- ObjectInputStream objectInputStream = new ObjectInputStream(fis);
- try {
- GlobalVars.figures =
- (ArrayList<Primitive>) objectInputStream.readObject();
- } catch (ClassNotFoundException ex) {
- Logger.getLogger(GUI.class.getName()).log(Level.SEVERE, null, ex);
- }
- } catch (IOException ex) {
- Logger.getLogger(GUI.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
- for (int i = 1; i < GlobalVars.figures.size(); i++) {
- if(GlobalVars.figures.get(i).isSelected()) {
- CommonPrimitive tempFigure =
- (CommonPrimitive) GlobalVars.figures.get(i);
- GlobalVars.figures.set(i, GlobalVars.figures.get(i-1));
- GlobalVars.figures.set(i-1, (Primitive) tempFigure);
- }
- }
- GlobalVars.drawFigures();
- }
- private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
- for (int i = GlobalVars.figures.size()-2; i>=0; i--) {
- if(GlobalVars.figures.get(i).isSelected()) {
- CommonPrimitive tempFigure =
- (CommonPrimitive) GlobalVars.figures.get(i);
- GlobalVars.figures.set(i, GlobalVars.figures.get(i+1));
- GlobalVars.figures.set(i+1, (Primitive) tempFigure);
- }
- }
- GlobalVars.drawFigures();
- }
- private void jButton1MouseReleased(java.awt.event.MouseEvent evt) {
- jSliderRotationMousePressed(evt);
- }
- private void jButton2MouseReleased(java.awt.event.MouseEvent evt) {
- jSliderRotationMousePressed(evt);
- }
- private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
- int i = GlobalVars.figures.size() - 1;
- while(i >= 0) {
- if(GlobalVars.figures.get(i).isSelected()) {
- GlobalVars.figures.remove(i);
- }
- i--;
- }
- GlobalVars.drawFigures();
- }
- private void jButton4MouseReleased(java.awt.event.MouseEvent evt) {
- jSliderRotationMousePressed(evt);
- }
- private void jList1MouseClicked(java.awt.event.MouseEvent evt) {
- if(!evt.isControlDown()) GlobalVars.deselectAll();
- int selectedIndices[] = jList1.getSelectedIndices();
- for(int i = 0; i < selectedIndices.length; i++) {
- GlobalVars.figures.get(selectedIndices[i]).setSelected(true);
- jPanelObjectColor.setBackground(
- GlobalVars.figures.get(selectedIndices[i]).getColor()
- );
- }
- GlobalVars.drawFigures();
- }
- private void jList1MouseReleased(java.awt.event.MouseEvent evt) {
- jSliderRotationMousePressed(evt);
- }
- private void jMenu2MenuDragMouseDragged(javax.swing.event.MenuDragMouseEvent evt) {
- // TODO add your handling code here:
- }
- private void jToggleButton1ActionPerformed(java.awt.event.ActionEvent evt) {
- // TODO add your handling code here:
- }
- private void jToggleButtonRectangleActionPerformed(java.awt.event.ActionEvent evt) {
- // TODO add your handling code here:
- }
- /**
- * @param args the command line arguments
- */
- public static void main(String args[]) {
- java.awt.EventQueue.invokeLater(new Runnable() {
- public void run() {
- new GUI().setVisible(true);
- }
- });
- }
- // Variables declaration - do not modify
- private javax.swing.ButtonGroup buttonGroup1;
- private javax.swing.JButton jButton1;
- private javax.swing.JButton jButton2;
- private javax.swing.JButton jButton4;
- private javax.swing.JLabel jLabel1;
- private javax.swing.JLabel jLabel2;
- private javax.swing.JLabel jLabel3;
- private javax.swing.JList jList1;
- private javax.swing.JMenu jMenu1;
- private javax.swing.JMenu jMenu2;
- private javax.swing.JMenuBar jMenuBar1;
- private javax.swing.JMenuItem jMenuItem1;
- private javax.swing.JMenuItem jMenuItem2;
- private javax.swing.JMenuItem jMenuItem3;
- private javax.swing.JMenuItem jMenuItem4;
- private javax.swing.JPanel jPanel1;
- private javax.swing.JPanel jPanelCanvas;
- private javax.swing.JPanel jPanelCurrentColor;
- private javax.swing.JPanel jPanelObjectColor;
- private javax.swing.JScrollPane jScrollPane1;
- private javax.swing.JSlider jSliderRotation;
- private javax.swing.JToggleButton jToggleButton1;
- private javax.swing.JToggleButton jToggleButtonEllipse;
- private javax.swing.JToggleButton jToggleButtonMove;
- private javax.swing.JToggleButton jToggleButtonPentagram;
- private javax.swing.JToggleButton jToggleButtonRectangle;
- private javax.swing.JToggleButton jToggleButtonSelect;
- // End of variables declaration
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement