Sams Teach Yourself Java 2 in 24 Hours
- Table of Contents
- Copyright
- About the Author
- About the Technical Editor
- Acknowledgments
- We Want to Hear from You!
- Reader Services
- Introduction
- Hour 1. Becoming a Programmer
- Hour 2. Writing Your First Program
- Hour 3. Vacationing in Java
- Hour 4. Understanding How Java Programs Work
- Part II: Learning the Basics of Programming
- Hour 5. Storing and Changing Information in a Program
- Hour 6. Using Strings to Communicate
- Hour 7. Using Conditional Tests to Make Decisions
- Hour 8. Repeating an Action with Loops
- Part III: Working with Information in New Ways
- Hour 9. Storing Information with Arrays
- Hour 10. Creating Your First Object
- Hour 11. Describing What Your Object Is Like
- Hour 12. Making the Most of Existing Objects
- Part IV: Programming a Graphical User Interface
- Hour 13. Building a Simple User Interface
- Hour 14. Laying Out a User Interface
- Hour 15. Responding to User Input
- Hour 16. Building a Complex User Interface
- Part V: Creating Multimedia Programs
- Hour 17. Creating Interactive Web Programs
- Hour 18. Handling Errors in a Program
- Hour 19. Creating a Threaded Program
- Hour 20. Reading and Writing Files
- Part VI: Creating Multimedia Programs
- Hour 21. Using Fonts and Color
- Hour 22. Playing Sound Files
- Hour 23. Working with Graphics
- Hour 24. Creating Animation
- Part VII: Appendixes
- Appendix A. Tackling New Features of Java 2 Version 1.4
- Appendix B. Using the Java 2 Software Development Kit
- Appendix C. Programming with the Java 2 Software Development Kit
- Appendix D. Using Sun ONE Studio
- Appendix E. Where to Go from Here: Java Resources
- Appendix F. This Book's Web Site
Workshop: Revolving Links
Now that all aspects of the Revolve applet have been described, you're ready to create the program and test it. Run your word processor and create a new file called Revolve.java. Enter the text of Listing 19.2 and save the file when you're done.
Example 19.2. The Full Text of Revolve.java
1: import java.applet.*;
2: import java.awt.*;
3: import java.awt.event.*;
4: import javax.swing.*;
5: import java.net.*;
6:
7: public class Revolve extends JApplet
8: implements Runnable, ActionListener {
9:
10: String[] pageTitle = new String[6];
11: URL[] pageLink = new URL[6];
12: Color butterscotch = new Color(255, 204, 158);
13: int current = 0;
14: Thread runner;
15:
16: public void init() {
17: pageTitle[0] = "Sun's Java site";
18: pageLink[0] = getURL("http://java.sun.com");
19: pageTitle[1] = "Cafe au Lait";
20: pageLink[1] = getURL("http://www.ibiblio.org/javafaq/");
21: pageTitle[2] = "JavaWorld";
22: pageLink[2] = getURL("http://www.javaworld.com");
23: pageTitle[3] = "Java 2 in 24 Hours";
24: pageLink[3] = getURL("http://www.java24hours.com");
25: pageTitle[4] = "Sams Publishing";
26: pageLink[4] = getURL("http://www.samspublishing.com");
27: pageTitle[5] = "Workbench";
28: pageLink[5] = getURL("http://workbench.cadenhead.info");
29: Button goButton = new Button("Go");
30: goButton.addActionListener(this);
31: FlowLayout flow = new FlowLayout();
32: Container pane = getContentPane();
33: pane.setLayout(flow);
34: pane.add(goButton);
35: setContentPane(pane);
36: }
37:
38: URL getURL(String urlText) {
39: URL pageURL = null;
40: try {
41: pageURL = new URL(getDocumentBase(), urlText);
42: } catch (MalformedURLException m) { }
43: return pageURL;
44: }
45:
46: public void paint(Graphics screen) {
47: Graphics2D screen2D = (Graphics2D) screen;
48: screen2D.setColor(butterscotch);
49: screen2D.fillRect(0, 0, getSize().width, getSize().height);
50: screen2D.setColor(Color.black);
51: screen2D.drawString(pageTitle[current], 5, 60);
52: screen2D.drawString("" + pageLink[current], 5, 80);
53: }
54:
55: public void start() {
56: if (runner == null) {
57: runner = new Thread(this);
58: runner.start();
59: }
60: }
61:
62: public void run() {
63: Thread thisThread = Thread.currentThread();
64: while (runner == thisThread) {
65: repaint();
66: current++;
67: if (current > 5)
68: current = 0;
69: try {
70: Thread.sleep(10000);
71: } catch (InterruptedException e) { }
72: }
73: }
74:
75: public void stop() {
76: if (runner != null) {
77: runner = null;
78: }
79: }
80:
81: public void actionPerformed(ActionEvent evt) {
82: if (runner != null) {
83: runner = null;
84: }
85: AppletContext browser = getAppletContext();
86: if (pageLink[current] != null)
87: browser.showDocument(pageLink[current]);
88: }
89: }
After you compile this program with the javac or another compiler, you need to create a Web page on which to put the applet. Create a new file with your word processor and name it Revolve.html. Enter Listing 19.3 and save the file.
Example 19.3. The Full Text of Revolve.html
1: <applet code="Revolve.class" width=200 height=100> 2: </applet>
When you're done, you can load this file in appletviewer to see how the different links are displayed. You won't be able to use the Go button to visit any of the links, however, because that isn't supported in appletviewer.
Figure 19.2 shows what the applet looks like in Internet Explorer 6.
Figure 19.2 Displaying revolving links in an applet window.
Summary | Next Section

Account Sign In
View your cart