GUI Midterm

Spring 2013

  1. This exam is open-note, open book.
  2. You must answer all of the questions.
  3. You must use Java to implement the coding questions.
  4. Good luck!

  1. (15 points) Each of the following questions asks you to collect some type of data or provide some type of operation to the user. For each question, select the most appropriate widget to use. You should select each answer from the following list of widgets:

    1. ________________ You want the user to enter a social security number.

    2. ________________ You want the user to select whethery they want to book a hotel, car, or airline and they are only allowed to select one of the three choices.

    3. ________________ You want the user to select the square footage of their house. The choices are in increments of 100, and range from 500-5000.

    4. ________________ You want the user to select the state in which they live.

    5. ________________ You want to display four icons representing the cut, copy, paste, and delete operations (i.e., selecting the icon causes the associated operation to be performed).

  2. (40 points) Short Answer: Answer each of the following questions in two sentences or less:

    1. Why is it hard to vertically center text in a graphical interface written in Java?
    2. Why are sans-serif fonts preferred for computer displays?
    3. Why is the MVC model considered so important for gui implementations?
    4. Give two reasons why the number of radio buttons or check boxes in a group should be limited to 5-7 items.
    5. What two problems does the delegation event handling model solve that exist in the listener event handling model? For this question only you may use 2 sentences for each of your reasons.
    6. Give two reasons why absolute positioning of objects is a poor idea in most interfaces.
    7. When should you use composition and when should you use inheritance?
    8. Why could using objects to represent a graphical interface's shapes be better than directly drawing graphics to the screen using paintComponent? This question refers to the lectures on having a library of objects and adding the objects to container objects in order to get them to be displayed.

  3. (15 points) Choose the best answer from the following list of layout managers:
    Box Flow Grid Group GridBag Border Spring None
      
    1. ____________ I want to equally space a group of objects horizontally across the window. If the window is re-sized, I want the objects to continual to be equally spaced across the window.
    2. ____________ I want to design a new type of browser window that has toolbars on both the left and right sides of the window and I want the content displayed in the center of the window.
    3. ____________ I want to display a set of equal-sized color chips in a color pallette using a tabular format.
    4. ____________ I want to display a set of command buttons vertically at their natural size.
    5. ____________ I want to design a dialog box with a number of different groups of widgets. What should I use as the top-level layout manager if I am designing the dialog box programmatically (i.e., I am not using an IDE).

  4. (30 points) Finish the following class definition so that it implements the following graphical interface:
    1. When the user presses the mouse button, draw a blue rectangle that completely fills the JPanel and that has a 10 pixel black border (i.e., the rectangle has the black border).
    2. When the user releases the mouse button, draw a green rectangle that completely fills the JPanel and that has a 10 pixel black border (i.e., the rectangle has the black border).
    3. The JPanel should initially appear as a 100x100 pixel object but it can be resized.
    4. The JPanel should be added to the center of its JFrame's content pane.

    Here is a sample gui for the interface:

    When the mouse is pressed, a blue rectangle is drawn that completely fills the JPanel When the mouse is released, a green rectangle is drawn that completely fills the JPanel

    Here are some helpful hints:

    1. You can use the command "new BasicStroke(10)" to get a stroke object that is 10 pixels wide.
    2. Color.black, Color.blue, and Color.green are constants for the colors you need.
    3. You will need a constructor method, a paintComponent method, and a getPreferredSize method

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    class code extends JPanel {
    
        public static void main(String args[]) {
             java.awt.EventQueue.invokeLater(new Runnable() {
                  public void run() {
                      new code();
                 }
             });
        }
    
        JFrame window = new JFrame();
    
        // Your code goes here--You may need to declare additional instance
        // variables