Page 225 - CTS - CSA TP - Volume 2
P. 225

COMPUTER SOFTWARE APPLICATION - CITS





                   drawingPanel.addMouseListener(new MouseAdapter() {
                       @Override

                       public void mousePressed(MouseEvent e) {
                           startX = e.getX();
                           startY = e.getY();
                       }



                       @Override
                      public void mouseReleased(MouseEvent e) {
                           int endX = e.getX();
                           int endY = e.getY();

                           createShape(startX, startY, endX, endY);
                           repaint();
                       }
                   });


                   add(drawingPanel);

               }


               private void drawShapes(Graphics g) {
                   Graphics2D g2d = (Graphics2D) g;

                   g2d.setColor(Color.BLACK);


                   for (Shape shape : shapes) {
                       g2d.draw(shape);
                   }

               }


               private void createShape(int startX, int startY, int endX, int endY) {
                   int width = Math.abs(endX - startX);

                   int height = Math.abs(endY - startY);


                   if (width > 0 && height > 0) {
                       currentShape = new Rectangle(startX, startY, width, height);
                       shapes.add(currentShape);
                   }

               }






                                                           210

                              CITS : IT & ITES - Computer Software Application - Exercise 119                                                        CITS : IT & ITES - Computer Software Application - Exercise 119
   220   221   222   223   224   225   226   227   228   229   230