Swing/CustomCursor のバックアップの現在との差分(No.23)
- category: swing folder: CustomCursor title: Cursorオブジェクトの生成 tags: [Cursor, BufferedImage, ImageIcon, JComponent] author: aterai pubdate: 2005-01-24T03:58:31+09:00 description: BufferedImageからカーソルオブジェクトを作成し、これをコンポーネントに設定します。 image:
概要
BufferedImage
からカーソルオブジェクトを作成し、これをコンポーネントに設定します。
Screenshot
Advertisement
サンプルコード
BufferedImage bi = new BufferedImage(32, 32, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = bi.createGraphics();
g2.setPaint(Color.RED);
g2.drawOval(8, 8, 16, 16);
g2.dispose();
button.setCursor(getToolkit().createCustomCursor(bi, new Point(16, 16), "oval"));
View in GitHub: Java, Kotlin解説
上記のサンプルでは、サイズが32x32
のBufferedImage
からToolkit#createCustomCursor(...)
メソッドを使用してホットスポットはそのBufferedImage
の中心に設定したカーソルオブジェクトを作成しています。
- サイズが
32x32px
のBufferedImage
からToolkit#createCustomCursor(...)
メソッドを使用してカーソルオブジェクトを作成- ホットスポットはその
BufferedImage
の中心に設定
- ホットスポットはその
String
:BufferedImage
に文字を描画drawOval
:BufferedImage
に円を描画paintIcon
:BufferedImage
にIcon
を描画