import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class RightClickGlassPane extends JComponent implements MouseListener, MouseMotionListener {
protected JPopupMenu popup;
protected Container contentPane;
public RightClickGlassPane(Container contentPane, JPopupMenu menu) {
addMouseListener(this);
addMouseMotionListener(this);
this.contentPane = contentPane;
popup = menu;
}
// draw some text just so we know the glass pane
// is installed and visible
public void paint(Graphics g) {
//g.drawString("I'm a glass pane",50,50);
}
// catch all mouse events and redispatch them
public void mouseMoved(MouseEvent e) {
redispatchMouseEvent(e, false);
}
public void mouseDragged(MouseEvent e) {
redispatchMouseEvent(e, false);
}
public void mouseClicked(MouseEvent e) {
redispatchMouseEvent(e, false);
}
public void mouseEntered(MouseEvent e) {
redispatchMouseEvent(e, false);
}
public void mouseExited(MouseEvent e) {
redispatchMouseEvent(e, false);
}
public void mousePressed(MouseEvent e) {
redispatchMouseEvent(e, false);
}
public void mouseReleased(MouseEvent e) {
redispatchMouseEvent(e, false);
}
public void showPopup(Component comp, int x, int y) {
popup.show(comp,x,y);
}
protected void redispatchMouseEvent(MouseEvent e, boolean repaint) {
// if it's a popup
if(e.isPopupTrigger()) {
// show the popup and return
showPopup(e.getComponent(), e.getX(), e.getY());
} else {
doDispatch(e);
}
}
protected Component getRealComponent(Point pt) {
// get the mouse click point relative to the content pane
Point containerPoint =
SwingUtilities.convertPoint(this, pt,contentPane);
// find the component that under this point
Component component = SwingUtilities.getDeepestComponentAt(
contentPane,
containerPoint.x,
containerPoint.y);
return component;
}
protected void doDispatch(MouseEvent e) {
// since it's not a popup we need to redispatch it.
Component component = getRealComponent(e.getPoint());
// return if nothing was found
if (component == null) {
return;
}
// convert point relative to the target component
Point componentPoint = SwingUtilities.convertPoint(
this,
e.getPoint(),
component);
// redispatch the event
component.dispatchEvent(new MouseEvent(component,
e.getID(),
e.getWhen(),
e.getModifiers(),
componentPoint.x,
componentPoint.y,
e.getClickCount(),
e.isPopupTrigger()));
}
public static void main(String[] args) {
// create a frame with some components in it
JFrame frame = new JFrame("Right Click Test");
JButton button = new JButton("this is a button");
JTextField tf = new JTextField("this is a textfield");
JPanel panel = new JPanel();
panel.add(button);
panel.add(tf);
frame.getContentPane().add(panel);
JPopupMenu popup = new JPopupMenu();
popup.add(new JMenuItem("Dogs"));
popup.add(new JMenuItem("Cats"));
popup.add(new JMenuItem("Mass Hysteria"));
// create the right click glass pane.
Component rc = new RightClickGlassPane(frame.getContentPane(),popup);
// set as glasspane and make it visible
frame.setGlassPane(rc);
rc.setVisible(true);
// pack and show the frame
frame.pack();
frame.setSize(400,200);
frame.show();
}
// utiltity function
public static void p(String str) {
System.out.println(str);
}
}
Create a pop-up baloon in the system tray
public class SysTray {
public static void main(String[] args) throws Exception {
TrayIcon icon = new TrayIcon(getImage(), "Java application as a tray icon",
createPopupMenu());
icon.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "Hey, you activated me!");
}
});
SystemTray.getSystemTray().add(icon);
Thread.sleep(3000);
icon.displayMessage("Attention", "Please click here",
TrayIcon.MessageType.WARNING);
}
private static Image getImage() throws HeadlessException {
Icon defaultIcon = MetalIconFactory.getTreeHardDriveIcon();
Image img = new BufferedImage(defaultIcon.getIconWidth(),
defaultIcon.getIconHeight(), BufferedImage.TYPE_4BYTE_ABGR);
defaultIcon.paintIcon(new Panel(), img.getGraphics(), 0, 0);
return img;
}
private static PopupMenu createPopupMenu() throws HeadlessException {
PopupMenu menu = new PopupMenu();
MenuItem exit = new MenuItem("Exit");
exit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
menu.add(exit);
return menu;
}
}
public static void main(String[] args) throws Exception {
TrayIcon icon = new TrayIcon(getImage(), "Java application as a tray icon",
createPopupMenu());
icon.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "Hey, you activated me!");
}
});
SystemTray.getSystemTray().add(icon);
Thread.sleep(3000);
icon.displayMessage("Attention", "Please click here",
TrayIcon.MessageType.WARNING);
}
private static Image getImage() throws HeadlessException {
Icon defaultIcon = MetalIconFactory.getTreeHardDriveIcon();
Image img = new BufferedImage(defaultIcon.getIconWidth(),
defaultIcon.getIconHeight(), BufferedImage.TYPE_4BYTE_ABGR);
defaultIcon.paintIcon(new Panel(), img.getGraphics(), 0, 0);
return img;
}
private static PopupMenu createPopupMenu() throws HeadlessException {
PopupMenu menu = new PopupMenu();
MenuItem exit = new MenuItem("Exit");
exit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
menu.add(exit);
return menu;
}
}
Wake-On Lan
package WakeOn;
import ems_proto.*;
import java.io.*;
import java.net.*;
public class WakeOnLan {
public static int PORT = 9;
String ipStr;
String macStr;
public static boolean wakeOnPC(String bIp, String mac){
String ipStr = bIp;
String macStr = mac;
try {
byte[] macBytes = getMacBytes(macStr);
byte[] bytes = new byte[6 + 16 * macBytes.length];
for (int i = 0; i < 6; i++) {
bytes[i] = (byte) 0xff;
}
for (int i = 6; i < bytes.length; i += macBytes.length) {
System.arraycopy(macBytes, 0, bytes, i, macBytes.length);
}
InetAddress address = InetAddress.getByName(ipStr);
DatagramPacket packet = new DatagramPacket(bytes, bytes.length, address, PORT);
DatagramSocket socket = new DatagramSocket();
socket.send(packet);
socket.close();
System.out.println("Wake-on-LAN packet sent.");
return true;
}
catch (Exception e) {
System.out.println("Failed to send Wake-on-LAN packet:"+e);
return false;
//System.exit(1);
}
}
public static void main(String[] args) {
if (args.length != 2) {
System.out.println("Usage: java WakeOnLan ");
System.out.println("Example: java WakeOnLan 192.168.0.255 00:0D:61:08:22:4A");
System.out.println("Example: java WakeOnLan 192.168.0.255 00-0D-61-08-22-4A");
System.exit(1);
}
}
private static byte[] getMacBytes(String macStr) throws IllegalArgumentException {
byte[] bytes = new byte[6];
String[] hex = macStr.split("(\\:|\\-)");
if (hex.length != 6) {
throw new IllegalArgumentException("Invalid MAC address.");
}
try {
for (int i = 0; i < 6; i++) {
bytes[i] = (byte) Integer.parseInt(hex[i], 16);
}
}
catch (NumberFormatException e) {
throw new IllegalArgumentException("Invalid hex digit in MAC address.");
}
return bytes;
}
}
import ems_proto.*;
import java.io.*;
import java.net.*;
public class WakeOnLan {
public static int PORT = 9;
String ipStr;
String macStr;
public static boolean wakeOnPC(String bIp, String mac){
String ipStr = bIp;
String macStr = mac;
try {
byte[] macBytes = getMacBytes(macStr);
byte[] bytes = new byte[6 + 16 * macBytes.length];
for (int i = 0; i < 6; i++) {
bytes[i] = (byte) 0xff;
}
for (int i = 6; i < bytes.length; i += macBytes.length) {
System.arraycopy(macBytes, 0, bytes, i, macBytes.length);
}
InetAddress address = InetAddress.getByName(ipStr);
DatagramPacket packet = new DatagramPacket(bytes, bytes.length, address, PORT);
DatagramSocket socket = new DatagramSocket();
socket.send(packet);
socket.close();
System.out.println("Wake-on-LAN packet sent.");
return true;
}
catch (Exception e) {
System.out.println("Failed to send Wake-on-LAN packet:"+e);
return false;
//System.exit(1);
}
}
public static void main(String[] args) {
if (args.length != 2) {
System.out.println("Usage: java WakeOnLan
System.out.println("Example: java WakeOnLan 192.168.0.255 00:0D:61:08:22:4A");
System.out.println("Example: java WakeOnLan 192.168.0.255 00-0D-61-08-22-4A");
System.exit(1);
}
}
private static byte[] getMacBytes(String macStr) throws IllegalArgumentException {
byte[] bytes = new byte[6];
String[] hex = macStr.split("(\\:|\\-)");
if (hex.length != 6) {
throw new IllegalArgumentException("Invalid MAC address.");
}
try {
for (int i = 0; i < 6; i++) {
bytes[i] = (byte) Integer.parseInt(hex[i], 16);
}
}
catch (NumberFormatException e) {
throw new IllegalArgumentException("Invalid hex digit in MAC address.");
}
return bytes;
}
}
Subscribe to:
Posts (Atom)