📄 rotator3d.java
字号:
/* * @(#)Rotator3D.java 1.4 98/12/03 * * Copyright 1993-1998 Sun Microsystems, Inc. 901 San Antonio Road, * Palo Alto, California, 94303, U.S.A. All Rights Reserved. * * This software is the confidential and proprietary information of Sun * Microsystems, Inc. ("Confidential Information"). You shall not * disclose such Confidential Information and shall use it only in * accordance with the terms of the license agreement you entered into * with Sun. * * CopyrightVersion 1.2 * */import java.awt.*;import java.awt.event.*;import java.awt.image.BufferedImage;import javax.swing.*;/** * The Rotator3D class displays animated multi-colored 3D objects that are * translated, rotated and scaled. Color changes are demonstrated by using a * light source and shading. */public class Rotator3D extends JApplet implements Runnable { private Objects3D objs[] = new Objects3D[3]; private static final int[][][] polygons = { // Solid cube {{5, 1, 15, 13, 21, 23, 15}, {5, 2, 21, 13, 19, 27, 21}, {5, 3, 23, 15, 17, 25, 23}, {5, 4, 19, 13, 15, 17, 19}, {5, 5, 27, 21, 23, 25, 27}, {5, 6, 27, 19, 17, 25, 27}}, // Polygonal faces cube {{5, 1, 21, 13, 19, 27, 21}, {5, 5, 23, 15, 17, 25, 23}, {4,0,15,14,16,15}, {7,6,16,14,13,12,18,17,16}, {4,0,12,19,18,12}, {4,2,22,21,20,22}, {7,0,24,23,22,20,27,26,24}, {4,2,24,26,25,24}, {4, 3, 15, 13, 23, 15}, {4, 0, 23, 13, 21, 23}, {5, 0, 27, 26, 18, 19, 27}, {5, 4, 25, 17, 18, 26, 25}}, // Octahedron {{4, 3, 18, 21, 16, 18}, {4, 1, 20, 16, 18, 20}, {4, 1, 18, 21, 16, 18}, {4, 3, 20, 17, 19, 20}, {4, 2, 20, 26, 27, 20}, {5, 3, 26, 18, 16, 27, 26}, {5, 0, 17, 24, 25, 19, 17}, {4, 3, 21, 25, 24, 21}, {4, 4, 18, 21, 22, 18}, {4, 2, 22, 21, 17, 22}, {4, 5, 20, 23, 16, 20}, {4, 1, 20, 23, 19, 20}, {4, 6, 21, 23, 16, 21}, {4, 4, 21, 23, 19, 21}, {4, 5, 20, 18, 22, 20}, {4, 6, 20, 22, 17, 20}} }; private static final double[][][] points = { // Points for solid cube & polygonal faces cube {{1, 0, 0}, {-1, 0, 0}, {0, 1, 0}, {0, -1, 0}, {0, 0, 1}, {0, 0, -1}, {1, 0, 0}, {-1, 0, 0}, {0, 1, 0}, {0, -1, 0}, {0, 0, 1}, {0, 0, -1}, {1, 1, 0}, {1, 1, 1}, {0, 1, 1}, {-1, 1, 1}, {-1, 1, 0}, {-1, 1, -1}, {0, 1, -1}, {1, 1, -1}, {1, -1, 0}, {1, -1, 1}, {0, -1, 1}, {-1, -1, 1}, {-1, -1, 0}, {-1, -1, -1}, {0, -1, -1}, {1, -1, -1}}, // Points for octahedron {{0, 0, 1}, {0, 0, -1}, {-0.8165, 0.4714, 0.33333}, {0.8165, -0.4714, -0.33333}, {0.8165, 0.4714, 0.33333}, {-0.8165, -0.4714, -0.33333}, {0, -0.9428, 0.3333}, {0, 0.9428, -0.33333}, {0, 0, 1}, {0, 0, -1}, {-0.8165, 0.4714, 0.33333}, {0.8165, -0.4714, -0.33333}, {0.8165, 0.4714, 0.33333}, {-0.8165, -0.4714, -0.33333}, {0, -0.9428, 0.33333}, {0, 0.9428, -0.33333}, {-1.2247, -0.7071, 1}, {1.2247, 0.7071, -1}, {0, 1.4142, 1}, {0, -1.4142, -1}, {-1.2247, 0.7071, -1}, {1.2247, -0.7071, 1}, {0.61237, 1.06066, 0}, {-0.61237, -1.06066, 0}, {1.2247, 0, 0}, {0.61237, -1.06066, 0}, {-0.61237, 1.06066, 0}, {-1.2247, 0, 0}} }; private static final int[][][] faces = { // Solid cube {{1, 1}, {1, 2}, {1, 3}, {1, 4}, {1, 0}, {1, 5}}, // Polygonal faces cube {{1, 0}, {1, 1}, {3, 2, 3, 4}, {3, 5, 6, 7}, {2,8,9}, {2,10,11}}, // Octahedron {{1, 2}, {1, 3}, {2, 4, 5}, {2, 6, 7}, {2, 8, 9}, {2, 10, 11}, {2, 12, 13}, {2, 14, 15}}, }; private Thread thread; private BufferedImage bimg; public void init() { setBackground(Color.white); } // initializes the 3D objects public void reset(int w, int h) { objs[0] = new Objects3D(polygons[0], points[0], faces[0], w, h); objs[1] = new Objects3D(polygons[1], points[0], faces[1], w, h); objs[2] = new Objects3D(polygons[2], points[1], faces[2], w, h); } public void step(int w, int h) { for (int i = objs.length; i-- > 0; ) { if (objs[i] != null) { objs[i].step(w, h); } } } public void drawDemo(int w, int h, Graphics2D g2) { for (int i = objs.length; i-- > 0; ) { if (objs[i] != null) { objs[i].Draw(g2); } } } public Graphics2D createGraphics2D(int w, int h) { Graphics2D g2 = null; if (bimg == null || bimg.getWidth() != w || bimg.getHeight() != h) { bimg = (BufferedImage) createImage(w, h); reset(w, h); } g2 = bimg.createGraphics(); g2.setBackground(getBackground()); g2.clearRect(0, 0, w, h); g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); return g2; } public void paint(Graphics g) { Dimension d = getSize(); step(d.width, d.height); Graphics2D g2 = createGraphics2D(d.width, d.height); drawDemo(d.width, d.height, g2); g2.dispose(); g.drawImage(bimg, 0, 0, this); } public void start() { thread = new Thread(this); thread.setPriority(Thread.MIN_PRIORITY); thread.start(); } public synchronized void stop() { thread = null; } public void run() { Thread me = Thread.currentThread(); while (thread == me) { repaint(); try { thread.sleep(10); } catch (InterruptedException e) { break; } } thread = null; } /** * The Objects3D class creates a Solid Cube, Cube and Octahedron with * polygonal faces. */ public class Objects3D { private final int UP = 0; private final int DOWN = 1; private int[][] polygons; private int npoly; private double[][] points; private int npoint; private int[][] faces; private int nface; private int ncolour = 10; private Color[][] colours = new Color[ncolour][7]; private double[] lightvec = {0, 1, 1}; private double Zeye = 10; private double angle; private Matrix3D orient, tmp, tmp2, tmp3; private int scaleDirection; private double scale, scaleAmt; private double ix = 3.0, iy = 3.0; private double[][] rotPts; private int[][] scrPts; private int xx[] = new int[20]; private int yy[] = new int[20]; private double x, y; private int p, j; private int colour; private double bounce, persp; public Objects3D(int[][] polygons, double[][] points, int[][] faces, int w, int h) { this.polygons = polygons; this.points = points; this.faces = faces; npoly = polygons.length; npoint = points.length; nface = faces.length; x = w * Math.random(); y = h * Math.random(); ix = Math.random() > 0.5 ? ix : -ix; iy = Math.random() > 0.5 ? iy : -iy; rotPts = new double[npoint][3]; scrPts = new int[npoint][2]; /* * computes colors to produce shading effect */ for (int i = 0; i < ncolour; i++) { // white colours[i][0]= new Color(255 -(ncolour-1-i)*100/ncolour, 255 -(ncolour-1-i)*100/ncolour,255 -(ncolour-1-i)*100/ncolour); // red
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -