GUI (Explanation of GUI.java) - Wait-For-It-123/logiCAD GitHub Wiki

This page explains the purpose and flow of GUI.java

The GUI class for LogiCAD powers all visual elements and handles all user interaction. It uses Java Swing for the core graphics library.

Important Variables:

  1. circuitElementButtonClicked

    1. This holds the the value of which gate button has been clicked. The values are held as constants which are enumerated as -1 through 12. You will find these at eh top of the GUI class.
  2. optionsButtons

    1. This holds the value of the which option button has been clicked. It is defaulted to INVALID so no particular action is taken when clicking in the workspace. The values are held in the same manner as the gate buttons. These can be found just below the gate button constants.
  3. Gate Constants (Special Cases)

    1. INVALID = -1: Invalid is the state in which no button has been selected and no action will be performed when clicking in the workspace.
    2. INPUT_LOGIC_0/1 = 7/8: The logic input can be either 0 or 1 and there are images to reflect that. If it is a 0 the state will be set to 7 and if a 1, 8.
    3. OUTPUT_LOGIC_0/1/X = 10/11/12: These behave in the same way as the inputs. If the output in the circuit is a 0/1 the appropriate state/image is set/displayed. The X state is for an incomplete circuit/dangling wires and will be displayed if evaluate finds these.

Class Structure: The main class is the GUI class which has one method, run. Inside run is all of the gui functionality. To get our workspace running as you see it we borrowed code from an Oracle tutorial that we cite containing the ScrollDemo2 class which itself contains the DrawingPane class.

GUI: This class holds all gui elements and is responsible for all gui interaction.

ScrollDemo2: extends JPanel implements MouseListener This class is from the borrowed code. This class creates a JScrollPane which contains a JPanel of type DrawingPane which is the main workspace for the application. Each interaction in this gui requires 2 clicks. the first is on the button of the action you wish to perform and the second is in the workspace to perform the action. The second click requires a second MouseListener for the workspace. The ArrayList of images to be used in the workspace is initialized in the InitializeImageContainer method. In this class we prevent two circuit elements from being placed too close together.

DrawingPane: extends JPanel The paintComponent method has been overridden with our own code. This method is run anytime a change is made in the application. It first clears the screen and then draws all circuit elements and wires in the workspace. to show any changes made.

Save Function & Button: The save button has been added to menuBar under the File menu. When save is clicked a file explorer pops up and allows the user to choose the location in which to save their work. The file must be save as a .lca file. When location and name for the file is chosen the user can click save. When save is clicked, FileInputAndOutput, serializeModelData, and getAbsolutePath are used. See the model and File Input & Output wiki pages for a description of these classes and methods. Lastly, if the extension is correct then the file is saved. Otherwise, the program will throw an error and the user must restart the saving process.

Load Function & Button: The Load button has also been added to the menuBar and File menu. A file explorer pops up when it is clicked. The user can choose any .lca file they have in their file system to load into LogiCAD. A dialogue box will appear warning the user about losing all current unsaved work if a file is loaded. Once a file is chosen All current workspace elements, connections and information are cleared from the ArrayLists, the GUI method loadStateFromString (see below) is called and the frame is redrawn to reflect the changes.

public void loadStateFromString(String state) - This method takes the state of a previously saved workspace in String form and builds the workspace back up to what the encoded text form represents by recreating circuit element objects, their connections, and restoring the values of global variables.

Delete Button Activates the delete function when clicked. The next Gate, Input, or Output clicked in the workspace is removed along with all connecting wires.

Connect Button Activates the connect function when clicked. A connection is made by first clicking a source (parent) circuit element and then a destination (child) circuit element. Invalid connections are not created and prompt an error message to be displayed. Connections can continue to be formed as long as this is activated.

Toggle Input Button Activates the toggle input function when clicked. When activated, any input that is clicked will change its signal value from 0 to 1 or from 1 to 0. Inputs may be toggled as many times as the user wishes when this is activated.

Cancel Button Cancels the previously selected/activated function.

Evaluate Button Evaluates the circuit signals when clicked. The signals are allowed to propagate from Inputs to Outputs through the circuit(s). Final output values are displayed in the blue square Output boxes. Only circuit(s) that are fully connected (no dangling wires / incompletely connected circuit elements (e.g., gate with missing input connection)) may be evaluated, or else an error message will be displayed.

Clear All Button This button clears the workspace of all circuit elements. The user is prompted to confirm that this action is desired before proceeding.