Results 1 to 11 of 11

Thread: click and drag

  1. #1
    PRW56 is offline Member
    Join Date
    Mar 2012
    Posts
    75
    Rep Power
    0

    Default click and drag

    Well I was attempting to do either a click and drag or click and the label follows the mouse pointer. At the moment I was trying to get the basics of this down, I realized I had a very big gap in my plan for the code. When I try to get the location of the event, from the inner class (i think that's what its called) for the TimerTask, it doesn't recognize the variable e (for the MouseEvent). I guess that means that its outside the scope of e, but I can't think of a way to get around this or an alternative. Any suggestions? Oh yes, and Fram is my window adapter, so the window is close-able.

    here is code

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.JLabel;
    import javax.swing.ImageIcon;
    import java.util.*;
    public class GFrame extends Fram implements MouseListener
    {
        JLabel p = new JLabel();
        boolean da = true;
        Timer t = new Timer();
        int x=0,q;
        MouseEvent e;
        GFrame()
        {
            super("GFrame");
            setSize(500,500);
            addMouseListener(this);
            setLayout(null);
            add(p);
            //p.setIcon(new ImageIcon("J:\\programmin\\WhiteBook\\pics\\bolt.jpg"));
            p.setBounds(250,250,50,50);
            setVisible(true);
        }
        public void mousePressed(MouseEvent e) {}
        public void mouseClicked(MouseEvent e) {}
        public void mouseEntered(MouseEvent e) 
        {
            t.schedule(new TimerTask()
            {
                public void run()
                {
                    ++x;
                    p.setText(e.getX()+"");
                    if(x>100)
                    {
                        x=0;
                        t.cancel();
                    }
                }
            },1000,100);
        }
        public void mouseExited(MouseEvent e) {}
        public void mouseReleased(MouseEvent e) {}
        public static void main(String[] args)
        {
            GFrame a = new GFrame();
        }
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: click and drag

    This looks to be a bad mix of Swing, AWT and non-Swing code. My first suggestions are to do this in all Swing code, meaning no Frame, but instead JFrame. Next, you shouldn't use a java.util.Timer with Swing code but rather a javax.swing.Timer or Swing Timer since it is built to optimally work with Swing applications. But looking at your needs, I *think* that you don't even need to use a Timer but instead just a MouseListener, though I'm not 100% sure. Please clarify your requirements a little bit.

  3. #3
    PRW56 is offline Member
    Join Date
    Mar 2012
    Posts
    75
    Rep Power
    0

    Default Re: click and drag

    First, whenever I use JFrame well... it does this: it takes a screenshot of whatever is behind it and becomes unusable and unresponsive showing no components within. Next, the swing timer requires an action listener in its constructor, and I have no clue what it is for or really how to use it. Last, I thought about using mouse listener alone to do this (and it would probably be much more simpler to do so), but when I try to think of a way to do it nothing really works. Like i think maybe use a boolean that is changed by the clicked and released methods, but if you do that you would need some other method that actually does the moving right? if you did so you wouldn't be able to access the position of the event from there. I'm sorry I am probably being too vague, but if you have an idea for that that you think would work pls tell me.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: click and drag

    Quote Originally Posted by PRW56 View Post
    First, whenever I use JFrame well... it does this: it takes a screenshot of whatever is behind it and becomes unusable and unresponsive showing no components within.
    This is because your code does not obey Swing's threading rules (due to your using a java.util.Timer), and you've stepped on and frozen the Swing event thread. So you're fixing the wrong thing by using a java.awt.Frame. Again, use a JFrame, and fix your threading problems by not using a util Timer with a Swing application.


    Next, the swing timer requires an action listener in its constructor, and I have no clue what it is for or really how to use it.
    There are tutorials that can fix that.


    Last, I thought about using mouse listener alone to do this (and it would probably be much more simpler to do so), but when I try to think of a way to do it nothing really works. Like i think maybe use a boolean that is changed by the clicked and released methods, but if you do that you would need some other method that actually does the moving right? if you did so you wouldn't be able to access the position of the event from there. I'm sorry I am probably being too vague, but if you have an idea for that that you think would work pls tell me.
    Again, please explain the behavior that you're trying to have occur here because I'm thinking that there really should be no need to use any type of Timer here.

  5. #5
    PRW56 is offline Member
    Join Date
    Mar 2012
    Posts
    75
    Rep Power
    0

    Default Re: click and drag

    The only tutorial I have seen, from the site that most moderators as well as my teacher direct me to, has an example that doesn't make sense to me. I just had a thought about it, is the actionPerformed method that you normally write for the ActionListener interface actually used here as... the TimerTask for the swing timer, as in the thing that takes place after delay? However the bigger reason I haven't used swing timers is because the util version has a simple constructor for setting up recurring events, but I am unsure how to use the methods in swing to do such a thing... Also by click and drag i mean it in the simplest sense, as in if you click on the (in this case) JLabel it then follows the pointer until the mouse is released. The problem I was thinking about though is if you do such a thing you need some way to tell if the mouse has been released, but no matter what way I think of it you would already be in a while loop in some other method and wouldn't be able to do so.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: click and drag

    Again, if you want to click and drag a JLabel, there's no need for a timer of any kind. Simply use a MouseListener and MouseMotionListener (combined as a MouseAdapter). An example can be found here: questions-about-events-differents-layers

  7. #7
    PRW56 is offline Member
    Join Date
    Mar 2012
    Posts
    75
    Rep Power
    0

    Default Re: click and drag

    i didn't know the MouseMotionListener existed, but when i try using it in this code, it looks like every time the object moves it is destroyed and created, causing a huge ammount of lag and flickering

    here is code
    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.JLabel;
    import javax.swing.ImageIcon;
    import java.util.*;
    public class GFrame extends Fram implements MouseListener, MouseMotionListener
    {
        JLabel p = new JLabel();
        boolean da = false,dom;
        Timer t = new Timer();
        int x=0,q;
        MouseEvent e;
        GFrame()
        {
            super("GFrame");
            setSize(500,500);
            addMouseListener(this);
            setLayout(null);
            add(p);
            p.setIcon(new ImageIcon("E:\\programmin\\WhiteBook\\pics\\bolt.jpg"));
            p.setBounds(250,250,50,50);
            p.addMouseMotionListener(this);
            setVisible(true);
        }
        public void mousePressed(MouseEvent e) {}
        public void mouseClicked(MouseEvent e)
        {
            /*if(e.getX()>p.getX()&&e.getY()>p.getY()&&e.getX()<(p.getX()+p.getWidth())&&e.getY()<(p.getY()+p.getHeight()))
            {
                System.out.println("hey");
                da=true;
            }
            if(dom)
            {
                da=false;
                dom=false;
            }*/
        }
        public void mouseEntered(MouseEvent e) 
        {
            /*t.schedule(new TimerTask()
            {
                public void run()
                {
                    ++x;
                    p.setText(e.getX()+"");
                    if(x>100)
                    {
                        x=0;
                        t.cancel();
                    }
                }
            },1000,100); */
        }
        public void mouseExited(MouseEvent e) {}
        public void mouseReleased(MouseEvent e) {}
        public void mouseMoved(MouseEvent e) {}
        public void mouseDragged(MouseEvent e)
        {
            //System.out.println("hey");
            //if(da)
            //{
                p.setLocation(e.getX(),e.getY());
                dom=true;
            //}
        }
        public static void main(String[] args)
        {
            GFrame a = new GFrame();
        }
    }

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,397
    Rep Power
    25

    Default Re: click and drag

    I assume your call to setLocation() is not doing what you want. Is that the problem?

    To see what your code is doing print out the values it uses in setLocation and then read the API doc for the MouseEvent methods it calls.
    Last edited by Norm; 04-10-2012 at 08:44 PM.
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    PRW56 is offline Member
    Join Date
    Mar 2012
    Posts
    75
    Rep Power
    0

    Default Re: click and drag

    Easiest way to describe it is to see it for yourself, it looks like every time it moves the object is being recreated/repainted/idk causing massive lag/flickering. I have no idea how to fix it.

  10. #10
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,236
    Rep Power
    19

    Default Re: click and drag

    Do you need Norm to repeat the advice he has already given? You don't seem to have followed that advice at all.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,397
    Rep Power
    25

    Default Re: click and drag

    Did you read the API doc for the MouseEvent class?
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Replies: 2
    Last Post: 06-22-2011, 01:19 PM
  2. Need help with drag and drop
    By NixasMuraki in forum New To Java
    Replies: 2
    Last Post: 06-07-2011, 01:55 AM
  3. drag and drop with click function
    By zwanz in forum AWT / Swing
    Replies: 1
    Last Post: 05-18-2010, 09:56 PM
  4. Drag and drop
    By thayalan in forum AWT / Swing
    Replies: 1
    Last Post: 02-16-2009, 03:04 PM
  5. Replies: 21
    Last Post: 08-01-2008, 11:13 AM

Tags for this Thread

Posting Permissions

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