This site requires JavaScript, please enable it in your browser!
Greenfoot back
Gevater_Tod4711
Gevater_Tod4711 wrote ...

2013/7/31

Subclass of greenfoot.MouseInfo

Gevater_Tod4711 Gevater_Tod4711

2013/7/31

#
Hi all, I'm currently programming on a full screen mode for greenfoot and therefore I want to make a subclass of greenfoot.MouseInfo. But the constructor in the MouseInfo class seems to have protected access. Does anyone know a way I could write a subclass of MouseInfo? Or can anyone of the greenfoot team tell me why the constructor has protected access?
erdelf erdelf

2013/7/31

#
why dont u just remove the protection, if u need it?
Gevater_Tod4711 Gevater_Tod4711

2013/7/31

#
How should I do this? The MouseInfo class is part of the greenfoot API. I have no access on this classes so I can't change it.
erdelf erdelf

2013/7/31

#
dl the source, remove protection, recompile
iau iau

2013/7/31

#
If you do as erdelf suggests, your game will be incompatible with the Gallery and anyone else's installation of (standard) Greenfoot. Access to non-public methods is faulted by Java whenever it loads a class.
davmac davmac

2013/7/31

#
There's no particular reason why the constructor for MouseInfo is package private, but it was never intended that anyone would subclass MouseInfo and I don't understand why you need to do so?
Gevater_Tod4711 Gevater_Tod4711

2013/7/31

#
Well I don't realy NEED to. But for my fullscreen demo it would be much easyer for everyone who wants to use it. To explain it: I had some problems using the greenfoot mouse and key supports because when my fullscreen frame runns the greenfoot frame is not active and doesn't react on key or mouse events. So I had to implement key and mouse listeners (and a class called FullScreenMouseInfo) myselves. Now I wanted to make FullScreenMouseInfo a subclass of greenfoot.MouseInfo because when you use the fullscreen mode you can't call Greenfoot.getMouseInfo but FullScreenWindow.getMouseInfo(). If now FullScreenMouseInfo would be a subclass of greenfoot.MouseInfo my method could return Greenfoot.getMouseInfo if the fullscreenmode is disabled (which is sometimes better for programming the game). So if I could make my class a subclass of greenfoot.MouseInfo I could make it like this:
MouseInfo mouse;

public void act() {
    mouse = FullScreenWindow.getMouseInfo();//If the fullscreen mode is disabled Greenfoot.getMouseInfo is returned;
    //...
}
If I don't declare my class a subclass of MouseInfo I always have to use two different objects and check which one is null ... So it wouldn't be totally neccessary to make it a subclass of MouseInfo but it would be much easyer.
davmac davmac

2013/7/31

#
I'd suggest making your own class (eg MyMouseInfo) which doesn't subclass MouseInfo but mirrors its functionality, and copying the values from MouseInfo (if that is available) into it, or setting the values specifically if in full screen mode.
Gevater_Tod4711 Gevater_Tod4711

2013/7/31

#
My class doesn't ony copy the values from MouseInfo. The funktion is majorly the same (I override all the MouseInfo methods) but I had to use this class because the greenfoot.MouseInfo didn't work in my fullscreen frame. The code I used is:
import java.awt.event.MouseEvent;
import java.awt.Point;
import java.awt.MouseInfo;

/**
 * Write a description of class FullScreenMouseInfo here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class FullScreenMouseInfo
{
    private MouseEvent mouseEvent;
    private Point mousePoint;
    
    public FullScreenMouseInfo() {
        this.mouseEvent = mouseEvent;
        mousePoint = MouseInfo.getPointerInfo().getLocation();
    }
    
    public int getButton() {
        if (mouseEvent != null) {
            return mouseEvent.getButton();
        }
        return -1;
    }
    
    public int getClickCount() {
        if (mouseEvent != null) {
            return mouseEvent.getClickCount();
        }
        return -1;
    }
    
    public int getX() {
        return (int) (mousePoint.getX() * FullScreenWorld.WORLD_WIDTH / FullScreenWindow.FRAME_WIDTH);
    }
    
    public int getY() {
        return (int) (mousePoint.getY() * FullScreenWorld.WORLD_HEIGHT / FullScreenWindow.FRAME_HEIGHT);
    }
    
    public Point getMousePoint() {
        return new Point(getX(), getY());
    }
    
    public String toString() {
        return mouseEvent.toString();
    }
}
and:
import java.awt.event.MouseEvent;

public class GreenfootMouseListener implements java.awt.event.MouseListener {

	private FullScreenWindow window;
	
	public GreenfootMouseListener(FullScreenWindow window) {
		this.window = window;
	}
	
	@Override
	public void mouseClicked(MouseEvent e) {
		window.setMouseClicked(true);
	}

	@Override
	public void mouseEntered(MouseEvent e) {
		window.setMouseDragged(true);
	}

	@Override
	public void mouseExited(MouseEvent e) {
		window.setMouseDragged(false);
		window.setMouseDragEnded(true);
	}

	@Override
	public void mousePressed(MouseEvent e) {
		window.setMousePressed(true);
	}

	@Override
	public void mouseReleased(MouseEvent e) {
		window.setMousePressed(false);
	}
}
Like I said it's not realy neccessary to make it a subclass so if it isn't possible (without changing the greenfoot source code) I will heave it like it is now. That will also work. It's just a formal thing which would make the support easyer.
You need to login to post a reply.