Swing/MouseInfo のバックアップ差分(No.10)
- バックアップ一覧
- 現在との差分 を表示
- 現在との差分 - Visual を表示
- ソース を表示
- バックアップ を表示
- Swing/MouseInfo へ行く。
- 1 (2007-10-29 (月) 13:39:49)
- 2 (2007-12-28 (金) 14:41:53)
- 3 (2009-08-12 (水) 16:31:52)
- 4 (2012-06-11 (月) 21:08:47)
- 5 (2013-01-30 (水) 23:47:58)
- 6 (2013-02-01 (金) 01:28:08)
- 7 (2013-07-24 (水) 15:39:51)
- 8 (2015-10-20 (火) 17:19:01)
- 9 (2016-01-28 (木) 13:07:06)
- 10 (2017-07-04 (火) 14:00:34)
- 11 (2018-07-05 (木) 15:54:12)
- 12 (2020-07-02 (木) 04:17:37)
- 13 (2021-12-02 (木) 11:12:43)
- 追加された行はこの色です。
- 削除された行はこの色です。
--- category: swing folder: MouseInfo title: Screen上にあるMouseの位置を取得する tags: [MouseInfo, Timer] author: aterai pubdate: 2007-10-29T13:39:49+09:00 description: Screen上にあるMouseの絶対位置を取得して、パネル内のラケットを移動します。 image: https://lh4.googleusercontent.com/_9Z4BYR88imo/TQTQC6wobCI/AAAAAAAAAe8/3UnK314olDM/s800/MouseInfo.png --- * 概要 [#pec7a075] * 概要 [#summary] `Screen`上にある`Mouse`の絶対位置を取得して、パネル内のラケットを移動します。 #download(https://lh4.googleusercontent.com/_9Z4BYR88imo/TQTQC6wobCI/AAAAAAAAAe8/3UnK314olDM/s800/MouseInfo.png) * サンプルコード [#o397b7d8] * サンプルコード [#sourcecode] #code(link){{ public static final Dimension panelDim = new Dimension(320, 240); private final Racket racket = new Racket(panelDim); public MainPanel() { super(new BorderLayout()); setPreferredSize(panelDim); new javax.swing.Timer(10, this).start(); } @Override public void paintComponent(Graphics g) { @Override protected void paintComponent(Graphics g) { super.paintComponent(g); racket.draw(g); } @Override public void actionPerformed(ActionEvent e) { PointerInfo pi = MouseInfo.getPointerInfo(); Point pt = pi.getLocation(); SwingUtilities.convertPointFromScreen(pt, this); racket.move(pt.x); repaint(); } }} * 解説 [#z2793b7a] * 解説 [#explanation] 上記のサンプルでは、マウスカーソルがパネル外に移動した場合でもラケットを動かせるように、以下のような方法を使用しています。 + `10`ミリ秒ごとに`MouseInfo`から`PointerInfo`を取得 + `PointerInfo`から画面上でのポインタ座標を取得 + `SwingUtilities.convertPointFromScreen`メソッドで、これをパネル相対の座標に変換 + ラケットに変換した座標を与えて`JPanel#repaint()`メソッドで再描画 * 参考リンク [#ia57502a] * 参考リンク [#reference] - [http://www.javainthebox.net/laboratory/J2SE1.5/GUI/MouseLocation/MouseLocation.html J2SE 5.0 Tiger 虎の穴 マウスの位置] - [http://d.hatena.ne.jp/aidiary/20070601/1251545490 ラケットを動かす - Javaでゲーム作りますが何か?] * コメント [#h33780ab] * コメント [#comment] #comment - スクリーンショットの間違いを修正。 -- &user(aterai); &new{2007-12-28 (金) 14:41:53}; #comment