Results 1 to 11 of 11
Thread: click and drag
- 04-08-2012, 05:02 AM #1
Member
- Join Date
- Mar 2012
- Posts
- 70
- Rep Power
- 0
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(); } }
-
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.
- 04-08-2012, 05:30 AM #3
Member
- Join Date
- Mar 2012
- Posts
- 70
- Rep Power
- 0
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.
-
Re: click and drag
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.
There are tutorials that can fix that.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.
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.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.
- 04-08-2012, 07:27 AM #5
Member
- Join Date
- Mar 2012
- Posts
- 70
- Rep Power
- 0
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.
-
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
- 04-10-2012, 07:56 PM #7
Member
- Join Date
- Mar 2012
- Posts
- 70
- Rep Power
- 0
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(); } }
- 04-10-2012, 08:35 PM #8
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.
- 04-14-2012, 09:54 AM #9
Member
- Join Date
- Mar 2012
- Posts
- 70
- Rep Power
- 0
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.
- 04-14-2012, 11:50 AM #10
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.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 04-14-2012, 12:14 PM #11
Similar Threads
-
Executing .class files on Windows with a double click or right click
By Norm in forum Forum LobbyReplies: 2Last Post: 06-22-2011, 01:19 PM -
Need help with drag and drop
By NixasMuraki in forum New To JavaReplies: 2Last Post: 06-07-2011, 01:55 AM -
drag and drop with click function
By zwanz in forum AWT / SwingReplies: 1Last Post: 05-18-2010, 09:56 PM -
Drag and drop
By thayalan in forum AWT / SwingReplies: 1Last Post: 02-16-2009, 03:04 PM -
i click on it,then first time error page comes,second time click then product page co
By 82rathi.angara in forum New To JavaReplies: 21Last Post: 08-01-2008, 11:13 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks