| Plot2D Application |
TABLE OF CONTENTS
<?xml encoding="UTF-8"?> <!ELEMENT barchart (rectangle+) > <!ELEMENT rectangle (titleofbar,width,height,color) > <!ELEMENT titleofbar (#PCDATA)> <!ELEMENT width (#PCDATA)> <!ELEMENT height (#PCDATA)> <!ELEMENT color (#PCDATA)>
<?xml version="1.0"?>
<!DOCTYPE barchart SYSTEM "barchart.dtd">
<barchart>
<rectangle>
<titleofbar> Bar1 </titleofbar>
<width> 20 </width>
<height> 100 </height>
<color> Red </color>
</rectangle>
<rectangle>
<titleofbar> Bar2 </titleofbar>
<width> 20 </width>
<height> 150 </height>
<color> Blue </color>
</rectangle>
<rectangle>
<titleofbar> Bar3 </titleofbar>
<width> 20 </width>
<height> 175 </height>
<color> Yellow </color>
</rectangle>
<rectangle>
<titleofbar> Bar4 </titleofbar>
<width> 20 </width>
<height> 125 </height>
<color> Green </color>
</rectangle>
</barchart>
/*
* =================================================================
* TestXmlPanel.java: Simple plotting program that exercises SAX
* parsing technology.
*
* Written By: Vijay Krishnamurthy & Abhinav Joshi Spring, 2001
* Modified By: Mark Austin March, 2002
* =================================================================
*/
// Import the Swing packages ....
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.SwingUtilities;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
// Import packages to implement the SAX parser .....
import javax.xml.parsers.*;
import org.xml.sax.*;
import org.xml.sax.helpers.*;
// For package for file handling ......
import java.io.*;
// =====================================================
// Create the Swing applet (by extending JApplet) ......
// =====================================================
public class TestXmlPanel extends JApplet {
parsexml pcopy;
jpanel j;
// Constructor .......
TestXmlPanel(parsexml p) { pcopy = p; }
// Instantiate an object of jpanel (which extends JPanel) ....
public void init() {
Container contentPane = getContentPane();
j = new jpanel( pcopy );
contentPane.add(j);
}
// Instantiate standalone Java program .....
public static void main( String[] args ) {
// Create a Swing frame.....
JFrame f = new JFrame("ENCE 489C Project");
parsexml p = new parsexml();
TestXmlPanel txml = new TestXmlPanel(p);
// Initialize the applet instance......
txml.init();
// Add the applet to the JFrame......
f.getContentPane().add( txml );
// Add the listener to close the frame ......
f.addWindowListener(new WindowEventHandler());
f.setSize(600, 600); // frame: width=300, height = 200
f.show(); // Display the frame
}
}
// =====================================================
// class jpanel (which extends JPanel
// =====================================================
class jpanel extends JPanel implements ActionListener, Runnable {
parsexml pcopy;
static final int BARCHART = 1;
static final int PIECHART = 2;
static final int HISTOGRAM = 3;
static final int PLOT2D = 4;
JButton b1,b2,b3,b4,b5,b6,b7,newBut,saveBut,okBut;
JTextField input,numChoices;
String arrValues[];
int choice,numElements = 0;
float total = 0;
boolean errorOccur = false, openOnce = true;
String errDisplay;
Thread t;
boolean animStop = false;
public PrintWriter xmlout, dtdout;
String color[] = new String[9];
String fileName;
// Constructor which takes in an object of parsexml class as parameter ...
jpanel(parsexml psrc) {
color[0]= "Red"; color[1] = "Green"; color[2] = "Pink";
color[3]= "Blue"; color[4] = "Yellow"; color[5] = "Cyan";
color[6]= "White"; color[7] = "Gray"; color[8] ="Magenta";
// Setting the default to NULL .....
setLayout(null);
setBackground(Color.black);
pcopy = psrc;
// Adding the various components in JPanel and specifying it's location....
JPanel pjsouth = new JPanel();
pjsouth.setSize(400,40);
pjsouth.setLocation(100,450);
JPanel pjnorth = new JPanel();
pjnorth.setSize(500,40);
pjnorth.setLocation(50,10);
JPanel pjwest = new JPanel();
pjwest.setSize(400,40);
pjwest.setLocation(100,500);
JPanel pjeast = new JPanel();
pjeast.setSize(560,40);
pjeast.setLocation(10,70);
// Creating the Buttons and adding the respective listeners ....
b1 = new JButton("Barchart");
b1.addActionListener(this);
pjsouth.add(b1);
b2 = new JButton("PieChart");
b2.addActionListener(this);
pjsouth.add(b2);
b3 = new JButton("Histogram");
b3.addActionListener(this);
pjsouth.add(b3);
b4 = new JButton("Plot2D");
b4.addActionListener(this);
pjsouth.add(b4);
add(pjsouth);
newBut = new JButton("NEW");
pjeast.add(newBut);
newBut.addActionListener(this);
newBut.setToolTipText("Click here to create an XML file");
JLabel lbl = new JLabel("Enter num of elements");
pjeast.add(lbl);
// numChoices JTextField for specifying the no of elements ....
numChoices = new JTextField();
numChoices.setColumns(4);
pjeast.add(numChoices);
numChoices.addActionListener(this);
numChoices.setEnabled(false);
okBut = new JButton("OK");
pjeast.add(okBut);
okBut.addActionListener(this);
okBut.setEnabled(false);
saveBut = new JButton("SAVE");
pjeast.add(saveBut);
saveBut.setEnabled(false);
saveBut.addActionListener(this);
saveBut.setToolTipText("Click here to save the new XML file");
add(pjeast);
// Input for taking in an xml file as input ....
input = new JTextField("Enter the URL here");
input.setColumns(25);
pjnorth.add(input);
input.addActionListener(this);
b7 = new JButton("OPEN");
pjnorth.add(b7);
b7.addActionListener(this);
b7.setToolTipText("Click here to open an XML file");
add(pjnorth);
// Stop and Resume Buttons for animation ....
b5 = new JButton("Stop");
b5.addActionListener(this);
b5.setToolTipText("Click here to stop the animation");
pjwest.add(b5);
b6 = new JButton("Resume");
b6.addActionListener(this);
b6.setToolTipText("Click here to start the animation");
pjwest.add(b6);
b6.setEnabled(false);
add(pjwest);
// Create a new thread object for animation .....
t = new Thread(this);
// Create an object of parsexml and specifying a default file ....
parsexml p1 = new parsexml();
String s = "http://www.glue.umd.edu/~abhinav/barchart.xml";
p1.copyPanel(this);
// Call the initialize function in to parse file ....
p1.initialize(s);
copy(p1);
choice = 1;
fileName = s;
}
public void copy(parsexml p) {
pcopy = p;
}
public void run() { }
// The gui component is used to display the parsed output ...
public void paintComponent(Graphics g) {
Font f = new Font("Times New Roman",Font.PLAIN,12);
f = g.getFont();
super.paintComponent(g);
setBackground(Color.black);
g.setColor(Color.yellow);
g.setFont(new Font("Times New Roman",Font.BOLD,26));
g.drawString("Java & XML",400,250);
g.drawString("Graphical",400,280);
g.drawString("Interface",400,310);
g.setFont(f);
int xpos = 75;
int startAngle = 0;
int point = 40;
int prevx = 0, prevy =0;
// option for error handling ....
if (errorOccur) {
g.setColor(Color.white);
//g.drawString(errDisplay,100,300);
if (openOnce) {
JOptionPane.showMessageDialog((Component)null,
"File does not exist at the specified location",
"ERROR",
JOptionPane.ERROR_MESSAGE);
openOnce = false;
input.setText(" ");
fileName = "";
updateUI();
}
errorOccur = false;
} else {
// Displaying the name of file ...
g.setColor(Color.white);
g.setFont(new Font("Times New Roman",Font.BOLD,20));
g.drawString(fileName,150,130);
g.setFont(f);
if (choice == 2) {
total =0;
for(int j=0;j" );
dtdout.println("");
dtdout.println("");
dtdout.println("");
dtdout.println("");
dtdout.println("");
dtdout.println("");
dtdout.println("");
dtdout.close();
/* Building the xml file */
xmlout.println("" );
xmlout.println("");
xmlout.println("");
xmlout.println("<"+result+">");
for (int aj=1;aj<=numElements;aj++) {
xmlout.println("");
xmlout.println(""+ "Bar "+ aj + " ");
xmlout.println("20 ");
xmlout.println("" + arrValues[aj-1] + " ");
xmlout.println(""+ color[aj-1] + " ");
xmlout.println(" ");
}
xmlout.println(""+result+">");
xmlout.close();
}
}
// ========================================================
// Window Adapter class to handle the closing of window ...
// ========================================================
class WindowEventHandler extends WindowAdapter {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}
// =======================================
// Class for parsing the xml document ....
// =======================================
class parsexml extends DefaultHandler {
private boolean opened;
public PrintWriter out;
private String tags;
public String arr[][];
public int row,col ;
private boolean new_row;
jpanel jp;
public void initialize(String name) {
SAXParserFactory spf = SAXParserFactory.newInstance();
spf.setValidating(true);
XMLReader reader = null;
// Create a parserFactory instance ....
try {
SAXParser parser = spf.newSAXParser();
reader = parser.getXMLReader();
}
// Check for and display errors on the screen ....
catch(Exception ex) {
String err = ex.toString();
// jp.errorOccur = true;
jp.openOnce = true;
jp.updateUI();
}
reader.setContentHandler(this);
// parsing the xml document ....
try {
reader.parse(name);
}
// Checking for errors ....
catch(SAXException ex) {
String err =ex.toString();
//jp.errorOccur = true;
jp.openOnce =true;
jp.updateUI();
}
catch(IOException ex) {
// Checking for file exceptions ....
String err = ex.toString();
//jp.errorOccur = true;
jp.openOnce = true;
jp.errDisplay = err;
JOptionPane.showMessageDialog((Component)null,
"File does not exist at the specified location",
"ERROR",JOptionPane.ERROR_MESSAGE);
jp.fileName = "";
jp.updateUI();
}
}
// Function fires when during start of xml document ...
public void startDocument() {
// Generate the existing xml file into an xml file ....
try {
out = new PrintWriter(new FileWriter("output.xml"));
}
catch(IOException e) {
System.out.println("Error "+e);
}
// storing the data in arr of string ....
arr = new String[20][4];
opened = false;
row = 0; col = 0;
new_row = false;
out.println("" );
out.println("");
out.println(" ");
}
// Get notification during start of an element tag ...
public void startElement(String namespaceURI,String localname,
String rawname, Attributes atts) {
// variable to store the name of the element ...
String key = localname;
tags = localname;
out.print("<");
out.print(key);
// Check to see if the element has any attributes ....
if (atts != null) {
int len = atts.getLength();
for (int i = 0; i < len; i++) {
out.print(" ");
out.print(atts.getQName(i));
out.print(" ");
out.print(atts.getValue(i));
out.println(" ");
}
}
// putting the output in the xml file ....
out.print(">");
if(key == "titleofbar")
new_row = true;
if((key == "barchart")|| (key == "rectangle"))
out.println(" ");
}
// Extract the data from between the tags .....
public void characters(char[] ch,int start,int length) throws SAXException {
out.print(new String(ch, start, length));
//out.print( " end of charc");
//out.println(" ");
//endElement(tags);
if (new_row) {
arr[row][col] = new String(ch, start, length);
col++;
}
}
// Get notification at the end of element tag .....
public void endElement (String namespaceURI, String localName, String qName)
throws SAXException {
out.println(""+localName+">");
if (localName == "rectangle") {
row++;
col = 0;
new_row = false;
}
}
// Get notification after the document has ended ....
public void endDocument() { out.close(); }
// Method for copying an instance of jpanel class in parsexml class ...
public void copyPanel(jpanel j) { jp = j; }
}
Developed in December 2001 by Mark Austin
Copyright © 2001-2002, Mark Austin, University of Maryland