Workshop: Revolving Links





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.

Listing 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://www.cadenhead.org/workbench");

29:         Button goButton = new Button("Go");

30:         goButton.addActionListener(this);

31:         FlowLayout flow = new FlowLayout();

32:         setLayout(flow);

33:         add(goButton);

34:     }

35:

36:     URL getURL(String urlText) {

37:         URL pageURL = null;

38:         try {

39:             pageURL = new URL(getDocumentBase(), urlText);

40:         } catch (MalformedURLException m) { }

41:         return pageURL;

42:     }

43:

44:     public void paint(Graphics screen) {

45:         Graphics2D screen2D = (Graphics2D) screen;

46:         screen2D.setColor(butterscotch);

47:         screen2D.fillRect(0, 0, getSize().width, getSize().height);

48:         screen2D.setColor(Color.black);

49:         screen2D.drawString(pageTitle[current], 5, 60);

50:         screen2D.drawString("" + pageLink[current], 5, 80);

51:     }

52:

53:     public void start() {

54:         if (runner == null) {

55:             runner = new Thread(this);

56:             runner.start();

57:         }

58:     }

59:

60:     public void run() {

61:         Thread thisThread = Thread.currentThread();

62:         while (runner == thisThread) {

63:             current++;

64:             if (current > 5) {

65:                 current = 0;

66:             }

67:             repaint();

68:             try {

69:                 Thread.sleep(10000);

70:             } catch (InterruptedException e) {

71:                 // do nothing

72:             }

73:         }

74:     }

75:

76:     public void stop() {

77:         if (runner != null) {

78:             runner = null;

79:         }

80:     }

81:

82:     public void actionPerformed(ActionEvent evt) {

83:         if (runner != null) {

84:             runner = null;

85:         }

86:         AppletContext browser = getAppletContext();

87:         if (pageLink[current] != null) {

88:             browser.showDocument(pageLink[current]);

89:         }

90:     }


After you compile this program with the javac compiler tool, 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.

Listing 19.3. The Full Text of Revolve.html

1: <applet code="Revolve.class" width="300" 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 shows what the applet looks like in Mozilla Firefox.

2. Displaying revolving links in an applet window.



     Python   SQL   Java   php   Perl 
     game development   web development   internet   *nix   graphics   hardware 
     telecommunications   C++ 
     Flash   Active Directory   Windows