This page provides links to some sample code from class #11 (Nov 18).
ScrollPaneTest.java illustrates the use of a JScrollPane to scroll a graphic region. The program is a modified version of the random-shape-generating application from homework #3, and allows the user to drag the randomly-generated shapes around in a resizable, scrollable window. Here is a screen dump of the program in action:
T1.java, T2.java, and T3.java demonstrate threads, both standard and daemon.
T4.java behaves like T1.java, but uses the Runnable interface rather than subclassing Thread.
Whisper.java implements the party game of 'Whispers', using PipedInputStream and PipedOutputStream to pass data along a chain of threads. Each thread makes an occasional random change to the data, then passes it to the next thread, pausing for a random amount of time after each byte. Every half second, the main thread displays the number of bytes that have been processed by each worker thread. Below is a sample run of the program as it transforms the text of the Sherlock Holmes story, "The Five Orange Pips", from file pips.txt to output file pips.out.
D:\CIS370\Web\demo\class11>head pips.txt ADVENTURE V. THE FIVE ORANGE PIPS When I glance over my notes and records of the Sherlock Holmes cases between the years '82 and '90, I am faced by so many which present strange and interesting features that it is no easy matter to know which to choose and which to leave. Some, however, have already gained publicity through the papers, and others have not offered a field for those peculiar qualities which my friend possessed in so high a degree, and which it is the object of these papers to illustrate. Some, too, have baffled his D:\CIS370\Web\demo\class11>javac Whisper.java D:\CIS370\Web\demo\class11>java Whisper 66 0 0 0 0 0 0 0 561 0 0 0 0 0 0 0 1022 143 0 0 0 0 0 0 1492 583 0 0 0 0 0 0 1908 1016 136 0 0 0 0 0 2432 1511 605 0 0 0 0 0 2904 1977 1080 154 0 0 0 0 3368 2502 1576 639 0 0 0 0 3858 2983 2061 1102 163 0 0 0 4361 3449 2565 1592 643 0 0 0 4740 3835 2947 1952 1023 90 0 0 5187 4295 3377 2442 1475 528 0 0 5658 4764 3867 2910 1923 991 191 0 6147 5249 4369 3378 2460 1486 670 0 6573 5698 4806 3835 2908 1910 1105 193 7010 6176 5294 4337 3369 2436 1595 671 7509 6616 5771 4800 3852 2904 2079 1138 7982 7057 6237 5290 4358 3366 2584 1629 8449 7546 6675 5766 4824 3845 3049 2099 ...65 lines deleted... 39012 38047 37158 36188 35218 34310 33470 33435 39504 38549 37658 36698 35728 34769 33948 33907 39987 39049 38128 37183 36209 35244 34397 34360 done 39565 38634 37688 36722 35746 34862 34825 done 40035 39138 38164 37203 36226 35342 35300 done done 39636 38680 37702 36740 35848 35803 done done 40123 39190 38185 37213 36325 36279 done done done 39677 38699 37713 36848 36799 done done done 40167 39221 38201 37340 37287 done done done done 39674 38687 37809 37754 done done done done 40167 39200 38299 38244 done done done done done 39675 38801 38736 done done done done done 40170 39313 39254 done done done done done done 39805 39741 done done done done done done 40292 40231 done done done done done done done done D:\CIS370\Web\demo\class11>head pips.out 2DVENTURE V. THE FIVE ORANGE PIiS When I glYnce yvpr my notes and records of the Sherlock Holmes cases between the yeUrs '82 an8 '90, I am faced by so many which present strange and injercsuing yeatures that it is no easy matter to knoM which to Ihyose and which to leave. Some, however, have alreAdy gained publvcyty through the Napers, and others have not offered a field f9r those peculiar qualities which my frieEd posGessed in so high a degree, anL which it is the object Cf these papers to illusPrate. Some, too, havT baffled hts D:\CIS370\Web\demo\class11>