00000002.htm
来自「水木清华BBS」· HTM 代码 · 共 208 行
HTM
208 行
<HTML><HEAD> <TITLE>BBS水木清华站∶精华区</TITLE></HEAD><BODY><CENTER><H1>BBS水木清华站∶精华区</H1></CENTER>发信人: VolVis (arthur), 信区: Java <BR>标 题: Java Program(3) <BR>发信站: BBS 水木清华站 (Fri Dec 25 13:39:32 1998) WWW-POST <BR> <BR>At last, I want to introduce the class Viewer3D. The init function of View3D <BR>is
<BR>to get the OFF file name by getParameter("model"), and using the class <BR>Matrix3d
<BR>to rotate X and Y axis 20 degrees seperately. Then the applet class will <BR>invokethe function start to work, which will create a new class OOGL_OFF and <BR>transformthe origin to the center of object, and at last create a buffer <BR>image to be
<BR>painted on. The paint function is to transform and paint the object to the
<BR>graphics context. The model transform is to transform the origin to the <BR>center
<BR>of object, scale, and then translate to the image center. At the last stage, <BR>theobjects in the scenes will be drawn on the buffer image.
<BR>
<BR>Java Program:
<BR>/**
<BR> *
<BR> * Author: Daeron Meyer
<BR> * Copyright (c) 1995 by The Geometry Center, University of Minnesota
<BR> * Distributed under the terms of the GNU Library General Public License
<BR> * 12-14-95
<BR> *
<BR> */
<BR>
<BR>import java.applet.Applet;
<BR>import java.awt.Image;
<BR>import java.awt.Graphics;
<BR>import java.awt.Color;
<BR>import java.awt.Event;
<BR>import java.lang.*;
<BR>import java.io.InputStream;
<BR>import java.net.URL;
<BR>
<BR>/**
<BR> * Viewer3D: An applet that displays and rotates a 3D OFF object
<BR> * interactively, in response to user mouse events.
<BR> */
<BR>
<BR>public class Viewer3D extends Applet {
<BR>
<BR> OOGL_OFF obj;
<BR> boolean painted = true;
<BR> String objname = null,
<BR> message = null;
<BR> float xfac, scaleval = 0.1f;
<BR> Matrix3D amat = new Matrix3D(),
<BR> tmat = new Matrix3D();
<BR>
<BR> Image bbuffer;
<BR> Graphics bgc;
<BR> int prevx, prevy;
<BR>
<BR> public void init() {
<BR>
<BR> objname = null;
<BR> objname = getParameter("model"); // get the object name
<BR> if (objname == null) objname = "models/chemi.off";
<BR>
<BR> try {
<BR> scaleval = Float.valueOf(getParameter("scale")).floatValue() * 0.1f;
<BR> } catch (Exception e) { };
<BR>
<BR> amat.yrot(20); // set initial rotation
<BR> amat.xrot(20);
<BR>
<BR> resize(size().width <=20 ? 400 : size().width,
<BR> size().height <= 20 ? 400 : size().height);
<BR>
<BR> }
<BR>
<BR> public void start() {
<BR>
<BR> try {
<BR>
<BR> OOGL_OFF x = new OOGL_OFF (new URL(getDocumentBase(), objname));
<BR> // read object from URL
<BR> obj = x;
<BR> obj.findBB(); // find bounding box
<BR> float xw = obj.xmax - obj.xmin; // so we can scale
<BR> float yw = obj.ymax - obj.ymin; // the object to fit
<BR> float zw = obj.zmax - obj.zmin; // in our window
<BR> if (yw > xw) xw = yw;
<BR> if (zw > xw) xw = zw;
<BR> float f1 = 250 / xw;
<BR> float f2 = 250 / xw;
<BR> xfac = 0.7f * (f1 < f2 ? f1 : f2) * scaleval;
<BR>
<BR> bbuffer = createImage(size().width, size().height);
<BR> bgc = bbuffer.getGraphics(); // create image to do
<BR> // double buffering
<BR>
<BR> } catch(Exception e) {
<BR> obj = null;
<BR> message = e.toString();
<BR> }
<BR> repaint();
<BR>
<BR> }
<BR>
<BR>
<BR>/* handle mouse events */
<BR>
<BR> public boolean mouseDown(Event e, int x, int y) {
<BR> prevx = x;
<BR> prevy = y;
<BR> return true;
<BR> }
<BR>
<BR> public boolean mouseDrag(Event e, int x, int y) {
<BR>
<BR> tmat.unit();
<BR> float xtheta = (prevy - y) * 360.0f / size().width;
<BR> float ytheta = (prevx - x) * 360.0f / size().height;
<BR> tmat.xrot(xtheta);
<BR> tmat.yrot(ytheta);
<BR> amat.mult(tmat);
<BR>
<BR> if (painted) {
<BR>
<BR> painted = false;
<BR> repaint();
<BR>
<BR> }
<BR>
<BR> prevx = x; prevy = y;
<BR> return true;
<BR>
<BR> }
<BR>
<BR> public void update(Graphics g) {
<BR>
<BR> if (bbuffer == null)
<BR> g.clearRect(0, 0, size().width, size().height);
<BR>
<BR> paint(g);
<BR>
<BR> }
<BR>
<BR>/* transform and paint the object to the graphics context */
<BR>
<BR> public void paint(Graphics g) {
<BR>
<BR> if (obj != null) {
<BR>
<BR> obj.mat.unit();
<BR> obj.mat.translate(-(obj.xmin + obj.xmax) / 2,
<BR> -(obj.ymin + obj.ymax) / 2,
<BR> -(obj.zmin + obj.zmax) / 2);
<BR> obj.mat.mult(amat);
<BR>
<BR> int scale = (int)( xfac * size().width / 25 );
<BR>
<BR> obj.mat.scale(scale, scale, 16 * xfac / size().width);
<BR> obj.mat.translate(size().width / 2, size().height / 2, 8);
<BR> obj.transformed = false;
<BR>
<BR> if (bbuffer != null) {
<BR>
<BR> bgc.setColor(getBackground());
<BR> bgc.fillRect(0,0,size().width,size().height);
<BR> obj.paint(bgc);
<BR> g.drawImage(bbuffer, 0, 0, this);
<BR>
<BR> } else
<BR> obj.paint(g);
<BR>
<BR> setPainted();
<BR>
<BR> } else if (message != null)
<BR> g.drawString("Error reading OFF file", 3, 20);
<BR>
<BR> }
<BR>
<BR> private synchronized void setPainted() {
<BR>
<BR> painted = true;
<BR> notifyAll();
<BR>
<BR> }
<BR>
<BR>}
<BR> <BR> <BR>-- <BR>※ 来源:·BBS 水木清华站 bbs.net.tsinghua.edu.cn·[FROM: 162.105.18.102] <BR><CENTER><H1>BBS水木清华站∶精华区</H1></CENTER></BODY></HTML>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?