• 追加された行はこの色です。
  • 削除された行はこの色です。
#navi(../)
*Cursorオブジェクトの生成 [#l60ded0f]
>編集者:[[Terai Atsuhiro>terai]]~
作成日:2005-01-24~
更新日:&lastmod;
---
category: swing
folder: CustomCursor
title: Cursorオブジェクトの生成
tags: [Cursor, BufferedImage, ImageIcon, JComponent]
author: aterai
pubdate: 2005-01-24T03:58:31+09:00
description: BufferedImageからカーソルオブジェクトを作成し、これをコンポーネントに設定します。
image: https://lh5.googleusercontent.com/_9Z4BYR88imo/TQTKTOEY7FI/AAAAAAAAAVw/OeBJRlIWHsQ/s800/CustomCursor.png
---
* 概要 [#summary]
`BufferedImage`からカーソルオブジェクトを作成し、これをコンポーネントに設定します。

#contents
**概要 [#c4de6873]
新しいカスタムカーソルオブジェクトを作成します。
#download(https://lh5.googleusercontent.com/_9Z4BYR88imo/TQTKTOEY7FI/AAAAAAAAAVw/OeBJRlIWHsQ/s800/CustomCursor.png)

//http://terai.xrea.jp/swing/customcursor/screenshot.png
* サンプルコード [#sourcecode]
#code(link){{
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"));
}}

**サンプルコード [#x837b7d6]
 BufferedImage bi2 = new BufferedImage(
                       32, 32, BufferedImage.TYPE_4BYTE_ABGR);
 Graphics2D g2d2 = bi2.createGraphics();
 g2d2.setPaint(Color.red);
 g2d2.drawOval(8, 8, 16, 16);
 g2d2.dispose();
 lbl2.setCursor(getToolkit().createCustomCursor(
                                bi2,new Point(16,16),"oval"));
* 解説 [#explanation]
- サイズが`32x32px`の`BufferedImage`から`Toolkit#createCustomCursor(...)`メソッドを使用してカーソルオブジェクトを作成
-- ホットスポットはその`BufferedImage`の中心に設定
- `String`: `BufferedImage`に文字を描画
- `drawOval`: `BufferedImage`に円を描画
- `paintIcon`: `BufferedImage`に`Icon`を描画

-[[サンプルを起動>http://terai.xrea.jp/swing/customcursor/sample.jnlp]]
-[[jarファイル>http://terai.xrea.jp/swing/customcursor/sample.jar]]
-[[ソース>http://terai.xrea.jp/swing/customcursor/src.zip]]
**解説 [#k25e265f]
Toolkit#createCustomCursorメソッドでカーソルオブジェクトを作成します。上記のサンプルでは、32*32のバッファの中心に、直径16ptの円をかき、中心がカーソルのホットスポットとなるようなカーソルを作っています。
* 参考リンク [#reference]
- [https://docs.oracle.com/javase/jp/8/docs/api/java/awt/Toolkit.html#createCustomCursor-java.awt.Image-java.awt.Point-java.lang.String- Toolkit#createCustomCursor(...) (Java Platform SE 8)]

//**参考リンク
**コメント [#qecf5901]
* コメント [#comment]
#comment
#comment