I'm writing a method that I will be able to easily re-use for making a hardware cursor (with the option of it being animated) and I've run into a problem with the image data I'm giving it. There are two methods involved with this problem, the first is the method for creating the cursor with a single image, the second is a method for converting an image to bytes.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| public static Cursor createHWCursor(BufferedImage image, int xHotSpot, int yHotSpot) { IntBuffer imageBytes = ImageUtils.convertImageToBytes(image).asIntBuffer(); IntBuffer bufferedDelays = ByteBuffer.allocateDirect(4).order(ByteOrder.nativeOrder()).asIntBuffer(); bufferedDelays.put(0); bufferedDelays.flip(); try { return new Cursor(image.getWidth(), image.getHeight(), xHotSpot, yHotSpot, 1, imageBytes, bufferedDelays); } catch (LWJGLException ex) { ex.printStackTrace(); return null; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13
| public static ByteBuffer convertImageToBytes(BufferedImage bufferedImage) { ByteBuffer imageBuffer = null; byte[] data = ((DataBufferByte) bufferedImage.getRaster().getDataBuffer()).getData();
imageBuffer = ByteBuffer.allocateDirect(data.length); imageBuffer.order(ByteOrder.nativeOrder()); imageBuffer.put(data, 0, data.length); imageBuffer.flip(); return imageBuffer; } |
I am getting this error:
java.lang.IllegalArgumentException: Number of remaining buffer elements is 825, must be at least 3300
at this line:
return new Cursor(image.getWidth(), image.getHeight(), xHotSpot, yHotSpot, 1, imageBytes, bufferedDelays);
any ideas? I'm lost...