0

I created a cursor using:

    BufferedImage im=null;
    try {
        im = ImageIO.read(new File("images/cursor1.jpg"));
    } catch (IOException ex) {
        Logger.getLogger(SRGView.class.getName()).log(Level.SEVERE, null, ex);
    }
    Cursor cursor = getToolkit().createCustomCursor(im, new Point(1,1), "s");
    this.setCursor(cursor);

The cursor1.jpg is 5X5 (in pixels). However, when it is displayed on the screen, it is much larger. I would like to make cursors of size 1X1, 5X5, 10X10. I would rather prefer creating the image dynamically than read the image file. i.e.

for (int x = 0; x < w; x++) {
        for (int y = 0; y < h; y++) {
        im.setRGB(x, y, new Color(255, 0, 0).getRGB());
  }
    }

The above code would create a red image "im" of width, w and height, h and I would like to use that as my cursor.

How to do it?

tshepang
  • 12,111
  • 21
  • 91
  • 136
coolscitist
  • 3,317
  • 8
  • 42
  • 59

1 Answers1

0

It seems like windows only accepts 32X32 sized cursors. So this is how I solved the problem:

The main idea is to make every pixel in the 32X32 sized image transparent and apply color only to those pixels u want to see in the cursor. In this case, I want a w x h red rectangular cursor so, at the center of the 32X32 image, I set w x h pixels as red and set that image as the cursor.

public void setCursorSize(int w, int h) {
    this.cw = w;
    this.ch = h;
    if (w > 32) {
        w = 32;
    }
    if (h > 32) {
        h = 32;
    }
    Color tc = new Color(0, 0, 0, 0);
    BufferedImage im = new BufferedImage(32, 32, BufferedImage.TYPE_4BYTE_ABGR);//cursor size is 32X32
    for (int x = 0; x < 32; x++) {
        for (int y = 0; y < 32; y++) {
            im.setRGB(x, y, tc.getRGB());
        }
    }
    for (int x = 16 - w / 2; x <= 16 + w / 2; x++) {
        for (int y = 16 - h / 2; y <= 16 + h / 2; y++) {
            im.setRGB(x, y, new Color(255, 0, 0).getRGB());
        }
    }
    this.setCursor(getToolkit().createCustomCursor(im, new Point(16, 16), "c1"));
}
coolscitist
  • 3,317
  • 8
  • 42
  • 59