1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
|
package applets;
import java.applet.*;
import java.awt.*;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import netscape.javascript.JSObject;
import netscape.javascript.*;
public class MacAddressApplet extends Applet {
private static final long serialVersionUID = 2L;
private static String macAddress = null;
JSObject hostJavascriptEngine = null;
public void init() {
macAddress = getMacAddr();
}
public void start() {
//ne marche pas sous l'appletrunner d'Eclipse bien sur
try {
hostJavascriptEngine = JSObject.getWindow(this);
} catch (netscape.javascript.JSException ex) {
ex.printStackTrace();
}
}
public void stop() {
}
public void paint(Graphics g) {
// method to draw text on screen
if (macAddress !=null) {
g.drawString("Addresse Mac: "+macAddress, 20, 20);
}
}
public String getMacAddr()
{
String macAddr = "";
try {
InetAddress addr = InetAddress.getLocalHost();
System.out.println(addr.getHostAddress());
NetworkInterface dir = NetworkInterface.getByInetAddress(addr);
byte[] mac = dir.getHardwareAddress();
macAddr = String.format("%02X:%02X:%02X:%02X:%02X:%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
} catch (UnknownHostException e) {
macAddr = e.getMessage();
} catch (SocketException e) {
macAddr = e.getMessage();
}
return macAddr;
}
} |
Partager