Results 1 to 2 of 2
  1. #1
    Preethi is offline Member
    Join Date
    Jan 2008
    Posts
    83
    Rep Power
    0

    Default MouseDragged Event

    I could able to drag the rectangle..but the initially placed postion was repainted
    It remain in the same position..can u help me
    Java Code:
    public class mousedrag extends javax.swing.JApplet 
    {
        Imagepanel image;
        @Override
        public void init()
        {
            Container cont = getContentPane();
            cont.setLayout(new BorderLayout());
            setPreferredSize(new Dimension(300, 300));
            JScrollPane scroll = new JScrollPane();
            getContentPane().add(scroll, BorderLayout.CENTER);
            scroll.getViewport().add(new Imagepanel());
            
               
        }
        
         private class Imagepanel extends JPanel implements MouseListener,MouseMotionListener{
         
         public Imagepanel() 
                 { 
                     panel();
                     addMouseMotionListener(this);
                     addMouseListener(this);
                     
                     }
                     protected void paintComponent(Graphics g)
                     {
                         super.paintComponent(g);
                         Graphics2D g2 = (Graphics2D)g;
                         g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
                         //MouseEvent e1 = this.e;
                         //g2.setColor(getBackground());
                         //g2.fillRect(0, 0, getWidth(), getHeight());
                         //g2.setColor(getForeground());
                         g2.setColor(getBackground());   
                         g2.fillRect(0, 0, getWidth(), getHeight()); 
                         g2.setColor(getForeground());  
                         drawdig(g2);
                         drawCalendar(g2);
                     }
                     
                      public void drawdig(Graphics g)
                     {
                         Graphics2D g2 = (Graphics2D)g;
         
                         g2.setPaint(new Color(0,157,250));
                         System.out.println(rectangles.size()+"SIZE");
                         for(Rectangle ret : rectangles)
                         {   
                             g2.draw(ret);
                             g2.fill(ret);
                         
                       }
                    public void mouseDragged(MouseEvent e)
    		      {
    		            Point p = e.getPoint();
    		            Rectangle[] r = rectangles.toArray(new Rectangle[rectangles.size()]);
    		            for(int i=0, j = rectangles.size(); i<j ; i++)
    		            {
    		                Point lastP = (Point)lastPos.get(r[i]);
    		                if(r[i].contains(p))
    		                {
    		                    r[i].translate(e.getX(), e.getY());
    		                    this.repaint();
    		                }
    		                
    		                lastPos.put(r[i], p);
    		            }
    		public void mouseMoved(MouseEvent e) 
    		{
    			lastPos.clear();  
            	}
            }

  2. #2
    Preethi is offline Member
    Join Date
    Jan 2008
    Posts
    83
    Rep Power
    0

Similar Threads

  1. Event
    By nt5515 in forum New To Java
    Replies: 0
    Last Post: 02-15-2008, 05:44 PM
  2. Listener for SWT event
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-08-2008, 10:04 AM
  3. key pressed event
    By kavithas in forum New To Java
    Replies: 7
    Last Post: 12-10-2007, 03:01 PM
  4. Need help with JButton event
    By adlb1300 in forum New To Java
    Replies: 2
    Last Post: 11-19-2007, 02:15 AM
  5. Event Handling
    By luisarca in forum Sun Java Wireless Toolkit
    Replies: 5
    Last Post: 05-07-2007, 07:05 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •