Hardware cursor (another buffer problem)

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.


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;
    } 
  }


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…

Problem solved once again, this time not by my own doing though. Thanks to Matzon in the irc channel! The pixel format wasn’t correct so the byte[] holding the image data was too small. Also the image needed to be square for it to work.