title: Drawing rectangle using mouse
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/1115359/how-to-draw-a-rectangle-on-a-java-applet-using-mouse-drag-event-and-make-it-stay">How to draw a rectangle on a java applet using mouse drag event and make it stay</a>  </p>
  </blockquote>
  
  
  
  <p>Hello. 
  I would like to know how I can draw rectangle using mousedragged event. I know that I must record mouse location using class Point. I need to implement paint function in paint method and in mousedragged call it or my paint code should implement in mousedragged event?</p>
  
  <p>At this time I put my code in mouse dragged event. This is code:</p>
  
  <pre><code>@Override
      public void mousePressed(MouseEvent e)
      {
          super.mousePressed(e);
          System.out.println("f.getGlassPane() mousePressed");
          if(e.getButton() == MouseEvent.BUTTON1)
  
  
          frame.getGlassPane().setVisible(true);
  
          startPoint=e.getPoint();
  
          Graphics2D g = null;
              Graphics2D g2 = (Graphics2D) g;
          Rectangle2D prostokat = new Rectangle2D.Double();
          prostokat.setFrameFromDiagonal(e.getPoint().x, e.getPoint().y,startPoint.x, startPoint.y);
          g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5F));
          g2.setColor(Color.BLUE);
          g2.fill(prostokat);
          g2.draw(prostokat);
  
      }
  
      });
  </code></pre>