Guest User

cursor-sandbox

a guest
Aug 6th, 2020
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.15 KB | None | 0 0
  1. import java.awt.Cursor;
  2. import java.awt.Point;
  3. import java.awt.Toolkit;
  4. import java.awt.image.BufferedImage;
  5.  
  6. import javax.swing.ImageIcon;
  7. import javax.swing.JFrame;
  8. import javax.swing.JLabel;
  9.  
  10. import com.sun.jna.Pointer;
  11.  
  12. /**
  13.  * Taken from
  14.  * https://stackoverflow.com/questions/739870/extract-cursor-image-in-java/740528
  15.  */
  16. class CursorExtractor {
  17.  
  18.     private static void draw(BufferedImage image, int cursor,
  19.                              int diFlags) {
  20.         int width = image.getWidth();
  21.         int height = image.getHeight();
  22.  
  23.         User32 user32 = User32.INSTANCE;
  24.         Gdi32 gdi32 = Gdi32.INSTANCE;
  25.  
  26.         Pointer hIcon = user32
  27.                 .LoadCursorW(Pointer.NULL, cursor);
  28.         Pointer hdc = gdi32.CreateCompatibleDC(Pointer.NULL);
  29.         Pointer bitmap = gdi32.CreateCompatibleBitmap(hdc,
  30.                 width, height);
  31.  
  32.         gdi32.SelectObject(hdc, bitmap);
  33.         user32.DrawIconEx(hdc, 0, 0, hIcon, 0, 0, 0,
  34.                 Pointer.NULL, diFlags);
  35.  
  36.         for (int x = 0; x < width; x++) {
  37.             for (int y = 0; y < width; y++) {
  38.                 int rgb = gdi32.GetPixel(hdc, x, y);
  39.                 image.setRGB(x, y, rgb);
  40.             }
  41.         }
  42.  
  43.         gdi32.DeleteObject(bitmap);
  44.         gdi32.DeleteDC(hdc);
  45.     }
  46.  
  47.  
  48.     public static void main(String[] args) {
  49.  
  50.         BufferedImage image = getCursor(User32.IDC_ARROW);
  51.  
  52.         JLabel icon = new JLabel();
  53.         icon.setIcon(new ImageIcon(image));
  54.  
  55.         JFrame frame = new JFrame();
  56.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  57.         frame.setContentPane(icon);
  58.         frame.pack();
  59.         frame.setVisible(true);
  60.  
  61.         Toolkit toolkit = Toolkit.getDefaultToolkit();
  62.         Point pointerPos = new Point(1, 1);
  63.         Cursor c = toolkit.createCustomCursor(image, pointerPos, "cursorname");
  64.         frame.setCursor(c);
  65.     }
  66.  
  67.  
  68.     public static BufferedImage getCursor(int cursorType) {
  69.         final int width = User32.INSTANCE.GetSystemMetrics(User32.SM_CXCURSOR);
  70.         final int height = User32.INSTANCE.GetSystemMetrics(User32.SM_CYCURSOR);
  71.  
  72.         System.out.println(String.format("Cursor height: %d, Cursor weight: %d", height, width));
  73.  
  74.         BufferedImage image = new BufferedImage(width, height,
  75.                 BufferedImage.TYPE_INT_ARGB);
  76.         draw(image, cursorType, User32.DI_NORMAL);
  77.         BufferedImage mask = new BufferedImage(width, height,
  78.                 BufferedImage.TYPE_INT_RGB);
  79.         draw(mask, cursorType, User32.DI_MASK);
  80.         applyMask(image, mask);
  81.         return image;
  82.     }
  83.  
  84.     private static void applyMask(BufferedImage image,
  85.                                   BufferedImage mask) {
  86.         int width = image.getWidth();
  87.         int height = image.getHeight();
  88.         for (int x = 0; x < width; x++) {
  89.             for (int y = 0; y < height; y++) {
  90.                 int masked = mask.getRGB(x, y);
  91.                 if ((masked & 0x00FFFFFF) == 0) {
  92.                     int rgb = image.getRGB(x, y);
  93.                     rgb = 0xFF000000 | rgb;
  94.                     image.setRGB(x, y, rgb);
  95.                 }
  96.             }
  97.         }
  98.     }
  99.  
  100. }
Add Comment
Please, Sign In to add comment