import java.net.URL;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.io.FileNotFoundException;
import javax.swing.*;
import javax.swing.BorderFactory;
import javax.swing.border.EtchedBorder;
import javax.swing.text.html.HTMLDocument;
import javax.swing.text.html.HTMLEditorKit;
import javax.swing.text.html.StyleSheet;
public class htmlviewer implements ActionListener, ComponentListener {
public JPanel theContentPane;
public JEditorPane htmlPanel;
public JEditorPane webPanel;
public HTMLEditorKit htmlEditorKit;
public HTMLDocument htmlDocument;
JFrame frame;
String thefilename;
String thefilepath;
String thehelpURL = "";
JButton pressme, pressme1;
public static void main(String[] args) {
new htmlviewer().run();
}
public void run() {
frame = new JFrame("HTML Viewer");
frame.setResizable(true);
frame.setBackground(Color.YELLOW);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
///////////////////////////
webPanel = new JEditorPane();
//webPanel.setEditable(false);
webPanel.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.RAISED, Color.BLACK, Color.LIGHT_GRAY));
webPanel.setContentType("text/html;charset=\"UTF-8\"");
webPanel.setBackground(Color.WHITE);
webPanel.setOpaque(true);
webPanel.setPreferredSize(new Dimension(300, 400));
///////////////////////////////////
htmlPanel = new JEditorPane();
htmlPanel.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.RAISED, Color.BLACK, Color.LIGHT_GRAY));
htmlPanel.setContentType("text/html;charset=\"UTF-8\"");
htmlPanel.setBackground(Color.WHITE);
htmlPanel.setOpaque(true);
htmlPanel.setPreferredSize(new Dimension(300, 400));
htmlEditorKit = new HTMLEditorKit();
htmlDocument = (HTMLDocument) htmlEditorKit.createDefaultDocument();
theContentPane = new JPanel(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
theContentPane.setOpaque(true);
theContentPane.add(htmlPanel);
pressme = new JButton("Change_HTML");
pressme.addActionListener(this);
pressme.setActionCommand("A");
pressme1 = new JButton("Change_Web");
pressme1.addActionListener(this);
pressme1.setActionCommand("B");
JPanel theButtons = new JPanel(new BorderLayout());
theButtons.add(pressme, BorderLayout.NORTH);
theButtons.add(pressme1, BorderLayout.CENTER);
theContentPane.add(theButtons);
theContentPane.add(webPanel);
frame.setContentPane(theContentPane);
frame.getContentPane().setBackground(Color.YELLOW);
fillFrames();
frame.pack();
frame.setVisible(true);
}
public htmlviewer() {
}
public void fillFrames() {
setTheStyleSheet(htmlEditorKit.getStyleSheet());
htmlPanel.setText("<html><center><b>The Final State is: δ</center></html>");
String thisdir = (System.getProperty("user.dir") + "/").replace("\\", "/");
try {
File thisHTML = new File(thisdir + "evenodd.html");
if (!thisHTML.exists()) {
thisHTML = new File(thisdir + "turing.html");
}
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(thisHTML)));
webPanel.read(br, htmlDocument);
webPanel.repaint();
} catch (java.io.IOException e) {
e.printStackTrace();
}
}
public void writehtmlPanel() {
String htmlstring = "<html><center>I am changed δ: θ ω</center></html>( ";
htmlPanel.setText(htmlstring);
}
public void writewebPanel() {
URL helpURL = null;
try {
helpURL = new URL("http://www.umsl.edu/~siegelj/turing.html");
} catch (java.net.MalformedURLException e) {
e.printStackTrace();
}
if (helpURL != null) {
try {
String theLine;
String theDoc = "";
BufferedReader bis = new BufferedReader(new InputStreamReader(helpURL.openStream()));
while ((theLine = bis.readLine()) != null) {
theDoc = theDoc + theLine;
}
webPanel.setText(theDoc);
webPanel.repaint();
} catch (java.io.IOException e) {
e.printStackTrace();
}
}
}
public void setTheStyleSheet(StyleSheet s) {
s.addRule("body {color:#000; background-color:#ffdddd; font-family:times; font-size:20px; margin: 4px; }");
s.addRule("h1 {color: blue;}");
s.addRule("h2 {color: #ff0000;}");
s.addRule("h3 {color: #ff0000;}");
s.addRule("pre {font : 10px monaco; color : black; background-color : #fafafa; }");
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("A")) {
writehtmlPanel();
}
if (e.getActionCommand().equals("B")) {
writewebPanel();
}
}
@Override
public void componentHidden(ComponentEvent e) {
System.out.println(e.getComponent().getClass().getName() + " --- Hidden");
}
@Override
public void componentMoved(ComponentEvent e) {
// System.out.println(e.getComponent().getClass().getName() + " --- Moved");
}
@Override
public void componentResized(ComponentEvent e) {
// System.out.println(e.getComponent().getClass().getName() + " --- Resized ");
}
@Override
public void componentShown(ComponentEvent e) {
// System.out.println(e.getComponent().getClass().getName() + " --- Shown");
}
}
turing.html
<html>
<head>
<title></title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<h1>Hello from <br /> www.umsl.edu</h1>
<hr />
<img src="http://www.umsl.edu/~siegelj/turing.jpg" />
</body>
</html>
evenodd.html
<html>
<head>
<title></title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
body {
background-color: #eeffee;
font-size: 14px;
}
h1 {
color: #ff0000
}
</style>
</head>
<body >
<center><h1> Accept Even - Reject Odd</h1></center><hr />
<p>This machine determines whether the length of a string of 1 's is even<br />
If it is it accepts the string, if not it rejects the string.</p>
<p> <font size=+2>
1111 → qaccept<br />
11111 → qreject
</font>
</body>
</html>