Chapter 12 Graphical User Interface Components: Part 1 (Web hosting faq)
Chapter 12 Graphical User Interface Components: Part 1 719 12.23 Modify your solution to Exercise 12.17 to enable the user to select a font and a font size, then type text into a JTextField. When the user presses Enter, the text should be displayed on the background in the chosen font and size. Modify the program further to allow the user to specify the exact position at which the text should be displayed. 12.24 Write a program that allows the user to select a shape from a JComboBox, then draws that shape 20 times with random locations and dimensions in method paint. The first item in the JComboBox should be the default shape that is displayed the first time paint is called. 12.25 Modify Exercise 12.24 to draw each of the 20 randomly sized shapes in a randomly selected color. Use all 13 predefined Color objects in an array of Colors. 12.26 Modify Exercise 12.25 to allow the user to select the color in which shapes should be drawn from a JColorChooserdialog. 12.27 Write a program using methods from interface MouseListener that allows the user to press the mouse button, drag the mouse and release the mouse button. When the mouse is released, draw a rectangle with the appropriate upper-left corner, width and height. (Hint: The mouse- Pressed method should capture the set of coordinates at which the user presses and holds the mouse button initially, and the mouseReleased method should capture the set of coordinates at which the user releases the mouse button. Both methods should store the appropriate coordinate values. All calculations of the width, height and upper-left corner should be performed by the paint method before the shape is drawn.) 12.28 Modify Exercise 12.27 to provided a rubber-banding effect. As the user drags the mouse, the user should be able to see the current size of the rectangle to know exactly what the rectangle will look like when the mouse button is released. (Hint: Method mouseDragged should perform the same tasks as mouseReleased.) 12.29 Modify Exercise 12.28 to allow the user to select which shape to draw. A JComboBox should provide options including at least rectangle, oval, line and rounded rectangle. 12.30 Modify Exercise 12.29 to allow the user to select the drawing color from a JColor- Chooser dialog box. 12.31 Modify Exercise 12.30 to allow the user to specify whether a shape should be filled or empty when it is drawn. The user should click a JCheckBox to indicate filled or empty. 12.32 (Painting program) Using the techniques of Exercise 9.28 Exercise 9.29 and Exercise 12.27 Exercise 12.30 and the graphics techniques of Chapter 11, rewrite Exercise 12.31 to allow the user to draw multiple shapes and store each shape in an array of shapes. (If you feel ambitious, investigate the capabilities of class Vector in Chapter 20.) For this program, create your own classes (like those in the class hierarchy described in Exercise 9.28 Exercise 9.29) from which objects will be created to store each shape the user draws. The classes should store the location, dimensions and color of each shape and should indicate whether the shape is filled or unfilled. Your classes should all derive from a class called MyShape that has all the common features of every shape type. Every subclass of My- Shape should have its own method draw, which returns void and receives a Graphics object as its argument. When the application window s paint method is called, it should walk through the array of shapes and display each shape by polymorphically calling the shape s draw method (passing the Graphics object as an argument). Each shape s draw method should know how to draw the shape. As a minimum, your program should provide the following classes: MyLine, MyOval, MyRect, MyRoundRect. Design the class hierarchy for maximum software reuse, and place all your classes in the package shapes. Import this package into your program. 12.33 Modify Exercise 12.32 to provide an Undo button that can be used repeatedly to undo the last painting operation. If there are no shapes in the array of shapes, the Undo button should be disabled. Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01
In case you need affordable webhost to host your website, our recommendation is ecommerce web host services.