7.9 Problems

 < Day Day Up > 



7.9 Problems

  1. Use the animator of Program7.5 (Exhibits 12 through 15) to animate an image such as a ".tiff" file. To do this, get an instance of the Toolkit and use the getImage method of the Toolkit class to read the image from a file. Use the Graphic drawImage method to draw it to the screen.

  2. Use the Generic Animator of Program7.5 (Exhibits 12 through 15) to draw an image of a person. Create the impression of a person walking by cycling through images of a person in different stages of walking.

  3. Change the MoveBall program so that the two balls bounce off each other when they meet. This would cause Ball 1 to stay in the upper left of the screen and Ball 2 to stay in the lower right. Add more balls and have them interact. You do not need to (and should not) modify the Animator to do this.

  4. Prove by induction that the interactions between the buttons in the ControlFrame and the Animator control loop are safe. What anomalies in the behavior exist? Do you think they would affect the animator?

  5. Modify the animator component so that if either the control frame or animation frame is closed the program exits.

  6. Can the paint method in the animator ever be running twice at the same time? Why or why not?

  7. Write a program that creates two different animators to animate the balls in different frames. Are these two animations concurrent?

  8. Write a program that puts five balls on the screen and moves them.

  9. Extend the Path class so that it will handle Bessel functions and Spline Curves.

  10. Explain why a race condition exists in Exhibit 5 (Program7.4).

  11. Explain why synchronizing the add<Listener>, remove<Listener>, and processEvent methods to remove the race condition can result in deadlock.

  12. The collection classes that were introduced in JDK1.2 (the classes that implement java.util.Collection) are specifically not synchronized because the synchronization of such classes as Vector does not make them thread safe and adds overhead. Explain why a Vector is not thread safe and account for the extra overhead generated by its use in the examples in this chapter, as compared to an unsynchronized collection.

  13. Explain why the AWTMulticaster was used in the Java AWT and not the method of cloning the vector as in Program7.5 (Exhibits 12 through 15). Should a Multicaster always be used?

  14. Is there any difference between using an Enumeration and an Iterator in Program7.3 (Exhibits 6 through 8)? If so, how and when would the difference become apparent?

  15. Does it matter where the sleep method is called in the paint method of the SimpleAnimator in Program7.1 (Exhibits 1 through 3)? Why or why not?

  16. Using the immutable object of Listeners in the processEvent method, can a Listener receive an event after it has successfully called remove<Listener> in the event source? How?

  17. The following program is incorrect because a thread can be running in the object before the constructor has completed. Why does this occur? Change this program to correct this problem.

       import java.awt.*;   public class MyClass extends Frame {     public MyClass() {       setSize(300,300);       setVisible(true);     }     public static void main(String args[]) {       MyClass mc = new MyClass();     }   } 

  18. Create a race condition in a constructor by starting a thread in that constructor and then allocating an instance variable for that object in the run method and the constructor. What does this say about starting threads in a constructor?

  19. Replace the "startAnimator" method with a "setVisible" method. This method should make the animator and control panel visible or hidden. When it is hidden, the control thread should stop running.

  20. Using the class in Exhibit 18 and the documentation in the AWTEventMulticaster API, discuss in detail the methods that are in class Component. How is a button implemented? Implement your own Button class.

    Exhibit 18: Program for Problem 20

    start example

     import java.awt.*; public class MyComponent extends Component {   // contains checks to see if the mouse is in the current   // component. If it is, the event is sent to the component.   // Otherwise the event is not sent to the component.   public boolean contains(int x, int y) {     System.out.println("In contains "+ x + ""+ y);     // return false;// If false is returned, the            // processEvent is never called.     return true;//Tell the container to send events to            // this Component.   }   // processEvent is called when an event occurs. This is what   // causes the Event Source to call all its listeners.   public void processEvent(AWTEvent e) {     System.out.println("In Event");   }   // Paint is always called on a repaint event. This is where   // the Component is drawn.   public void paint(Graphics g) {     System.out.println("In Paint");   }   public static void main(String args[]) {     MyComponent my_c1 = new MyComponent();     // Set the event mask to some event; otherwise, processEvent     // is never called.     my_c1.enableEvents(AWTEvent.MOUSE_EVENT_MASK);     // Put it all together in a Frame.     Frame f1 = new Frame();     f1.add(my_c1);     f1.setSize(200,200);     f1.show();   } } 

    end example



 < Day Day Up > 



Creating Components. Object Oriented, Concurrent, and Distributed Computing in Java
The .NET Developers Guide to Directory Services Programming
ISBN: 849314992
EAN: 2147483647
Year: 2003
Pages: 162

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net