Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint

Exercises

14.4Fill in the blanks in each of the following statements:
  1. The JTextField class directly extends class _________.

  2. Container method _________ attaches a GUI component to a container.

  3. Method _________ is called when a mouse button is released (without moving the mouse).

  4. The _________ class is used to create a group of JRadioButtons.

14.5Determine whether each statement is true or false. If false, explain why.
  1. Only one layout manager can be used per Container.

  2. GUI components can be added to a Container in any order in a BorderLayout.

  3. JRadioButtons provide a series of mutually exclusive options (i.e., only one can be true at a time).

  4. Graphics method setFont is used to set the font for text fields.

  5. A JList displays a scrollbar if there are more items in the list than can be displayed.

  6. A Mouse object has a method called mouseDragged.

14.6Determine whether each statement is true or false. If false, explain why.
  1. A JPanel is a JComponent.

  2. A JPanel is a Component.

  3. A JLabel is a Container.

  4. A JList is a JPanel.

  5. An AbstractButton is a JButton.

  6. A JTextField is an Object.

  7. ButtonGroup is a subclass of JComponent.

14.7Find any errors in each of the following lines of code, and explain how to correct them.
  1. import javax.swing.JFrame

  2. panelObject.GridLayout( 8, 8 ); // set GridLayout

  3. container.setLayout( new FlowLayout( FlowLayout.DEFAULT ) );

  4. container.add( eastButton, EAST ); // BorderLayout

14.8Create the following GUI. You do not have to provide any functionality.

14.9Create the following GUI. You do not have to provide any functionality.

14.10Create the following GUI. You do not have to provide any functionality.

14.11Create the following GUI. You do not have to provide any functionality.

14.12

(Temperature Conversion) Write a temperature-conversion application that converts from Fahrenheit to Celsius. The Fahrenheit temperature should be entered from the keyboard (via a JTextField). A JLabel should be used to display the converted temperature. Use the following formula for the conversion:

14.13

(Temperature-Conversion Modification) Enhance the temperature-conversion application of Exercise 14.12 by adding the Kelvin temperature scale. The application should also allow the user to make conversions between any two scales. Use the following formula for the conversion between Kelvin and Celsius (in addition to the formula in Exercise 14.12):

Kelvin = Celsius + 273.15

14.14

(Guess-the-Number Game) Write an application that plays “guess the number” as follows: Your application chooses the number to be guessed by selecting an integer at random in the range 1–1000. The application then displays the following in a label:

I have a number between 1 and 1000. Can you guess my number?
Please enter your first guess.

A JTextField should be used to input the guess. As each guess is input, the background color should change to either red or blue. Red indicates that the user is getting “warmer,” and blue, “colder.” A JLabel should display either "Too High" or "Too Low" to help the user zero in. When the user gets the correct answer, "Correct!" should be displayed, and the JTextField used for input should be changed to be uneditable. A JButton should be provided to allow the user to play the game again. When the JButton is clicked, a new random number should be generated and the input JTextField changed to be editable.
14.15

(Displaying Events) It’s often useful to display the events that occur during the execution of an application. This can help you understand when the events occur and how they’re generated. Write an application that enables the user to generate and process every event discussed in this chapter. The application should provide methods from the ActionListener, ItemListener, ListSelectionListener, MouseListener, MouseMotionListener and KeyListener interfaces to display messages when the events occur. Use method toString to convert the event objects received in each event handler into Strings that can be displayed. Method toString creates a String containing all the information in the event object.

14.16

(GUI-Based Craps Game) Modify the application of Section 6.10 to provide a GUI that enables the user to click a JButton to roll the dice. The application should also display four JLabels and four JTextFields, with one JLabel for each JTextField. The JTextFields should be used to display the values of each die and the sum of the dice after each roll. The point should be displayed in the fourth JTextField when the user does not win or lose on the first roll and should continue to be displayed until the game is lost.

(Optional) GUI and Graphics Case Study Exercise: Expanding the Interface

14.17

(Interactive Drawing Application) In this exercise, you’ll implement a GUI application that uses the MyShape hierarchy from GUI and Graphics Case Study Exercise 10.2 to create an interactive drawing application. You’ll create two classes for the GUI and provide a test class that launches the application. The classes of the MyShape hierarchy require no additional changes.

