Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9235775
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T07:07:55+00:00 2026-06-18T07:07:55+00:00

So I found this code here , it works, it just causes quite a

  • 0

So I found this code here, it works, it just causes quite a lot of juddering – (the faster you drag the more the image shakes) – in the image when it’s dragged. OP said it wasn’t optimized and since it’s a dead post I thought I’d see if anyone here could help! I’ve also tried the code from stack, but I couldn’t get it to do anything. If anyone has any suggestions for this code, or a better solution I’d love to hear it!

OF note, my Jpanel that needs to be dragged around (paintComponent drawn object) is inside a scrollpane!

   //initial reference point  
   private Point mouseLocation;  
   public void mousePressed(MouseEvent evt){  
      mouseLocation = evt.getPoint();  
   }  
   public void mouseDragged(MouseEvent evt){  
      //current mouse location  
      Point newLoc = evt.getPoint();  
      //deltas  
      int deltaX = newLoc.x-mouseLocation.x;  
      int deltaY = newLoc.y-mouseLocation.y;  
      p.setLocation(p.getX()+deltaX,p.getY()+deltaY);  
      //move the reference point to the current location  
      this.mouseLocation = newLoc;  
   }  

Here is a sample program that shows the juddering!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T07:07:56+00:00Added an answer on June 18, 2026 at 7:07 am

    So I found this code here, it works, it just causes quite a lot of juddering

    Then I would say that it does not work. And if you read the post carefuly, the OP also states that it does not work.

    Moreover, the button does not strictly follows the mouse, so it’s just horrible in my opinion.

    Now, two things to consider:

    1. You are using evt.getPoint(): the value of that method is relative to the JButton. As you move the JButton around, you cannot compare the value of that method with the previous one (since your button is moving). One simple solution is to convert those points relatively to a fixed panel, for example the parent, which is not moving. Tadaam: it works smoothly now and the button follows your mouse perfectly.
    2. When you use LayoutManager’s, you can’t call setLocation (nor setBounds or setSize()) because this is the job of LayoutManager’s, and as soon as they will re-layout your container, the button will be set back to its original location (which I am guessing you don’t want). There are several ways to solve this, but usually, the simplest one is to use absolute positionning (ie, set the layout to null). In the end, this means that you have to perform yourself whatever the LayoutManager was previously doing.

    Here is a small demo (which is flawed but demonstrates basic princiles):

    import java.awt.Component;
    import java.awt.Point;
    
    import javax.swing.SwingUtilities;
    
    /**
     * 
     * @author Stuart.Bradley
     */
    public class NewJFrame extends javax.swing.JFrame {
        private Point mouseLocation;
    
        /**
         * Creates new form NewJFrame
         */
        public NewJFrame() {
            initComponents();
        }
    
        /**
         * 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() {
            jButton1 = new javax.swing.JButton();
    
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    
            jButton1.setText("jButton1");
            jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
                @Override
                public void mousePressed(java.awt.event.MouseEvent evt) {
                    jButton1MousePressed(evt);
                }
            });
            jButton1.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
                @Override
                public void mouseDragged(java.awt.event.MouseEvent evt) {
                    jButton1MouseDragged(evt);
                }
            });
            setLayout(null);
            jButton1.setSize(jButton1.getPreferredSize());
            add(jButton1);
            setSize(300, 300);
        }// </editor-fold>
    
        private void jButton1MouseDragged(java.awt.event.MouseEvent evt) {
            // current mouse location
            Point newLoc = SwingUtilities.convertPoint(evt.getComponent(), evt.getPoint(), jButton1.getParent());
            // deltas
            int deltaX = newLoc.x - mouseLocation.x;
            int deltaY = newLoc.y - mouseLocation.y;
            jButton1.setLocation(jButton1.getX() + deltaX, jButton1.getY() + deltaY);
            // move the reference point to the current location
            this.mouseLocation = newLoc; // TODO add your handling code here:
        }
    
        private void jButton1MousePressed(java.awt.event.MouseEvent evt) {
            mouseLocation = SwingUtilities.convertPoint(evt.getComponent(), evt.getPoint(), jButton1.getParent()); // TODO add your
        }
    
        /**
         * @param args
         *            the command line arguments
         */
        public static void main(String args[]) {
            /* Set the Nimbus look and feel */
            // <editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
            /*
             * If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. For details see
             * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
             */
            try {
                for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                    if ("Nimbus".equals(info.getName())) {
                        javax.swing.UIManager.setLookAndFeel(info.getClassName());
                        break;
                    }
                }
            } catch (ClassNotFoundException ex) {
                java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (InstantiationException ex) {
                java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (javax.swing.UnsupportedLookAndFeelException ex) {
                java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            }
            // </editor-fold>
    
            /* Create and display the form */
            java.awt.EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    new NewJFrame().setVisible(true);
                }
            });
        }
    
        // Variables declaration - do not modify
        private javax.swing.JButton jButton1;
        // End of variables declaration
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I cannot get this to work, here is code that I found in another
I found this code here class Usable; class Usable_lock { friend class Usable; private:
I found this code from here: http://www.cssportal.com/form-elements/text-box.htm But the problem is you can still
Here I found this code: import java.awt.*; import javax.swing.*; public class FunWithPanels extends JFrame
I've been here and accordingly found out that this piece of code here var
Here in Apple's sample code I found this type of persistence storage. Can anyone
Based on example found here but I guess I'm not understanding it. This works
I found this code on the internetz, it checks the current page url; function
i found this code -(NSString *) genRandStringLength: (int) len { NSMutableString *randomString = [NSMutableString
I found this code snippet to retrieve the title from a YouTube video and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.