Lazy library instantiation

This commit is contained in:
Caspian Rychlik-Prince 2003-09-16 15:39:46 +00:00
parent f67cd0e384
commit 00616de78b
3 changed files with 37 additions and 19 deletions

View file

@ -132,10 +132,9 @@ public class Controller {
private static String[] buttonName;
private static final Map buttonMap = new HashMap(8);
static {
initialize();
}
/** Lazy initialization */
private static boolean initialized;
/**
* Controller cannot be constructed.
*/
@ -156,6 +155,7 @@ public class Controller {
buttonMap.put(buttonName[i], new Integer(i));
}
initialized = true;
}
/**
@ -163,6 +163,10 @@ public class Controller {
* @throws Exception if the controller could not be created for any reason
*/
public static void create() throws Exception {
if (!initialized) {
initialize();
}
if (created) {
return;
}

View file

@ -50,9 +50,8 @@ import java.nio.ByteOrder;
public class Cursor {
static {
System.loadLibrary(Sys.getLibraryName());
}
/** Lazy initialization */
private static boolean initialized = false;
/**
* The native handle to the cursor
@ -75,15 +74,23 @@ public class Cursor {
* @throws Exception if the cursor could not be created for any reason
*/
public Cursor(int width, int height, int xHotspot, int yHotspot, int numImages, IntBuffer images, IntBuffer delays) throws Exception {
if (!initialized) {
initialize();
}
assert Mouse.isCreated();
assert width*height*numImages <= images.remaining(): "width*height*numImages > images.remaining()";
assert delays == null || numImages <= delays.remaining(): "delays != null && numImages > delays.remaining()";
assert xHotspot <= width && xHotspot >= 0: "xHotspot > width || xHotspot < 0";
assert yHotspot <= height && yHotspot >= 0: "yHotspot > height || yHotspot < 0";
assert xHotspot < width && xHotspot >= 0: "xHotspot > width || xHotspot < 0";
assert yHotspot < height && yHotspot >= 0: "yHotspot > height || yHotspot < 0";
IntBuffer images_copy = ByteBuffer.allocateDirect(images.remaining()*4).order(ByteOrder.nativeOrder()).asIntBuffer();
flipImages(width, height, numImages, images, images_copy);
nativeHandle = nCreateCursor(width, height, xHotspot, height - yHotspot, numImages, images_copy, 0, delays, delays != null ? delays.position() : 0);
}
private static void initialize() {
System.loadLibrary(Sys.getLibraryName());
initialized = true;
}
private static void flipImages(int width, int height, int numImages, IntBuffer images, IntBuffer images_copy) {
for (int i = 0; i < numImages; i++) {

View file

@ -83,9 +83,8 @@ public class Mouse {
private static String[] buttonName;
private static final Map buttonMap = new HashMap(16);
static {
initialize();
}
/** Lazy initialization */
private static boolean initialized;
/**
* Mouse cannot be constructed.
@ -190,6 +189,8 @@ public class Mouse {
buttonName[i] = "BUTTON" + i;
buttonMap.put(buttonName[i], new Integer(i));
}
initialized = true;
}
/**
@ -203,17 +204,20 @@ public class Mouse {
* @throws Exception if the mouse could not be created for any reason
*/
public static void create() throws Exception {
if (created)
if (!initialized) {
initialize();
}
if (created) {
return;
if (!nCreate())
}
if (!nCreate()) {
throw new Exception("The mouse could not be created.");
}
created = true;
currentCursor = null;
//set mouse buttons
// set mouse buttons
buttons = new boolean[buttonCount];
}
/**
@ -271,7 +275,10 @@ public class Mouse {
*/
public static boolean isButtonDown(int button) {
assert created : "The mouse has not been created.";
return buttons[button];
if (button >= buttonCount)
return false;
else
return buttons[button];
}
/**
@ -280,7 +287,7 @@ public class Mouse {
* @return a String with the button's human readable name in it or null if the button is unnamed
*/
public static String getButtonName(int button) {
if (button < 0 || button >= buttonName.length)
if (button >= buttonName.length)
return null;
else
return buttonName[button];