鼠标事件实例
来源:互联网 发布:gnuradio windows 编辑:程序博客网 时间:2024/04/29 19:46
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.awt.geom.*;
public class MouseTest
{
public static void main(String[] args)
{
MouseFrame frame = new MouseFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();
}
}
class MouseFrame extends JFrame
{
public MouseFrame()
{
setTitle("MouseTest");
setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT);
MousePanel panel = new MousePanel();
Container contentPane = getContentPane();
contentPane.add(panel);
}
public static final int DEFAULT_WIDTH = 300;
public static final int DEFAULT_HEIGHT = 200;
}
class MousePanel extends JPanel
{
public MousePanel()
{
squares = new ArrayList();
current = null;
addMouseListener(new MouseHandler());
addMouseMotionListener(new MouseMotionHandler());
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
for(int i=0;i<squares.size();i++)
g2.draw((Rectangle2D)squares.get(i));
}
public Rectangle2D find(Point2D p)
{
for(int i=0;i<squares.size();i++)
{
Rectangle2D r = (Rectangle2D)squares.get(i);
if(r.contains(p)) return r;
}
return null;
}
public void add(Point2D p)
{
double x=p.getX();
double y=p.getY();
current = new Rectangle2D.Double(
x - SIDELENGTH/2,
y - SIDELENGTH/2,
SIDELENGTH,
SIDELENGTH);
squares.add(current);
repaint();
}
public void remove(Rectangle2D s)
{
if(s == null) return;
if(s == current) current = null;
squares.remove(s);
repaint();
}
private static final int SIDELENGTH = 10;
private ArrayList squares;
private Rectangle2D current;
private class MouseHandler extends MouseAdapter
{
public void mousePressed(MouseEvent event)
{
current = find(event.getPoint());
if(current == null)
add(event.getPoint());
}
public void mouseClicked(MouseEvent event)
{
current = find(event.getPoint());
if(current != null&& event.getClickCount()>=2)
remove(current);
}
}
private class MouseMotionHandler
implements MouseMotionListener
{
public void mouseMoved(MouseEvent event)
{
if(find(event.getPoint()) == null)
setCursor(Cursor.getDefaultCursor());
else
setCursor(Cursor.getPredefinedCursor
(Cursor.CROSSHAIR_CURSOR));
}
public void mouseDragged(MouseEvent event)
{
if(current != null)
{
int x = event.getX();
int y = event.getY();
current.setFrame(
x - SIDELENGTH/2,
y - SIDELENGTH/2,
SIDELENGTH,
SIDELENGTH);
repaint();
}
}
}
}
- 鼠标事件实例
- openCV鼠标事件实例
- c 语言 鼠标事件 实例
- Java 鼠标点击事件实例
- 鼠标事件和滚轮事件实例
- javascript事件处理中的鼠标事件实例
- JQuery实例入门(添加鼠标事件)
- JQuery实例入门(添加鼠标事件)
- opengl入门实例(4)-鼠标事件
- 第一个JavaScript实例,鼠标事件
- 【PyQt4 实例25】获得鼠标事件
- 鼠标事件之购物筐实例
- Javascript事件对象+鼠标拖动实例
- Matlab学习-------GUI鼠标事件响应(鼠标划线实例)
- Python中使用PyHook监听鼠标和键盘事件实例
- javascript事件处理中Event对象(键盘事件和鼠标事件)实例
- 鼠标事件
- 鼠标事件
- 数据库入口:JDBC
- 俺的博客开版!!自己祝贺一下下!
- 自由软件发布方法惯例
- tomcat中的server.xml元素详解
- 未来15天的工作计划
- 鼠标事件实例
- web.xml元素详解
- 如何在WinXP平台下架设Snort+acid入侵侦测系统
- 花7150元买了一个房子!
- 我与感冒有个约会
- 爱情点滴摘录
- Input — the only way to learn English
- IIS不支持ASP的解决办法
- IIS不支持ASP的解决办法