16. 3. 12. 鼠标拖拽及滑轮图形Move and scale graphical objects with a mouse on the panel
2011-10-04 15:51
531 查看
![](https://oscdn.geek-share.com/Uploads/Images/Content/201912/15/ac67aacf1f2bd981bb762b7a69c5deef.gif)
import java.awt.Color; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.RenderingHints; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.event.MouseWheelEvent; import java.awt.event.MouseWheelListener; import java.awt.geom.Rectangle2D; import javax.swing.JFrame; import javax.swing.JPanel; public class MouseMoveScale extends JPanel{ private Rectangle2D.Float myRect = new Rectangle2D.Float(50,50,50,50); MovingAdapter ma = new MovingAdapter(); public MouseMoveScale(){ addMouseMotionListener(ma); addMouseListener(ma); addMouseWheelListener(new ScaleHandler()); } public void paint(Graphics g){ super.paint(g); Graphics2D g2d = (Graphics2D)g; g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); //文本抗锯齿提示 key_text_antialiasing g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); g2d.setColor(Color.magenta); g2d.fill(myRect); } class MovingAdapter extends MouseAdapter{ private int x; private int y; public void mousePressed(MouseEvent e){//按下事件 x = e.getX();//鼠标的点就是它的点 y = e.getY(); } public void mouseDragged(MouseEvent e){//拖拽事件 int dx = e.getX() - x; int dy = e.getY() - y; /** * 返回一个高精度的、比 getBounds 方法更准确的 Shape 边界框。 * 注意,不保证返回的 Rectangle2D 是包围 Shape 的最小边界框, * 只保证 Shape 完全位于指示的 Rectangle2D 中。 * 此方法返回的边界框通常比 getBounds 方法返回的更紧密, * 而且永远不会因为溢出问题而出错,因为返回值可以是一个使用双精度值存储尺寸的 Rectangle2D 实例。 */ if(myRect.getBounds2D().contains(x,y)){ myRect.x += dx; myRect.y += dy; repaint(); } x += dx; y += dy; } } class ScaleHandler implements MouseWheelListener{//鼠标滚轮事件的侦听器接口 public void mouseWheelMoved(MouseWheelEvent e) { int x = e.getX(); int y = e.getY(); if(e.getScrollType() == MouseWheelEvent.WHEEL_UNIT_SCROLL){ if(myRect.getBounds2D().contains(x,y)){//鼠标的位置在方块之内有效 //返回鼠标滚轮旋转的“喀哒声”的次数。 //如果鼠标滚轮向上/远离用户方向旋转,则为负值;如果鼠标滚轮向下/朝向用户方向旋转,则为正值 float amount = e.getWheelRotation() * 5f;//amount:总计 myRect.width += amount; myRect.height += amount; repaint(); } } } } public static void main(String[] args) { JFrame frame = new JFrame("Moving and Scaling"); MouseMoveScale m = new MouseMoveScale(); /**setDoubleBuffered()方法: * 设置此组件是否应该使用缓冲区进行绘制。如果设置为 true,则此组件的所有绘制将在一个离屏绘制缓冲区内完成。 * 然后将该离屏绘制缓冲区复制到屏幕上。如果缓冲了某个 Component,并且还缓冲了它的某个祖先, * 则将使用该祖先缓冲区。 */ m.setDoubleBuffered(true); frame.add(m); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(300, 300); frame.setLocationRelativeTo(null); frame.setVisible(true); } }
相关文章推荐
- Move ListBox Items with the Mouse (Drag and Drop
- #481 – 在InkCanvas 上使用鼠标绘图(You Can Draw On an InkCanvas Control with the Mouse)
- show div and mouse can move on the div
- Installing the Graphical Window System (X.org-X11) and the Default Desktop Environment on CentOS 6
- get the position of mouse base on IE and Fire fox
- #678 – 通过IsHitTestVisible属性使界面元素不响应鼠标(Hide an Element from the Mouse with IsHitTestVisible Property)
- 19,事件中级,默认事件,renturn false,拖拽,onmousedown,onmousemove,onmouseup
- u3d 鼠标点击位置,物体移动过去。 U3d mouse clicks position, objects move past.
- [WP7] How to press the mouse on a control, and detect MouseLeftButtonUp on another
- JavaScript onmousedown选择元素,onmousemove拖动元素,onmouseup松开鼠标,磁性吸附效果示例
- HOW TO: Tune and Scale Performance of Applications That Are Built on the .NET Framework -zt from MS
- Programming Windows with MFC - Capter 3. Mouse and keybord
- JavaScript鼠标事件onmousedown,onmousemove,onmouseout,onmouseover,onmouseup的用法和区别详解
- Complexities with NHibernate, DataTransferObjects and the Assembler Pattern(Ben Scott's Blog)
- 16. 3. 6. Tranformation with AffineTransform.getScaleInstance
- HOW TO: Tune and Scale Performance of Applications That Are Built on the .NET Framework -zt from MS
- Running OpenBTS with the Nuand bladeRF on Ubuntu (The Definitive and Step by Step Guide)
- Building Applications with Force.com and VisualForce (DEV401) (二) : Application Essentials:Designing Application on the Force.com Platform
- Testing C-State Settings and Performance with the Oracle Database on Linux
- Virtualbox mouse move in and out and file share with windows