Here it is. It may contain some Paradroidz specific stuff, but it should help to get you started.
package naroth.util;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import org.lwjgl.input.*;
import com.threed.jpct.*;
import naroth.config.*;
public class MouseMapper extends MouseMotionAdapter implements MouseListener {
public final static int BUTTON1=0;
public final static int BUTTON2=1;
private boolean lwjgl;
private Frame frame=null;
private java.awt.Cursor oldCursor=null;
Robot robot=null;
private int mouseX=0;
private int mouseY=0;
private boolean button1=false;
private boolean button2=false;
private boolean button3=false;
private boolean hidden=false;
public MouseMapper() {
init(null);
}
public MouseMapper(Frame comp) {
try {
robot=new Robot();
} catch (Exception e) {
Logger.log("Error initializing awt.Robot: "+e.getMessage(), Logger.ERROR);
}
init(comp);
}
public void mouseMoved(MouseEvent e) {
mouseX=e.getX()-frame.getInsets().left;
mouseY=e.getY()-frame.getInsets().top;
}
public void mouseDragged(MouseEvent e) {
mouseX=e.getX()-frame.getInsets().left;
mouseY=e.getY()-frame.getInsets().top;
}
public void mouseClicked(MouseEvent e) {}
public void mousePressed(MouseEvent e) {
if (e.getButton()==MouseEvent.BUTTON1) {
button1=true;
}
if (e.getButton()==MouseEvent.BUTTON2) {
button2=true;
}
if (e.getButton()==MouseEvent.BUTTON3) {
button3=true;
}
}
public void mouseReleased(MouseEvent e) {
if (e.getButton()==MouseEvent.BUTTON1) {
button1=false;
}
if (e.getButton()==MouseEvent.BUTTON2) {
button2=false;
}
if (e.getButton()==MouseEvent.BUTTON3) {
button3=false;
}
}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void center() {
if (frame!=null) {
int x=frame.getX();
int y=frame.getY();
int width=frame.getWidth();
int height=frame.getHeight();
width=width>>1;
height=height>>1;
x+=width;
y+=height;
robot.mouseMove(x,y);
} else {
// Tu nix...dies ist schon automatisch durch das Mouse.setGrabbed() erledigt.
}
}
public void hide() {
if (frame!=null) {
oldCursor=frame.getCursor();
int[] pixels=new int[16*16];
Image image=Toolkit.getDefaultToolkit().createImage(new MemoryImageSource(16, 16, pixels, 0, 16));
java.awt.Cursor transparentCursor=Toolkit.getDefaultToolkit().createCustomCursor(image, new Point(0, 0),"hidden");
frame.setCursor(transparentCursor);
} else {
Mouse.setGrabbed(true);
}
hidden=true;
}
public void show() {
if (frame!=null) {
if (oldCursor!=null) {
frame.setCursor(oldCursor);
oldCursor=null;
}
} else {
Mouse.setGrabbed(false);
}
hidden=false;
}
public boolean isVisible() {
return !hidden;
}
public void destroy() {
if (frame!=null) {
frame.removeMouseMotionListener(this);
frame.removeMouseListener(this);
} else {
if (Mouse.isCreated()) {
Mouse.destroy();
}
}
show();
}
public boolean buttonDown(int button) {
if (!lwjgl) {
if (button==BUTTON1) return button1;
if (button==BUTTON2) return button2|button3;
}
return Mouse.isButtonDown(button);
}
public int getMouseX() {
if (!lwjgl) {
return mouseX;
} else {
return Mouse.getX();
}
}
public int getMouseY() {
if (!lwjgl) {
return mouseY;
} else {
return Configuration.height-Mouse.getY();
}
}
public int getDeltaX() {
if (!lwjgl) {
int mouseXCenter=(frame.getWidth()>>1)-frame.getInsets().left; // Korrektur, da mouseMoved und mouseDragged dies auch abziehen
return mouseX-mouseXCenter;
} else {
if (Mouse.isGrabbed()) {
return Mouse.getDX();
} else {
return 0;
}
}
}
public int getDeltaY() {
if (!lwjgl) {
int mouseYCenter=(frame.getHeight()>>1)-frame.getInsets().top; // Korrektur, da mouseMoved und mouseDragged dies auch abziehen
return mouseY-mouseYCenter;
} else {
if (Mouse.isGrabbed()) {
return Mouse.getDY();
} else {
return 0;
}
}
}
private void init(Frame comp) {
if (comp!=null) {
this.frame=comp;
comp.addMouseMotionListener(this);
comp.addMouseListener(this);
lwjgl=false;
center();
} else {
lwjgl=true;
try {
if (!Mouse.isCreated()) {
Mouse.create();
}
} catch (Exception e) {
//e.printStackTrace();
Logger.log("Couldn't create LWJGL-mouse - initialize the OpenGL renderer first!", Logger.WARNING);
}
}
}
}