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

    Default Mouse dragg not working

    i tried to drag my rectangle..But my code is not working....
    can anyone help me please...
    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Point;
    import java.awt.Rectangle;
    import java.awt.RenderingHints;
    import java.awt.event.*;
    import java.awt.font.FontRenderContext;
    
    public class Mousedragg extends javax.swing.JApplet  {
       
    
        
        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   
        {
            Imagepanel image;
            JScrollPane scroll = new JScrollPane();
            boolean selected= false;
            boolean selected1= false;
            private boolean canDrag = false;
            int x,y,w,h;
            Rectangle r =  new Rectangle(x,y,w,h);
            
            Imagepanel() 
            {
                setPreferredSize(new Dimension(32910, 32710));
                scroll = new JScrollPane();
                add(scroll);
                addMouseListener(this);
                addMouseMotionListener(this);
                Imagepanel imagepanel ;
                
            }
     
            protected void paintComponent(Graphics g) 
            {
                Graphics2D g2 = (Graphics2D)g;
               x= 40;
               y =20;
               w = 15;
               h =10;
                     
               
                for(int i=0;i<5;i++)
                {
                    g2.fillRect(x,y,w,h);
                    g2.fill(r);
                     x +=30;
                }
            }
     
    
            
            public void mouseClicked(MouseEvent e) 
            {
    
            }
     
            private void imagePaneMouseClicked(MouseEvent e)
            {
                Point  p = e.getPoint();
                if(r.contains(p))
                {
                    if(!selected)
                    {
                        canDrag = true;
                        repaint();
                    }
                    
                }
                
            }
            public void mousePressed(MouseEvent e) 
            {
                            imagePaneMouseClicked(e);
            }
            public void mouseDragged(MouseEvent e) 
            {
                Point p = e.getPoint();
                if(canDrag == true)
                {
                    int x = e.getX();
                    int y = e.getY();
                    int w = (int)r.getHeight();
                    r.setLocation(x,y);
                    repaint();
                }
            }
            public void mouseMoved(MouseEvent e) 
            {  
             
            }
    
            public void mouseReleased(MouseEvent e) {
                canDrag = false;
            }
    
            public void mouseEntered(MouseEvent e) {
            }
    
            public void mouseExited(MouseEvent e) {
            }
            }
        }
    Last edited by Preethi; 02-07-2008 at 11:21 AM.

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

Similar Threads

  1. Mouse Right click option not working in solaris and linux OS
    By dinesh kaushik in forum AWT / Swing
    Replies: 2
    Last Post: 11-21-2007, 04:02 PM
  2. The mouse and the cheese
    By Don Quixote in forum Java 2D
    Replies: 4
    Last Post: 08-15-2007, 10:55 PM
  3. mouse over on JButton
    By gradon in forum Java Applets
    Replies: 1
    Last Post: 08-04-2007, 05:50 AM
  4. Use the mouse position
    By susan in forum Java Applets
    Replies: 1
    Last Post: 07-28-2007, 11:10 PM
  5. Mouse over JButton
    By sandor in forum AWT / Swing
    Replies: 1
    Last Post: 05-17-2007, 09:15 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
  •