In: Computer Science
3.) Analyze the Java Program LinkRotator
_______________________________________________________________
Below is the "LinkRotator" java program:
1: package com.java24hours;
2:
3: import java.awt.*;
4: import java.awt.event.*;
5: import java.io.*;
6: import javax.swing.*;
7: import java.net.*;
8:
9: public class LinkRotator extends JFrame
10: implements Runnable, ActionListener {
11:
12: String[] pageTitle = new String[6];
13: URI[] pageLink = new URI[6];
14: int current = 0;
15: Thread runner;
16: JLabel siteLabel = new JLabel();
17:
18: public LinkRotator() {
19: setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
20: setSize(300, 100);
21: FlowLayout flo = new FlowLayout();
22: setLayout(flo);
23: add(siteLabel);
24: pageTitle = new String[] {
25: "Oracle's Java site",
26: "Server Side",
27: "JavaWorld",
28: "Java in 24 Hours",
29: "Sams Publishing",
30: "Workbench"
31: };
32: pageLink[0] =
getURI("http://www.oracle.com/technetwork/java");
33: pageLink[1] = getURI("http://www.theserverside.com");
34: pageLink[2] = getURI("http://www.javaworld.com");
35: pageLink[3] = getURI("http://www.java24hours.com");
36: pageLink[4] = getURI("http://www.samspublishing.com");
37: pageLink[5] = getURI("http://workbench.cadenhead.org");
38: Button visitButton = new Button("Visit Site");
39: visitButton.addActionListener(this);
40: add(visitButton);
41: setVisible(true);
42: start();
43: }
44:
45: private URI getURI(String urlText) {
46: URI pageURI = null;
47: try {
48: pageURI = new URI(urlText);
49: } catch (URISyntaxException ex) {
50: // do nothing
51: }
52: return pageURI;
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: current++;
66: if (current > 5) {
67: current = 0;
68: }
69: siteLabel.setText(pageTitle[current]);
70: repaint();
71: try {
72: Thread.sleep(2000);
73: } catch (InterruptedException exc) {
74: // do nothing
75: }
76: }
77: }
78:
79: public void actionPerformed(ActionEvent event) {
80: Desktop desktop = Desktop.getDesktop();
81: if (pageLink[current] != null) {
82: try {
83: desktop.browse(pageLink[current]);
84: runner = null;
85: System.exit(0);
86: } catch (IOException exc) {
87: // do nothing
88: }
89: }
90: }
91:
92: public static void main(String[] arguments) {
93: new LinkRotator();
94: }
95: }
PLEASE GIVE IT A THUMBS UP, I SERIOUSLY NEED ONE, IF YOU
NEED ANY MODIFICATION THEN LET ME KNOW, I WILL DO IT FOR
YOU
RUN the code and add the screenshot of the window and output.
Output:
Describe what code change you would make if you wanted each link to be displayed for 2.5 seconds.
To display for 2.5 seconds set Thread.sleep in run() method to 2500
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Code:
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import javax.swing.*;
class LinkRotator extends JFrame implements Runnable, ActionListener {
String[] pageTitle = new String[6];
URI[] pageLink = new URI[6];
int current = 0;
Thread runner;
JLabel siteLabel = new JLabel();
public LinkRotator() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(300, 100);
FlowLayout flo = new FlowLayout();
setLayout(flo);
add(siteLabel);
pageTitle =
new String[] {
"Oracle's Java site",
"Server Side",
"JavaWorld",
"Java in 24 Hours",
"Sams Publishing",
"Workbench",
};
pageLink[0] = getURI("http://www.oracle.com/technetwork/java");
pageLink[1] = getURI("http://www.theserverside.com");
pageLink[2] = getURI("http://www.javaworld.com");
pageLink[3] = getURI("http://www.java24hours.com");
pageLink[4] = getURI("http://www.samspublishing.com");
pageLink[5] = getURI("http://workbench.cadenhead.org");
Button visitButton = new Button("Visit Site");
visitButton.addActionListener(this);
add(visitButton);
setVisible(true);
start();
}
private URI getURI(String urlText) {
URI pageURI = null;
try {
pageURI = new URI(urlText);
} catch (URISyntaxException ex) {
// do nothing
}
return pageURI;
}
public void start() {
if (runner == null) {
runner = new Thread(this);
runner.start();
}
}
public void run() {
Thread thisThread = Thread.currentThread();
while (runner == thisThread) {
current++;
if (current > 5) {
current = 0;
}
siteLabel.setText(pageTitle[current]);
repaint();
try {
Thread.sleep(2500);
} catch (InterruptedException exc) {
// do nothing
}
}
}
public void actionPerformed(ActionEvent event) {
Desktop desktop = Desktop.getDesktop();
if (pageLink[current] != null) {
try {
desktop.browse(pageLink[current]);
runner = null;
System.exit(0);
} catch (IOException exc) {
// do nothing
}
}
}
public static void main(String[] arguments) {
new LinkRotator();
}
}