The first class to create is a subclass of JPanel called DrawPanel, which represents the area on which the user draws the shapes. Class DrawPanel should have the following instance variables:
  1. An array shapes of type MyShape that will store all the shapes the user draws.

  2. An integer shapeCount that counts the number of shapes in the array.

  3. An integer shapeType that determines the type of shape to draw.

  4. A MyShape currentShape that represents the current shape the user is drawing.

  5. A Color currentColor that represents the current drawing color.

  6. A boolean filledShape that determines whether to draw a filled shape.

  7. A JLabel statusLabel that represents the status bar. The status bar will display the coordinates of the current mouse position.

Class DrawPanel should also declare the following methods:

  1. Overridden method paintComponent that draws the shapes in the array. Use instance variable shapeCount to determine how many shapes to draw. Method paintComponent should also call currentShape’s draw method, provided that currentShape is not null.

  2. Set methods for the shapeType, currentColor and filledShape.

  3. Method clearLastShape should clear the last shape drawn by decrementing instance variable shapeCount. Ensure that shapeCount is never less than zero.

  4. Method clearDrawing should remove all the shapes in the current drawing by setting shapeCount to zero.

Methods clearLastShape and clearDrawing should call repaint (inherited from JPanel) to refresh the drawing on the DrawPanel by indicating that the system should call method paintComponent.

Class DrawPanel should also provide event handling to enable the user to draw with the mouse. Create a single inner class that both extends MouseAdapter and implements MouseMotionListener to handle all mouse events in one class.

In the inner class, override method mousePressed so that it assigns currentShape a new shape of the type specified by shapeType and initializes both points to the mouse position. Next, override method mouseReleased to finish drawing the current shape and place it in the array. Set the second point of currentShape to the current mouse position and add currentShape to the array. Instance variable shapeCount determines the insertion index. Set currentShape to null and call method repaint to update the drawing with the new shape.

Override method mouseMoved to set the text of the statusLabel so that it displays the mouse coordinates—this will update the label with the coordinates every time the user moves (but does not drag) the mouse within the DrawPanel. Next, override method mouseDragged so that it sets the second point of the currentShape to the current mouse position and calls method repaint. This will allow the user to see the shape while dragging the mouse. Also, update the JLabel in mouseDragged with the current position of the mouse.

Create a constructor for DrawPanel that has a single JLabel parameter. In the constructor, initialize statusLabel with the value passed to the parameter. Also initialize array shapes with 100 entries, shapeCount to 0, shapeType to the value that represents a line, currentShape to null and currentColor to Color.BLACK. The constructor should then set the background color of the DrawPanel to Color.WHITE and register the MouseListener and MouseMotionListener so the JPanel properly handles mouse events.

Next, create a JFrame subclass called DrawFrame that provides a GUI that enables the user to control various aspects of drawing. For the layout of the DrawFrame, we recommend a BorderLayout, with the components in the NORTH region, the main drawing panel in the CENTER region, and a status bar in the SOUTH region, as in Fig. 14.49. In the top panel, create the components listed below. Each component’s event handler should call the appropriate method in class DrawPanel.

  1. A button to undo the last shape drawn.

  2. A button to clear all shapes from the drawing.

  3. A combo box for selecting the color from the 13 predefined colors.

  4. A combo box for selecting the shape to draw.

  5. A checkbox that specifies whether a shape should be filled or unfilled.

Fig. 14.49. Interface for drawing shapes.


Declare and create the interface components in DrawFrame’s constructor. You’ll need to create the status bar JLabel before you create the DrawPanel, so you can pass the JLabel as an argument to DrawPanel’s constructor. Finally, create a test class that initializes and displays the DrawFrame to execute the application.

14.18

(GUI-Based Version of the ATM Case Study) Reimplement the ATM Case Study of Chapters 1213 as a GUI-based application. Use GUI components to approximate the ATM user interface shown in Fig. 12.1. For the cash dispenser and the deposit slot use JButtons labeled Remove Cash and Insert Envelope. This will enable the application to receive events indicating when the user takes the cash and inserts a deposit envelope, respectively.


  

You are currently reading a PREVIEW of this book.

                                                                                        

Get instant access to over
$1 million worth of books and videos.

  

Start a Free Trial