Clay Shell Version 3

Version 3 of the Clay Shell program processes command line arguments. It allows you to specify the height and width of the frame, and permits you some control over the background color.

Demos

java -jar ".../Clay3.jar" -s small


java -jar ".../Clay3.jar" -w 400 -h 200 -c red


java -jar ".../Clay3.jar" -c random -s medium


moxie> java -jar ".../Clay3.jar" -c yellow
Java Accessibility Bridge for GNOME loaded.

### Bad command line argument for -c: value = yellow


moxie> java -jar ".../Clay3.jar" -w
Java Accessibility Bridge for GNOME loaded.

### Invalid command line arguments.


moxie> java -jar ".../Clay3.jar" -s 200 500
Java Accessibility Bridge for GNOME loaded.

### Invalid command line arguments.


moxie> java -jar ".../Clay3.jar" -w infinity
Java Accessibility Bridge for GNOME loaded.

### Bad command line argument for -w: value = infinity


java.lang.NumberFormatException: For input string: "infinity"
moxie>

Code

package clay3;

import java.awt.*;

public class Main {
    
    public Main() {
    }
    
    public static void main(String[] args) {
        processCommandLineArguments(args);
        ClayFrame frame = new ClayFrame("Clay",background,size);
    }
    
    static private Dimension size; 
    static private Color background; 

    static private Dimension screenSize() {
        Toolkit toolkit = Toolkit.getDefaultToolkit();
        return toolkit.getScreenSize();
    }

    /** Establishes the size and background values */
    static private void processCommandLineArguments(String[] args) {
        // set default screen size and color
        size = screenSize();
        size.height = size.height - 35;
        background = Color.cyan;
        if ( args.length == 0 ) {
            // leave with default values in place
        } else if ( args.length == 2 ) {
            processCommandLineArgumentPair(args[0],args[1]);
        } else if ( args.length == 4 ) {
            processCommandLineArgumentPair(args[0],args[1]);
            processCommandLineArgumentPair(args[2],args[3]);
        } else if ( args.length == 6 ) {
            processCommandLineArgumentPair(args[0],args[1]);
            processCommandLineArgumentPair(args[2],args[3]);
            processCommandLineArgumentPair(args[4],args[5]);
        } else {
            System.out.println("### Invalid command line arguments.");
            System.exit(-1);
        }
    }

    static private void processCommandLineArgumentPair(String flag, String value) {
        if ( flag.equalsIgnoreCase("-s") ) {
            if ( value.equalsIgnoreCase("small") ) {
                size.height = 400;
                size.width = 300;
            } else if ( value.equalsIgnoreCase("medium") ) {
                size.height = 600;
                size.width = 500;
            } else if ( value.equalsIgnoreCase("large") ) {
                size.height = 800;
                size.width = 700;
            } else if ( value.equalsIgnoreCase("screen") ) {
                size = screenSize();
                size.height = size.height - 35;
            } else {
                System.out.println("### Bad command line argument for -s: ");
                System.out.println("value = " + value);
                System.exit(-1);
            }
        } else if ( flag.equalsIgnoreCase("-h") ) {
            try {
                Integer heightInteger = new Integer(value);
                int height = heightInteger.intValue();
                size.height = height;
            } catch ( Exception e ) {
                System.out.println("### Bad command line argument for -h: ");
                System.out.println("value = " + value);
                System.out.println(e);
                System.exit(-1);
            }
        } else if ( flag.equalsIgnoreCase("-w") ) {
            try {
                Integer widthInteger = new Integer(value);
                int width = widthInteger.intValue();
                size.width = width;
            } catch ( Exception e ) {
                System.out.println("### Bad command line argument for -w: ");
                System.out.println("value = " + value);
                System.out.println(e);
                System.exit(-1);
            }
        } else if ( flag.equalsIgnoreCase("-c") ) {
            if ( value.equalsIgnoreCase("red") ) {
                background = new Color(255,0,0);
            } else if ( value.equalsIgnoreCase("green") ) {
                background = new Color(0,255,0);
            } else if ( value.equalsIgnoreCase("blue") ) {
                background = new Color(0,0,255);
            } else if ( value.equalsIgnoreCase("random") ) {
                int r = (int)(Math.random()*256);
                int g = (int)(Math.random()*256);
                int b = (int)(Math.random()*256);
                background = new Color(r,g,b);
            } else {
                System.out.print("### Bad command line argument for -c: ");
                System.out.println("value = " + value);
                System.exit(-1);
            }
        }
    }

}

class ClayFrame extends Frame {
    
    public ClayFrame(String title, Color color, Dimension size) {
        super(title);
        setBackground(color);
        setSize(size.width,size.height);
        setVisible(true);
    }

}

Questions

  1. What can you infer about the Dimension class by inspecting this program?
  2. What can you infer about the Toolkit class by inspecting this program?
  3. What methods associated with the Frame class are used called in this program?
  4. How many parameters does the the ClayFrame constructor take in this program? What are the types of the parameters?
  5. Are the variables called size and background local to the methods of the main class or global to the methods of the main class?
  6. Are the variables called size and background local to the main class or global to the main class?
  7. What is an interpreter ?
  8. Is it reasonable to think of the command line argument processing method be viewed as an interpreter?
  9. Describe the Clay command line command "c". (Read the code.)
  10. Describe the Clay command line command "s". (Read the code.)
  11. Describe the Clay command line command "w". (Read the code.)
  12. Describe the Clay command line command "h". (Read the code.)