/** * Doppler Effect Demonstrator * @author Robert J Morton YE572246C * @version 25 November 2001 revamped 16 November 2007 * Converted to a JFramed application Fri 03 Mar 2017. */ /* Open a terminal and change directory to where this file is loacated. To compile this program, enter terminal command: javac dilation.java To run this program, enter the terminal command: java dilation */ import javax.swing.*; import java.awt.Dimension; // to be able to set the preferred JFrame size import java.awt.Container; // to be able to set the preferred JFrame size import java.awt.Color; // to be able to set JFrame background colour public class dilation extends JFrame { private static final long serialVersionUID = 207L; // what the hell this is for, I don't know! private int // Integer values which are private to this class XE = 552, // Horizontal extent of window and JFrame. 550 + 2 YE = 175; // Vertical extent of window and JFrame. 150 + 25 private Color bg = new Color(220,220,220); // background colour private static int ls = 0; private static String // default URL of the class, image and data files cb = "http://localhost/software/java_progs/altrel/"; /* 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 altrel LG; // STUFF SPECIFIC TO THIS PARTICULAR APPLICATION PROGRAM private static String Title[] = { // Title of applet window "Time Dilation Demonstrator", "Demonstrador de Dilatação do Tempo" }, SA[][] = { // annotations in both languages {"Module Velocity","Phantom Velocity","Phantom Clock Speed", "Outward:","Return:"}, {"Veloc. do Módulo","Veloc. do Fantasma","Relógio do Fantasma", "Saída:","Retorno:"} }, SB[] = new String[5]; // for the annotations in the selected language public dilation(){ // construct an instance of this extended JFrame super(Title[ls]); //set window frame title on title bar /* 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)); setLayout(null); // allow the control panels to be laid out manually setBounds(0,0,XE,YE); // of area inside window frame setBackground(bg); // default background colour of JFrame /* copy the annotations for the selected language from the SA array to the SB array. */ for(int i = 0; i < 5; i++) SB[i] = SA[ls][i]; Container cp = getContentPane(); // ref to JFrame's content pane /* Annotation Labels: These are added here, directly to the JFrame's content pane, rather than to the child JPanel. This is because the paint() method of the child JPanel, in this particular application, is overridden to paint only the off-screen animation images, which means that it will not paint the JLabels. */ JLabel a = new JLabel(SB[0]); cp.add(a); a.setBounds( 85, 65, 145, 20); JLabel b = new JLabel(SB[1]); cp.add(b); b.setBounds(230, 65, 170, 20); JLabel c = new JLabel(SB[2]); cp.add(c); c.setBounds(385, 65, 160, 20); JLabel d = new JLabel(SB[3], JLabel.RIGHT); cp.add(d); d.setBounds( 10, 93, 70, 20); JLabel e = new JLabel(SB[4], JLabel.RIGHT); cp.add(e); e.setBounds( 10,115, 70, 20); LG = new altrel(XE,YE,ls); // Create new instance of altrel class cp.add(LG); // add it to the extended JFrame's pane. 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 dilation();}} ); } 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()); } /* The above try-catch sequence is necessary for the following reason. The statement "new dilation();" is a request to Swing to create a GUI instance of this application. It includes, in effect, all the state- ments in the constructor 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 constructing process necessarily takes longer to do than the mere task of REQUESTING that the constructing be done. This means that the main() thread will set the run() thread in motion before the GUI has finished being constructed. 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 completely 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. */ } // end of if(argsValid) } // end of main() 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("set the annotation language of the application:"); System.out.println("-en = show annotations in English."); System.out.println("-pt = mostre anotações no português."); return false; } if(s.equals("-pt")) ls = 1; return true; } } // end of dilation class