Swing/MoveNonRectangularImage のバックアップソース(No.2)
- バックアップ一覧
- 差分 を表示
- 現在との差分 を表示
- 現在との差分 - Visual を表示
- バックアップ を表示
- Swing/MoveNonRectangularImage へ行く。
- 1 (2008-11-17 (月) 16:07:53)
- 2 (2011-02-20 (日) 22:03:05)
- 3 (2012-05-16 (水) 14:17:36)
- 4 (2013-01-15 (火) 17:43:55)
- 5 (2015-07-15 (水) 18:58:58)
- 6 (2016-05-19 (木) 20:42:45)
- 7 (2017-03-29 (水) 15:47:04)
- 8 (2018-02-07 (水) 18:37:57)
- 9 (2018-12-20 (木) 11:33:09)
- 10 (2020-11-14 (土) 16:53:38)
- 11 (2022-12-09 (金) 12:46:24)
TITLE:JComponentの形状を変更する #navi(../) RIGHT:Posted by [[aterai]] at 2008-11-17 *JComponentの形状定義を変更する [#hda649fc] コンポーネントの形状を画像の不透明領域に合わせて変更します。 -&jnlp; -&jar; -&zip; //#screenshot #ref(http://lh5.ggpht.com/_9Z4BYR88imo/TQTQKdiDk4I/AAAAAAAAAfI/tb322r8ngL0/s800/MoveNonRectangularImage.png) **サンプルコード [#h071e116] #code{{ ImageIcon i = new ImageIcon(getClass().getResource("duke.gif")); Dimension d = new Dimension(i.getIconWidth(), i.getIconHeight()); final BufferedImage image = new BufferedImage(d.width,d.height,BufferedImage.TYPE_INT_ARGB); Graphics g = image.createGraphics(); i.paintIcon(null,g,0,0); g.dispose(); final JLabel icon = new JLabel(i) { public boolean contains(int x, int y) { return super.contains(x, y) && ((image.getRGB(x, y) >> 24) & 0xff) > 0; } }; icon.setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR)); MouseAdapter l = new MouseAdapter() { private Point start; private Point loc; public void mousePressed(MouseEvent me) { start = me.getPoint(); } public void mouseDragged(MouseEvent me) { loc = icon.getLocation(loc); int x = loc.x - start.x + me.getX(); int y = loc.y - start.y + me.getY(); icon.setLocation(x, y); } }; icon.addMouseListener(l); icon.addMouseMotionListener(l); }} **解説 [#lea73de2] 上記のサンプルでは、非矩形画像の不透明部分だけマウスでドラッグできるように、マウス処理にUIが使用する contains メソッドをオーバーライドし、透明部分がJLabelの形状に含まれないようにしています。 **参考リンク [#va0cebde] -[https://duke.dev.java.net/images/iconSized/index.html duke - Files in images - iconSized] -[[JButtonの形を変更>Swing/RoundButton]] -[[ImageIconの形でJButtonを作成>Swing/RoundImageButton]] **コメント [#a017a413] #comment