1 /*
2 *
3 * Copyright (c) 2007, 2011, Oracle and/or its affiliates. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * - Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * - Neither the name of Oracle nor the names of its
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
21 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
33
34
35 import static java.lang.Math.PI;
36 import static java.lang.Math.abs;
37 import static java.lang.Math.cos;
38 import static java.lang.Math.min;
39 import static java.lang.Math.random;
40 import static java.lang.Math.sin;
41 import static java.lang.Math.sqrt;
42 import java.awt.Color;
43 import java.awt.Graphics2D;
44 import java2d.AnimatingSurface;
45
46
47 /**
48 * 3D objects with color & lighting translated, rotated and scaled.
49 */
50 @SuppressWarnings("serial")
51 public class Rotator3D extends AnimatingSurface {
52
53 private Objects3D objs[] = new Objects3D[3];
54 private static final int[][][] polygons = {
55 // Solid cube
56 { { 5, 1, 15, 13, 21, 23, 15 },
57 { 5, 2, 21, 13, 19, 27, 21 },
58 { 5, 3, 23, 15, 17, 25, 23 },
59 { 5, 4, 19, 13, 15, 17, 19 },
60 { 5, 5, 27, 21, 23, 25, 27 },
61 { 5, 6, 27, 19, 17, 25, 27 } },
62 // Polygonal faces cube
63 { { 5, 1, 21, 13, 19, 27, 21 },
64 { 5, 5, 23, 15, 17, 25, 23 },
65 { 4, 0, 15, 14, 16, 15 }, { 7, 6, 16, 14, 13, 12, 18, 17, 16 }, { 4,
66 0, 12, 19, 18, 12 },
67 { 4, 2, 22, 21, 20, 22 }, { 7, 0, 24, 23, 22, 20, 27, 26, 24 }, { 4,
68 2, 24, 26, 25, 24 },
69 { 4, 3, 15, 13, 23, 15 }, { 4, 0, 23, 13, 21, 23 },
70 { 5, 0, 27, 26, 18, 19, 27 }, { 5, 4, 25, 17, 18, 26, 25 } },
71 // Octahedron
72 { { 4, 3, 18, 21, 16, 18 }, { 4, 1, 20, 16, 18, 20 },
73 { 4, 1, 18, 21, 16, 18 }, { 4, 3, 20, 17, 19, 20 },
125 }
126
127 @Override
128 public void step(int w, int h) {
129 for (Objects3D obj : objs) {
130 if (obj != null) {
131 obj.step(w, h);
132 }
133 }
134 }
135
136 @Override
137 public void render(int w, int h, Graphics2D g2) {
138 for (Objects3D obj : objs) {
139 if (obj != null) {
140 obj.render(g2);
141 }
142 }
143 }
144
145 public static void main(String argv[]) {
146 createDemoFrame(new Rotator3D());
147 }
148
149
150 /**
151 * 3D Objects : Solid Cube, Cube & Octahedron with polygonal faces.
152 */
153 public class Objects3D {
154
155 private final int UP = 0;
156 private final int DOWN = 1;
157 private int[][] polygons;
158 private double[][] points;
159 private int npoint;
160 private int[][] faces;
161 private int nface;
162 private int ncolour = 10;
163 private Color[][] colours = new Color[ncolour][7];
164 private double[] lightvec = { 0, 1, 1 };
165 private double Zeye = 10;
166 private double angle;
167 private Matrix3D orient, tmp, tmp2, tmp3;
168 private int scaleDirection;
169 private double scale, scaleAmt;
170 private double ix = 3.0, iy = 3.0;
171 private double[][] rotPts;
172 private int[][] scrPts;
173 private int xx[] = new int[20];
174 private int yy[] = new int[20];
175 private double x, y;
176 private int p, j;
177 private int colour;
178 private double bounce, persp;
179
180 public Objects3D(int[][] polygons,
181 double[][] points,
182 int[][] faces,
183 int w,
184 int h) {
185
186 this.polygons = polygons;
187 this.points = points;
188 this.faces = faces;
189 npoint = points.length;
190 nface = faces.length;
191
192 x = w * random();
193 y = h * random();
194
|
1 /*
2 *
3 * Copyright (c) 2007, 2018, Oracle and/or its affiliates. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * - Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * - Neither the name of Oracle nor the names of its
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
21 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
33
34
35 import static java.lang.Math.PI;
36 import static java.lang.Math.abs;
37 import static java.lang.Math.cos;
38 import static java.lang.Math.min;
39 import static java.lang.Math.random;
40 import static java.lang.Math.sin;
41 import static java.lang.Math.sqrt;
42 import java.awt.Color;
43 import java.awt.Graphics2D;
44 import java2d.AnimatingSurface;
45
46
47 /**
48 * 3D objects with color & lighting translated, rotated and scaled.
49 */
50 @SuppressWarnings("serial")
51 public class Rotator3D extends AnimatingSurface {
52
53 private Objects3D[] objs = new Objects3D[3];
54 private static final int[][][] polygons = {
55 // Solid cube
56 { { 5, 1, 15, 13, 21, 23, 15 },
57 { 5, 2, 21, 13, 19, 27, 21 },
58 { 5, 3, 23, 15, 17, 25, 23 },
59 { 5, 4, 19, 13, 15, 17, 19 },
60 { 5, 5, 27, 21, 23, 25, 27 },
61 { 5, 6, 27, 19, 17, 25, 27 } },
62 // Polygonal faces cube
63 { { 5, 1, 21, 13, 19, 27, 21 },
64 { 5, 5, 23, 15, 17, 25, 23 },
65 { 4, 0, 15, 14, 16, 15 }, { 7, 6, 16, 14, 13, 12, 18, 17, 16 }, { 4,
66 0, 12, 19, 18, 12 },
67 { 4, 2, 22, 21, 20, 22 }, { 7, 0, 24, 23, 22, 20, 27, 26, 24 }, { 4,
68 2, 24, 26, 25, 24 },
69 { 4, 3, 15, 13, 23, 15 }, { 4, 0, 23, 13, 21, 23 },
70 { 5, 0, 27, 26, 18, 19, 27 }, { 5, 4, 25, 17, 18, 26, 25 } },
71 // Octahedron
72 { { 4, 3, 18, 21, 16, 18 }, { 4, 1, 20, 16, 18, 20 },
73 { 4, 1, 18, 21, 16, 18 }, { 4, 3, 20, 17, 19, 20 },
125 }
126
127 @Override
128 public void step(int w, int h) {
129 for (Objects3D obj : objs) {
130 if (obj != null) {
131 obj.step(w, h);
132 }
133 }
134 }
135
136 @Override
137 public void render(int w, int h, Graphics2D g2) {
138 for (Objects3D obj : objs) {
139 if (obj != null) {
140 obj.render(g2);
141 }
142 }
143 }
144
145 public static void main(String[] argv) {
146 createDemoFrame(new Rotator3D());
147 }
148
149
150 /**
151 * 3D Objects : Solid Cube, Cube & Octahedron with polygonal faces.
152 */
153 public class Objects3D {
154
155 private final int UP = 0;
156 private final int DOWN = 1;
157 private int[][] polygons;
158 private double[][] points;
159 private int npoint;
160 private int[][] faces;
161 private int nface;
162 private int ncolour = 10;
163 private Color[][] colours = new Color[ncolour][7];
164 private double[] lightvec = { 0, 1, 1 };
165 private double Zeye = 10;
166 private double angle;
167 private Matrix3D orient, tmp, tmp2, tmp3;
168 private int scaleDirection;
169 private double scale, scaleAmt;
170 private double ix = 3.0, iy = 3.0;
171 private double[][] rotPts;
172 private int[][] scrPts;
173 private int[] xx = new int[20];
174 private int[] yy = new int[20];
175 private double x, y;
176 private int p, j;
177 private int colour;
178 private double bounce, persp;
179
180 public Objects3D(int[][] polygons,
181 double[][] points,
182 int[][] faces,
183 int w,
184 int h) {
185
186 this.polygons = polygons;
187 this.points = points;
188 this.faces = faces;
189 npoint = points.length;
190 nface = faces.length;
191
192 x = w * random();
193 y = h * random();
194
|