This site requires JavaScript, please enable it in your browser!
Greenfoot back
F-L-Undead
F-L-Undead wrote ...

2011/6/24

Scrolling

F-L-Undead F-L-Undead

2011/6/24

#
How Can you check, if the mouse has been scrolled?
Busch2207 Busch2207

2011/6/24

#
I think that might be the shortest version:
import greenfoot.core.WorldHandler;
import java.awt.event.*;

public class world  extends World
{
       ScrollingListener scroll=new ScrollingListener();
       int Scroll = 0;
    
    public world()
    {
        super(500, 400, 1,false);
        WorldHandler.getInstance().getWorldCanvas().addMouseWheelListener(scroll);
    }

    public void act()
    {
        Scroll+=scroll.getScroll();
    }
}

class ScrollingListener implements MouseWheelListener
{
    int scroll = 0;
    
    public void mouseWheelMoved(MouseWheelEvent MWE)
    {
        scroll+=MWE.getWheelRotation();  
        MWE.consume();
    }
    
    public int getScroll()
    {
        int a=scroll;
        scroll=0;
        return a;
    }
}
scroll.getScroll() returns a Number. If you havent scrolled it returns 0. This can be written in every class.
danpost danpost

2011/6/25

#
Curious: Would getScroll() return a negative number when the wheel is scrolled up instead of down?
Busch2207 Busch2207

2011/6/25

#
It returns a negativ number if you scrolled up and a positive by scrolling down... If you called this Method once an act, it will return 0, by the second call int he same act. Thats because of the frame:
 int a=scroll;  
 scroll=0;  
 return a;  
But you need this frame, otherwise it always returns every round a Higher Number... So it may will help if you safe the scroll in an extra int Number... Here it will print out 0, if you scrolled:
if(scroll.getScroll()!=0)
{
System.out.println(scroll.getScroll());
}
Here it prints out, how many you scrolled, if you scrolled:
int s=scroll.getScroll();
if(s!=0)
{
System.out.println(s);
}
F-L-Undead F-L-Undead

2011/6/25

#
Thank you very much!
danpost danpost

2011/6/25

#
Thanks for your input Busch2207. Therefore from what I understand if scrolling was to control something that had limits, I could do something like this:
private int scrollValue;
private int scrollHiLimit = 15;
private int scrollLoLimit = 0;
//
public void act()
{
    scrollValue += scroll.getScroll();
    if (scrollValue > scrollHiLimit) { scrollValue = scrollHiLimit; }
    if (scrollValue < scrollLoLimit) { scrollValue = scrollLoLimit; }
}
And I will always be within limits!
davmac davmac

2011/6/27

#
It's worth mentioning that the technique given here uses private API methods, is not supported, and may not work at all in future versions of Greenfoot. Also: for thread-safety you should declare both methods in ScrollingListener to be synchronized:
class ScrollingListener implements MouseWheelListener
{
    int scroll = 0;
    
    public synchronized void mouseWheelMoved(MouseWheelEvent MWE)
    {
        scroll+=MWE.getWheelRotation();  
        MWE.consume();
    }
    
    public synchronized int getScroll()
    {
        int a=scroll;
        scroll=0;
        return a;
    }
}
Finally, there is a small problem in that the listener added never gets removed. This probably won't be a practical concern though.
F-L-Undead F-L-Undead

2011/6/27

#
where do you need synchronized for? it although works without synchronized...
Busch2207 Busch2207

2011/6/27

#
Well, you don't need it, but it might be better, if you write it! And I think, that's not much more work, is it? ;)
davmac davmac

2011/6/27

#
F-L-Undead, let's put it this way: if you don't use "synchronized" on those methods, then you're messing with forces you don't understand :) It will probably work most of the time, but sometimes, just occasionally, it will go mysteriously wrong... unless you use "synchronized". You don't have to understand what it does, and you don't have to use it in your own code; just in the two methods in ScrollingListener.
You need to login to post a reply.