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?

4

1 に答える 1

0

Windowsは32X32サイズのカーソルしか受け入れないようです。これが私が問題を解決した方法です:

主なアイデアは、32X32サイズの画像のすべてのピクセルを透明にし、カーソルで見たいピクセルにのみ色を適用することです。この場合、awxhの赤い長方形のカーソルが必要なので、32X32画像の中央で、wxhピクセルを赤に設定し、その画像をカーソルとして設定します。

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"));
}
于 2012-09-24T05:13:38.033 に答える