hi everyone i am a newbie at this and have a question to ask......i have an image in jpg format, size 32 by 32 and i hope to display it as my mouse cursor image onscreen onscreen....currently the image is messed up although the size is correct.....here's my code which i got from this forum for displaying....
public void init()
{
try {
Mouse.create();
Mouse.enableBuffer();
Image image = (new javax.swing.ImageIcon("data/Mouse.jpg")).getImage();
// Extract the image
BufferedImage tex =
new BufferedImage(
image.getWidth(null),
image.getHeight(null),
BufferedImage.TYPE_3BYTE_BGR);
Graphics2D g = (Graphics2D) tex.getGraphics();
g.drawImage(image, null, null);
g.dispose();
int width = tex.getWidth();
int height = tex.getHeight();
// Put image in memory
ByteBuffer pixels =
ByteBuffer.allocateDirect(4 * tex.getWidth() * tex.getHeight());
byte data[] = (byte[]) tex.getRaster().getDataElements(
0,
0,
tex.getWidth(),
tex.getHeight(),
null);
pixels.clear();
pixels.put(data);
pixels.rewind();
IntBuffer intbuf =
ByteBuffer.allocateDirect(width * height * 4)
.order(ByteOrder.nativeOrder())
.asIntBuffer();
pixels.position(0).limit(width * height * 4 ) ; // RGBA: four pixel components
byte[] inPixel = new byte[4] ;
for(int i = 0 ; i < (width * height) ; i++)
{
int outPixel = 0x00000000 ; // AARRGGBB
inPixel[0] = pixels.get() ; // RR
inPixel[1] = pixels.get() ; // GG
inPixel[2] = pixels.get() ; // BB
inPixel[3] = pixels.get() ; // AA
outPixel |= inPixel[3] << 24 ; // AA
outPixel |= inPixel[0] << 16 ; // RR
outPixel |= inPixel[1] << 8 ; // GG
outPixel |= inPixel[2] ; // BB
intbuf.put(outPixel) ;
}
intbuf.flip() ;
Cursor cursor = new Cursor(width, height, 0, 0, 1, intbuf, null);
Mouse.setNativeCursor(cursor);
} catch (Exception e) {
e.printStackTrace();
}
}
thanks for your help!
You've created a 3byte BGR image, and then munged it into a 4byte RGBA image :) You need to provide an alpha channel for the JPEG somehow. I advise that you use a PNG image for a mouse cursor, because JPEGs can't have alpha channels (easily).
Cas :)
hello and thanks for your quick reply!
i have converted my image into png format and may i also ask how do we provide an alpha channel for the image? also, is it possible to create a 4byte RGBA image directly? thanks again!
to the above question, i tried out the codes and indeed it came out distorted cursor.... BUT i tried to change it to 4byte_ABGR and manage to display the cursor.
Still there are problem.... the image its upside down!! i just wonder how to solve this problem..
Thank you guys...
You need to flip the image, because OpenGL and image formats generally disagree about the coordinate system. Specifically, OpenGL has (0, 0) in the lower left corner, while most image formats has (0, 0) in the upper left corner of the image. Alternatively you can flip your texture coordinates, like this:
u = u;
v = 1 - v;
- elias
ok i have managed to display my mouse cursor by changing to a 4byte rgb image and got an inverted cursor too, but managed to right it by inverting the coordinates thanks everyone for your help!
I was looking at this code, and began wondering if there was a way to set the cursor to a null image to simply make it disappear, so it won't overlay over a software cursor in cases where the mouse isn't grabbed?