📄 threed.java
字号:
/* $RCSfile: coolant.cxx,v $
* $Revision: 1.3 $ $Date: 2006/08/21 14:15:51 $
*
* Copyright (c) 1991-2006 by STEP Tools Inc.
* All Rights Reserved.
*
* This file may be distributed and/or modified under the terms of
* the GNU General Public License version 2 as published by the Free
* Software Foundation and appearing in the file LICENSE.GPL included
* with this file.
*
* THIS FILE IS PROVIDED "AS IS" WITH NO WARRANTY OF ANY KIND,
* INCLUDING THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE.
*
* ----------------------------------------
*/
/*
* @(#)ThreeD.java 1.14 04/07/26
*
* Copyright (c) 2004 Sun Microsystems, Inc. All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* -Redistribution of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* -Redistribution in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* Neither the name of Sun Microsystems, Inc. or the names of contributors may
* be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* This software is provided "AS IS," without a warranty of any kind. ALL
* EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
* ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
* OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN MIDROSYSTEMS, INC. ("SUN")
* AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
* AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
* DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
* REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
* INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
* OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE,
* EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
*
* You acknowledge that this software is not designed, licensed or intended
* for use in the design, construction, operation or maintenance of any
* nuclear facility.
*/
/*
* @(#)ThreeD.java 1.14 04/07/26
*/
/* This code was derived from the Java wireframe demo applet */
package com.steptools.view238;
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.URL;
import javax.swing.*;
// class FileFormatException extends IOException {
// public FileFormatException(String s) {
// super(s);
// }
// }
/** An component to put a 3D model into a page */
public class ThreeD extends JComponent
implements MouseListener, MouseMotionListener {
Model3D md;
boolean painted = true;
double xfac;
int prevx, prevy;
double xtheta, ytheta;
double scalefudge = 1;
Matrix3D amat = new Matrix3D(), tmat = new Matrix3D();
String mdname = null;
String message = null;
public ThreeD() {
scalefudge = 1.0;
amat.yrot(20);
amat.xrot(20);
// if (mdname == null)
// mdname = "hughes_500.obj";
setSize(getSize().width <= 20 ? 400 : getSize().width,
getSize().height <= 20 ? 400 : getSize().height);
addMouseListener(this);
addMouseMotionListener(this);
// InputStream is = new FileInputStream(mdname);
// Model3D m = new Model3D (is);
// md = m;
// m.findBB();
// m.compress();
// double xw = m.xmax - m.xmin;
// double yw = m.ymax - m.ymin;
// double zw = m.zmax - m.zmin;
// if (yw > xw)
// xw = yw;
// if (zw > xw)
// xw = zw;
// double f1 = getSize().width / xw;
// double f2 = getSize().height / xw;
// xfac = 0.7f * (f1 < f2 ? f1 : f2) * scalefudge;
// is.close();
// repaint();
}
public void setModel (Model3D m) {
md = m;
m.findBB();
m.compress();
double xw = m.xmax - m.xmin;
double yw = m.ymax - m.ymin;
double zw = m.zmax - m.zmin;
if (yw > xw)
xw = yw;
if (zw > xw)
xw = zw;
double f1 = getSize().width / xw;
double f2 = getSize().height / xw;
xfac = 0.7f * (f1 < f2 ? f1 : f2) * scalefudge;
// is.close();
}
public Dimension getPreferredSize() {
return new Dimension(400,400);
}
public void mouseClicked(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
prevx = e.getX();
prevy = e.getY();
e.consume();
}
public void mouseReleased(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mouseDragged(MouseEvent e) {
int x = e.getX();
int y = e.getY();
tmat.unit();
double xtheta = (prevy - y) * 360.0f / getSize().width;
double ytheta = (x - prevx) * 360.0f / getSize().height;
tmat.xrot(xtheta);
tmat.yrot(ytheta);
amat.mult(tmat);
forceRepaint();
prevx = x;
prevy = y;
e.consume();
}
public void forceRepaint() {
if (painted) {
painted = false;
repaint();
}
}
public void mouseMoved(MouseEvent e) {
}
public void paint(Graphics g) {
if (md != null) {
md.mat.unit();
md.mat.translate(-(md.xmin + md.xmax) / 2,
-(md.ymin + md.ymax) / 2,
-(md.zmin + md.zmax) / 2);
md.mat.mult(amat);
md.mat.scale(xfac, -xfac, 16 * xfac / getSize().width);
md.mat.translate(getSize().width / 2, getSize().height / 2, 8);
md.transformed = false;
md.paint(g);
setPainted();
} else if (message != null) {
g.drawString("Error in model:", 3, 20);
g.drawString(message, 10, 40);
}
}
private synchronized void setPainted() {
painted = true;
notifyAll();
}
// private synchronized void waitPainted() {
// while (!painted)
// wait();
// painted = false;
// }
// public String getAppletInfo() {
// return "Title: ThreeD \nAuthor: James Gosling? \nAn applet to put a 3D model into a page.";
// }
// public String[][] getParameterInfo() {
// String[][] info = {
// {"model", "path string", "The path to the model to be displayed."},
// {"scale", "double", "The scale of the model. Default is 1."}
// };
// return info;
// }
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -