Sams Teach Yourself Java 2 in 24 Hours

Sams Teach Yourself Java 2 in 24 Hours

By Rogers Cadenhead

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.

19fig02.gif

Figure 19.2 Displaying revolving links in an applet window.

Share ThisShare This

Informit Network