/** Lissajou's Figures Generator by Robert John Morton * Originated as an applet * Finished 01 October 1997 * Converted to a JFramed application Wed 15 Feb 2017 */ /* Open a terminal and change directory to where this file is loacated. To compile this program, enter the terminal command: javac lissajou.java To run this program, enter the terminal command: java lissajou */ import javax.swing.*; import java.awt.Dimension; // to be able to set the preferred JFrame size import java.awt.Color; public class lissajou extends JFrame { private int XE = 240, // Horizontal extent of window and JFrame. YE = 260; // Vertical extent of window and JFrame. /* 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 lgraph LG; public lissajou() { // construct an instance of this extended JFrame super("Lissajou's Figures"); // set the 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)); setBounds(0,0,XE,YE); // of area inside window frame setBackground(Color.black); // default background colour of JFrame LG = new trackrec(XE,YE); // Create new instance of lissajou class getContentPane().add(LG); // add it to the extended JFrame's pane. pack(); // it should automatically pack to fill JFrame setVisible(true); // make this extended JFrame visible } public static void main(final String[] args) { /* Initiate the creation of a new instance of this extended JFrame and 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 lissajou(); } } ); } catch(Exception e) { System.out.println("Couldn't create Swing GUI. You have probably"); System.out.println("made a mistake in the constructor code of one"); System.out.println("of this class's child classes."); } } /* The above try-catch sequence is necessary for the following reason. The statement "LG = new lissajou();" is a request to Swing to create a GUI instance of this application. It includes, in effect, all the statements 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. */ }