• 追加された行はこの色です。
  • 削除された行はこの色です。
TITLE:Cursorオブジェクトの生成
#navi(../)
#tags()
RIGHT:Posted by &author(aterai); at 2005-01-24
*Cursorオブジェクトの生成 [#l60ded0f]
新しいカスタムカーソルオブジェクトを作成します。
---
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`からカーソルオブジェクトを作成し、これをコンポーネントに設定します。

-&jnlp;
-&jar;
-&zip;
#download(https://lh5.googleusercontent.com/_9Z4BYR88imo/TQTKTOEY7FI/AAAAAAAAAVw/OeBJRlIWHsQ/s800/CustomCursor.png)

//#screenshot
#ref(http://lh5.ggpht.com/_9Z4BYR88imo/TQTKTOEY7FI/AAAAAAAAAVw/OeBJRlIWHsQ/s800/CustomCursor.png)

**サンプルコード [#x837b7d6]
* サンプルコード [#sourcecode]
#code(link){{
BufferedImage bi2 = new BufferedImage(
                      32, 32, BufferedImage.TYPE_INT_ARGB);
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"));
label2.setIcon(new ImageIcon(bi2));
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"));
}}

**解説 [#k25e265f]
Toolkit#createCustomCursorメソッドでカーソルオブジェクトを作成します。上記のサンプルコードでは、32*32のバッファの中心に、直径16の円をかき、この円の中心がホットスポットとなるようなカーソルを作っています。
* 解説 [#explanation]
- サイズが`32x32px`の`BufferedImage`から`Toolkit#createCustomCursor(...)`メソッドを使用してカーソルオブジェクトを作成
-- ホットスポットはその`BufferedImage`の中心に設定
- `String`: `BufferedImage`に文字を描画
- `drawOval`: `BufferedImage`に円を描画
- `paintIcon`: `BufferedImage`に`Icon`を描画

//**参考リンク
**コメント [#qecf5901]
* 参考リンク [#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)]

* コメント [#comment]
#comment
#comment