/* * caverot.java * * Survex cave view applet using Java 1.1 * * Copyright (C) 2001, Clewin Griffith. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ /** .3dz files are gzip compressed text files. The file is made up of lines of the form command x y z where these are space separated integers. command is 0 for move, 1 for draw. the last two lines in the file are -1 zmin ymin zmin -1 xmax ymax zmax units are cm, so minimum accuracy is 1cm. */ import java.awt.*; import java.applet.*; import java.net.*; import java.util.*; import java.io.*; import java.util.zip.*; public class caverot extends Applet implements Runnable { private int commands[]; private float coords[]; private int numpoints; private int maxx, maxy, maxz; private int minx, miny, minz; private float scale; private float angle; long timer; Image backbuffer; boolean drawing=false; long last_update; Graphics g2; Thread animation_thread; public void init() { angle=0.0f; String arg = getParameter("cave"); System.err.println(arg); try { last_update=System.currentTimeMillis(); URL cave = new URL(arg); BufferedReader in = new BufferedReader(new InputStreamReader(new GZIPInputStream(cave.openStream()))); System.err.println(arg); numpoints=0; while (true) { String line; line=in.readLine(); StringTokenizer tok = new StringTokenizer(line); if(Integer.parseInt(tok.nextToken())>=0) numpoints++; else { minx = Integer.parseInt(tok.nextToken()); miny = Integer.parseInt(tok.nextToken()); minz = Integer.parseInt(tok.nextToken()); line=in.readLine(); tok = new StringTokenizer(line); Integer.parseInt(tok.nextToken()); maxx = Integer.parseInt(tok.nextToken()); maxy = Integer.parseInt(tok.nextToken()); maxz = Integer.parseInt(tok.nextToken()); break; } } in.close(); commands = new int[numpoints]; coords = new float[numpoints*3]; in = new BufferedReader(new InputStreamReader(new GZIPInputStream(cave.openStream()))); numpoints=0; while (true) { String line; line=in.readLine(); StringTokenizer tok = new StringTokenizer(line); int type = Integer.parseInt(tok.nextToken()); if(type<0) break; commands[numpoints] = type; coords[3*numpoints] = Integer.parseInt(tok.nextToken()); coords[3*numpoints+1] = Integer.parseInt(tok.nextToken()); coords[3*numpoints+2] = Integer.parseInt(tok.nextToken()); numpoints++; } in.close(); System.err.print(numpoints); System.err.println(""); in.close(); } catch (Exception e) { System.err.println("File input error"); } scale=270.0f/(float)Math.max(Math.max((maxx-minx),(maxy-miny)),(maxz-minz)); backbuffer=createImage(getSize().width, getSize().height); g2 = backbuffer.getGraphics(); } public void run() { Thread.currentThread().setPriority(Thread.MIN_PRIORITY); long t = System.currentTimeMillis(); Thread currentThread = Thread.currentThread(); while (currentThread == animation_thread) { if(!drawing) { repaint(); try { t += 20; //20ms delay Thread.sleep( Math.max( 0, t - System.currentTimeMillis() ) ); } catch( InterruptedException e ) { break; } } } } public void start() { animation_thread = new Thread(this); animation_thread.start(); } public void stop() { if (animation_thread != null) animation_thread=null; } public void destroy() { if (g2 != null) g2.dispose(); } public void paint( Graphics g ) { drawing=true; g2 = backbuffer.getGraphics(); g2.fillRect(0,0,getSize().width, getSize().height); g2.setColor(Color.white); int prevx, prevy; prevx=prevy=0; for(int i=0; i2.0*Math.PI) angle-=2.0*Math.PI; angle+=0.0005*(double)(curr_time-last_update); paint(g); last_update=curr_time; } }