Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

JAVA PROGRAMMING I. Let there be a base class named TestProxy. Let it be a norma

ID: 3829123 • Letter: J

Question

JAVA PROGRAMMING

I. Let there be a base class named TestProxy. Let it be a normal, concrete class with a main() method. Inside the main() method, you should do the following:

A. Construct an instance of JFrame.

B. Construct an instance of MyEditorPaneProxy (see below) with two construction parameters, a String containing this URL, “http://ic.payap.ac.th/” and a reference to the frame.

C. Add the pane proxy object to a JScrollPane object and add this to the frame.

D. Set the frame size to something suitable and set the frame to be visible.

E. For the purposes of effective testing, insert a Thread.sleep() call into your code.

F. Call the load() method (see below) on the pane proxy object.

II. Let there be a proxy class named MyEditorPaneProxy. Let it be a normal, concrete class with these characteristics:

A. It extends JEditorPane.

B. It implements Runnable.

C. It has two instance variables, String URLPath and JFrame myFrameReference.

- It has a constructor with these characteristics:

- It takes parameters for the two instance variables.

- It sets the values of the parameters.

- It then sets its page to “https://www.yahoo.com”.

- It then repaints the frame.

D. The class has a load() method with these characteristics:

- It sets the page to “http://uaa.alaska.edu”.

- It then repaints the frame.

- It includes a time delay.

- It then creates a thread based on the pane and calls start() on it.

E. The class has a run() method with these characteristics:

- It sets the page to the URLPath value that was passed in when the pane object was constructed.

- It then repaints the frame.

Black box testing and observable results: If you get the mess above to work, what will it do; what will you see? First it should bring up the Yahoo Web page. This is the equivalent of “Absent” in the book. Next it should bring up the UAA Web page. This is the equivalent of “Loading” in the book. Finally, it should bring up the Payap University Web page. Remember, the JEditorPane displays Web pages poorly, so the contents may not be recognizable. But it should be recognizable that different things are being shown in sequence. Also remember that you will probably need to put in calls to sleep() so that your life doesn’t flash before your eyes too quickly to see it. A successful implementation will make it clear that you’re sequencing through three different Web pages, even if the display of the Web pages is so poor that you can’t tell exactly what they are. If the delays are long enough, there might be time to use the scroll bars to look at some content and verify which page you’re looking at at any given time.

Explanation / Answer

import java.io.IOException;
import javax.swing.JFrame;
import javax.swing.JScrollPane;

public class TestProxy extends JFrame{
public static void main(String args[]) throws IOException, InterruptedException{
JFrame f=new JFrame();
MyEditorPaneProxy me=new MyEditorPaneProxy("http://ic.payap.ac.th",new JFrame());
JScrollPane jsp=new JScrollPane(me);
f.add(jsp);
f.setSize(1000,700);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JEditorPane;
import javax.swing.JFrame;


public class MyEditorPaneProxy extends JEditorPane implements Runnable{
String URLPath;
JFrame myFrameReference;
Thread th;
public MyEditorPaneProxy(String url,JFrame jf) throws IOException, InterruptedException{
URLPath=url;
myFrameReference=jf;
setPage("https://www.yahoo.com");
myFrameReference.repaint();
th=new Thread();
th.start();
}
public void load() throws IOException, InterruptedException{
setPage("http://uaa.alaska.edu");
myFrameReference.repaint();
}

@Override
public void run() {
try {
Thread.sleep(5000);
load();
Thread.sleep(5000);

setPage(URLPath);
  
myFrameReference.repaint();
} catch (IOException ex) {
Logger.getLogger(MyEditorPaneProxy.class.getName()).log(Level.SEVERE, null, ex);
} catch (InterruptedException ex) {
Logger.getLogger(MyEditorPaneProxy.class.getName()).log(Level.SEVERE, null, ex);
}
}
  
}