Vibrate a WindowTag(s): AWT
This HowTo is based on this post : http://sdnshare.sun.com/view.jsp?id=2326
This can be useful to make a visual effect when an event is occuring.
import java.awt.*;
public class FrameUtils {
private final static int VIBRATION_LENGTH = 20;
private final static int VIBRATION_VELOCITY = 5;
private FrameUtils() { }
public static void vibrate(Frame frame) {
try {
final int originalX = frame.getLocationOnScreen().x;
final int originalY = frame.getLocationOnScreen().y;
for(int i = 0; i < VIBRATION_LENGTH; i++) {
Thread.sleep(10);
frame.setLocation(originalX, originalY + VIBRATION_VELOCITY);
Thread.sleep(10);
frame.setLocation(originalX, originalY - VIBRATION_VELOCITY);
Thread.sleep(10);
frame.setLocation(originalX + VIBRATION_VELOCITY, originalY);
Thread.sleep(10);
frame.setLocation(originalX, originalY);
}
}
catch (Exception err) {
err.printStackTrace();
}
}
}
FrameUtils.vibrate(myFrame);
Here an example.
We display a small window. When a file is added or deleted, the window is shaking for a brief moment and display the event. By default, the folder c:/temp is used but you can specify another one on the command line.
First get the code to detect a file modfication in a folder (in this HowTo, you need the DirWatcher and DirFilterWatcher classes). Plus the following classes.
import java.awt.*;
import java.awt.event.*;
public class DirWatchWindow extends Frame {
Label folder;
Label info;
public DirWatchWindow() {
setTitle("DirWatchWindow");
setSize(600, 100);
setLayout(new BorderLayout());
folder = new Label("");
info = new Label("");
add(folder, BorderLayout.NORTH);
add(info, BorderLayout.SOUTH);
setVisible(true);
addWindowListener
(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}
);
}
public void setInfo(String text) {
info.setText(text);
}
public void setFolder(String dir) {
folder.setText("Watching folder : " + dir);
}
}
import java.util.*;
import java.io.*;
public class DirWatchTest {
public static void main(String args[]) {
String folderToWatch = "c:/temp";
if (args.length > 0) {
folderToWatch = args[0];
}
final DirWatchWindow dww = new DirWatchWindow();
dww.setFolder(folderToWatch);
TimerTask task = new DirWatcher(folderToWatch, "txt" ) {
protected void onChange( File file, String action ) {
// here we code the action on a change
dww.setInfo("File : "+ file.getName() +" action: " + action);
FrameUtils.vibrate(dww);
}
};
Timer timer = new Timer();
timer.schedule( task , new Date(), 1000 );
}
}
To launch the demo
java -jar DirWatch.jar
java -jar DirWatch.jar c:/myfolder
mail_outline
Send comment, question or suggestion to howto@rgagnon.com
Send comment, question or suggestion to howto@rgagnon.com