//MyUtil.java -- by tsaiwn@csie.nctu.edu.tw // some static utility functions for Application import java.applet.*; import java.awt.*; import java.net.*; import java.io.*; public class MyUtil { /// newCodeBase() --- get the user's current working Directory static public URL newCodeBase( ) { // for Application URL codeBase = null; try{ codeBase = new URL("file:" + System.getProperty("user.dir") + "/"); } catch ( Exception e ) { // maybe it is an Applet, use getCodeBase() or getDocumentBase() } return codeBase; } /// same as newCodeBase() --- get the user's current working Directory static public URL getMyCodeBase( ) { // for Application return newCodeBase( ); } /// getAudio(filename) --- get AudioClip from an Application (not Applet) static public AudioClip getAudio(String filename) { return getAudio(newCodeBase( ), filename); } // getAudio(url) /// getAudio(url, filename) static public AudioClip getAudio(URL base, String filename) { AudioClip audio = null; try{ audio = Applet.newAudioClip( new URL(base+ filename) ); } catch ( Exception e ) { // maybe it is an Applet, use getAudioClip(URL url) // .. or getAudioClip(URL url, String filename) in Applet } return audio; } // getAudio(url, filename) /// getAudio(url) --- get AudioClip from an Application (not Applet) static public AudioClip getAudio(URL url) { AudioClip audio = null; try{ audio = Applet.newAudioClip( url ); // JDK1.2 static method } catch ( Exception e ) {;} return audio; } // getAudio(url) /// get Image from a local file --- for Application (not Applet) static public Image newImage(String filename) { Image img = null; try{ Toolkit tkt = Toolkit.getDefaultToolkit( ); img = tkt.getImage(filename); //for application only // img = getImage(getCodeBase( ), filename); //for Applet } catch ( Exception e ) {;} return img; } // newImage(filename) static public Image getMyImage(String filename) { return newImage(filename); } // getMyImage(filename) /// get Image from a URL static public Image newImage(URL url) { Image img = null; try{ Toolkit tkt = Toolkit.getDefaultToolkit( ); img = tkt.getImage(url); //for application only } catch ( Exception e ) {;} return img; } // newImage(url) /// @@@ 寫個 function to createMyOwnCursor 方便使用@@@ static public Cursor createMyCursor(String filename) throws IndexOutOfBoundsException { Cursor csr = null; try{ Toolkit tkt = Toolkit.getDefaultToolkit( ); Image csrImg = tkt.getImage(filename); //for application //Image csrImg = getImage(getCodeBase( ), filename); //for Applet csr = tkt.createCustomCursor(csrImg, new Point(1,1), filename); } catch ( Exception e ) { csr = new Cursor(Cursor.HAND_CURSOR); } return csr; } // createMyCursor /// some printf utilities public static void print(String s) throws IOException { printf(s); } public static void printf(String s) throws IOException { System.out.print(s); } public static void println(String s) throws IOException { System.out.println(s); } public static void print(long n) throws IOException { printf(n); } public static void printf(long n) throws IOException { System.out.print(""+n); } public static void print(int n) throws IOException { printf(n); } public static void printf(int n) throws IOException { System.out.print(""+n); } /// print n as w columns width public static void printf(short n, int w) throws IOException { printf((long)n, w); // print n as w columns } public static void printf(int n, int w) throws IOException { printf((long)n, w); } public static void printf(long n, int w) throws IOException { long tmp = 1; // print n as w columns long nabs = n; if(n<0) {nabs = -nabs; --w; } // ensure positive for(;;) { tmp *= 10; if(tmp > nabs) break; --w; // w is the expected Width (number of digits) } for(;;) { if(--w <= 0) break; System.out.print(" "); // necessary leading space } System.out.print(""+n); } /// a main program to test my printf functions public static void main(String[]p) throws IOException { printf("=="); printf(1, 1); printf("==\n"); printf("=="); printf(2, 2); printf("==\n"); printf("=="); printf(3, 3); printf("==\n"); printf("=="); printf(11, 1); printf("==\n"); printf("=="); printf(12, 2); printf("==\n"); printf("=="); printf(13, 3); printf("==\n"); printf("=="); printf(111, 1); printf("==\n"); printf("=="); printf(112, 2); printf("==\n"); printf("=="); printf(113, 3); printf("==\n"); printf("=="); printf(114, 4); printf("==\n"); printf("=="); printf(115, 5); printf("==\n"); printf("=="); printf(-11, 1); printf("==\n"); printf("=="); printf(-12, 2); printf("==\n"); printf("=="); printf(-13, 3); printf("==\n"); printf("=="); printf(-14, 4); printf("==\n"); printf("=="); printf(-15, 5); printf("==\n"); printf("=="); printf(-116, 6); printf("==\n"); try { AudioClip sound = null; sound = getAudio("laugh.au"); if(sound != null) { sound.play( ); Thread.sleep(2588); } } catch(Exception e) {;} printf("Thank you!\n"); System.exit(0); } } // class MyUtil