package vgp.tutor.loader; import java.applet.Applet; import java.awt.*; import java.io.BufferedReader; import java.io.IOException; import jv.geom.PgPointSet; import jv.geom.PgPolygon; import jv.geom.PgPolygonSet; import jv.geom.PgElementSet; import jv.object.*; import jv.project.PgGeometryIf; import jv.project.PvGeometryIf; import jv.project.PvDisplayIf; import jv.project.PgJvxSrc; import jv.viewer.PvViewer; /** * Tutorial on geometry loaders. This tutorial explains how to write * and use an own geometry loader within JavaView. *
* The loader used in the init() method of this applet can be replaced with
* any of the built-in loaders (jv.loader.*, jvx.loader.*) of JavaView.
*
* @see vgp.tutor.model.PaModel
* @author Konrad Polthier
* @version 22.02.03, 1.15 revised (kp) Check for unknown geometry types.
* 06.10.02, 1.10 revised (kp) Use codebase when loading a file.
* 09.11.01, 1.00 created (kp)
*/
public class PaLoader extends Applet {
/** frame if run standalone, null if run as applet. */
public Frame m_frame = null;
/** 3D-viewer window for graphics output and which is embedded into the applet. */
protected PvViewer m_viewer;
/** Interface of applet to inform about author, version, and copyright. */
public String getAppletInfo() {
return "Name: " + this.getClass().getName()+ "\r\n" +
"Author: " + "Konrad Polthier" + "\r\n" +
"Version: " + "1.15" + "\r\n" +
"Tutorial on usage of parsing an own geometry file format." + "\r\n";
}
/**
* Configure and initialize the viewer, load system and user projects.
* One of the user projects must be selected here.
*
p
* Note, for tutorial reasons this method still continues
* even if parsing errors appear.
*/
public void init() {
// Create viewer for viewing 3d geometries
m_viewer = new PvViewer(this, m_frame);
PvDisplayIf disp = m_viewer.getDisplay();
// Get the name of the geometry file either as applet parameter
// or, if running as application, as command line argument.
String fileName = m_viewer.getParameter("filename");
String ext = PsUtil.getFileExtension(fileName);
if (ext == null) {
if (PsDebug.WARNING) PsDebug.warning("missing extension in file="+fileName);
}
// The geometries will be generate in the parser. This parser
// returns exactly one geometry.
PgJvxSrc [] geomArr = null;
// Open the file resp. URL. Getting the codeBase from PsConfig
// avoids a distinction between applets and applications.
BufferedReader in = PsUtil.open(PsConfig.getCodeBase()+fileName);
if (in == null) {
if (PsDebug.WARNING) PsDebug.warning("could not open file = "+fileName);
} else {
// Parse the file
PgAbcLoader loader = new PgAbcLoader();
String index = m_viewer.getParameter("vgp.tutor.loader.indexfirstvertex");
if (index != null) {
int ind = Integer.parseInt(index);
loader.setFirstVertexIndex(ind);
}
geomArr = loader.read(in);
// Before we continue, first close the file.
try { in.close(); } catch (IOException ex) {}
}
if (geomArr==null || geomArr[0]==null) {
if (PsDebug.WARNING) PsDebug.warning("error when reading file = "+fileName);
} else {
for (int i=0; i