/** * Plink: Contacts Database Demonstrator Applet * @author Robert J Morton YE572246C * @version 25 November 2001 revamped 16 November 2007 * Converted to a JFramed application Mon 06 Mar 2017 */ /* Open a terminal and change directory to where this file is loacated. To compile this program, enter the terminal command: javac plink.java To run this program, enter the terminal command: java plink */ import javax.swing.*; import java.awt.Dimension; // to be able to set the preferred JFrame size import java.awt.Color; // to be able to set JFrame background colour public class plink extends JFrame { private static final long serialVersionUID = 507L; // what the hell this is for, I don't know! private int XE = 515, // Horizontal extent of window and JFrame. 513 + 2 YE = 328; // Vertical extent of window and JFrame. 303 + 25 private static String[] SW = {"-f","-j","-s"}; private static int[] LS = {0,1,2}; private static int ls = 1; // default is load from jar file private static String // default URL of the class, image and data files cb = "http://localhost/software/java_progs/plink/"; /* Declare a reference to an instance of the child class. Beware: an instance of the child class cannot be created here because this must be done within the invokeAndWait environment. */ private address LG; public plink() { // construct an instance of this extended JFrame //set window frame title on title bar super("Plink: Contacts Database Demonstrator Applet"); /* Set up the window listener to listen for the window close command which occurs when the user clicks on the X control in the window's title bar.*/ setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); /* Set the initial size of the extended JFrame. This is the area INSIDE the frame. It does not include the border or the title bar areas. */ setPreferredSize(new Dimension(XE, YE)); setBounds(0,0,XE,YE); // of area inside window frame setBackground(Color.black); // default background colour of JFrame LG = new address(XE,YE,ls,cb); // Create new instance of plink getContentPane().add(LG); // add it to the extended JFrame's pane pack(); // should automatically fill the JFrame setVisible(true); // make this extended JFrame visible } public static void main(final String[] args) { if(argsValid(args)) { /* Initiate the creation of a new instance of this extended JFrame then wait until the building process has completely finished before giving the new instance of the child class permission to commence running. */ try { javax.swing.SwingUtilities.invokeAndWait( new Runnable(){public void run(){new plink();}}); } catch(Exception e) { System.out.println("Couldn't create Swing GUI."); System.out.println("There's probably an error in the constructor"); System.out.println("code of one of this class's child classes."); System.out.println(e); System.out.println(e.getCause()); } } // end of if(argsValid) } // end of main() /* The above try-catch sequence is necessary for the following reason. The statement "new plink();" is a request to Swing to create a GUI instance of this application. It includes, in effect, all the statements in the constr- uctor methods of all this class's child classes. All these requests are made on this, the main() program thread. However, these requests are carried out [ie the actual constructing is done] by the underlying Swing system on the Event Despatching Thread. The constr- ucting process necessarily takes longer to do than the mere task of REQUEST- ING that the constructing be done. This means that the main() thread will set the run() thread in motion before the GUI has finished being construct- ed. Hence trouble. The solution is to force the main() thread to withhold permission for the run() thread to start its job until the Event Despatching Thread has comp- letely finished constructing the GUI. */ /* NOTE: This class inherits a paint() method from the JFrame class that it extends. This method automatically calls the paint() method of the child [extended JPanel] class when necessary. */ private static boolean argsValid(String[] args) { int L = args.length; //number of command-line arguments if(L == 0) return true; //if command line has no arguments String s = args[0]; //the first command line argument if(s.indexOf("help") != -1) { System.out.println("This program accepts command line arguments to"); System.out.println("say from where it should load its data files:"); System.out.println("-f = load files from local directory."); System.out.println("-j = load files from this app's jar file."); System.out.println("-s = load files from a specified server."); System.out.println("A URL may be entered after the -s switch."); System.out.println("If no URL is given, a default URL is used."); return false; } for(int i = 0; i < 3; i++) { if(s.equals(SW[i])) { ls = i; break; } } if(L < 2) return true; //no second argument so use default URL cb = args[1]; //there must be a second arguement [URL] if(!cb.startsWith("http://")) { System.out.println("URL has absent or invalid protocol."); return false; } return true; } } //end of plink class