Exercises

  1. Take the MyFrame program that demonstrates the thread bug and update it to reproduce the race condition more easily. Change the arithmetic that updates variable i into two statements, and put a sleep() or a yield() statement between them. Observe the failure.

  2. Update the MyFrame program to remove the race condition by making the reading and writing of the variables mutually exclude each other. Test your code by running it on a version of the program that quickly reproduces the race condition failure.

  3. Write a small Java program to capture the screen, display it in a scroll panel inside a frame, and allow the user to trim its size. Write the cropped region to a file on request. Allow the user to choose any output file format that the Image I/O library on that platform supports.

  4. Write a program that uses Image I/O to display a thumbnail display of all the GIFs, JPEGs, and PNG files in a directory. Let the user select one, and display that full size in a new window. Allow the user to change the size of it, crop a region, and save it in a new format. That is the beginning of a general-purpose Java image editing application. You can take it as far as your interests run.

  5. The following code will grab a screen image.

    Robot ro = new Robot();
    Toolkit t = Toolkit.getDefaultToolkit();
    final Dimension d = t.getScreenSize();
    Rectangle re = new Rectangle(d.width, d.height);
    final Image image = ro.createScreenCapture( re );

    Write code to display it half size, and allow the user to crop the image. Use the class javax.imageio.ImageIO to write the image out as a JPEG file. Refer to the javadoc to see the write() methods.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.21.159.82