Swing/MouseInfo のバックアップ差分(No.2)
- バックアップ一覧
- 現在との差分 を表示
- 現在との差分 - 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)
- 追加された行はこの色です。
- 削除された行はこの色です。
TITLE:JFrameの外にあるMouseの位置を取得する TITLE:Screen上にあるMouseの位置を取得する #navi(../) *JFrameの外にあるMouseの位置を取得する [#pec7a075] *Screen上にあるMouseの位置を取得する [#pec7a075] >編集者:[[Terai Atsuhiro>terai]]~ 作成日:2007-10-29~ 更新日:&lastmod; #contents **概要 [#vaf313cf] JFrameの外にあるMouseの絶対位置を取得して、パネル内のラケットを移動します。 Screen上にあるMouseの絶対位置を取得して、パネル内のラケットを移動します。 #screenshot **サンプルコード [#o397b7d8] #code{{ 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(); super(new BorderLayout()); setPreferredSize(panelDim); new javax.swing.Timer(10, this).start(); } @Override public void paintComponent(Graphics g) { super.paintComponent(g); racket.draw(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(); PointerInfo pi = MouseInfo.getPointerInfo(); Point pt = pi.getLocation(); SwingUtilities.convertPointFromScreen(pt, this); racket.move(pt.x); repaint(); } }} -&jnlp; //-&jnlp; -&jar; -&zip; **解説 [#z2793b7a] 上記のサンプルでは、以下の手順でラケットを動かしています。 + 10ミリ秒ごとにMouseInfoからPointerInfoを取得 + PointerInfoから画面上でのポインタ座標を取得 + SwingUtilities.convertPointFromScreenメソッドで、パネル相対のポインタ座標に変換 + SwingUtilities.convertPointFromScreenメソッドで、これをパネル相対のポインタ座標に変換 + ラケットに変換した座標を与えて、repaint **参考リンク [#ia57502a] -[[J2SE 5.0 Tiger 虎の穴 マウスの位置>http://www.javainthebox.net/laboratory/J2SE1.5/GUI/MouseLocation/MouseLocation.html]] -[[ラケットを動かす - Javaでゲーム作りますが何か?>http://javagame.main.jp/index.php?%A5%E9%A5%B1%A5%C3%A5%C8%A4%F2%C6%B0%A4%AB%A4%B9]] **コメント [#h33780ab] #comment