java3d爱心代码加字
时间: 2023-12-08 07:38:55 浏览: 189
以下是Java 3D爱心代码加字的示例:
```java
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.media.j3d.Appearance;
import javax.media.j3d.Background;
import javax.media.j3d.BoundingSphere;
import javax.media.j3d.BranchGroup;
import javax.media.j3d.Canvas3D;
import javax.media.j3d.Font3D;
import javax.media.j3d.FontExtrusion;
import javax.media.j3d.GeometryArray;
import javax.media.j3d.ImageComponent2D;
import javax.media.j3d.Material;
import javax.media.j3d.PointArray;
import javax.media.j3d.PolygonAttributes;
import javax.media.j3d.Shape3D;
import javax.media.j3d.Texture;
import javax.media.j3d.Texture2D;
import javax.media.j3d.Transform3D;
import javax.media.j3d.TransformGroup;
import javax.vecmath.Color3f;
import javax.vecmath.Point3d;
import javax.vecmath.Vector3f;
import com.sun.j3d.utils.behaviors.mouse.MouseRotate;
import com.sun.j3d.utils.behaviors.mouse.MouseTranslate;
import com.sun.j3d.utils.behaviors.mouse.MouseZoom;
import com.sun.j3d.utils.geometry.GeometryInfo;
import com.sun.j3d.utils.geometry.NormalGenerator;
import com.sun.j3d.utils.image.TextureLoader;
import com.sun.j3d.utils.universe.SimpleUniverse;
public class Java3DHeart {
public static void main(String[] args) {
// 创建一个简单的宇宙
SimpleUniverse universe = new SimpleUniverse();
// 创建一个画布
Canvas3D canvas = new Canvas3D(SimpleUniverse.getPreferredConfiguration());
// 将画布添加到宇宙中
universe.getViewingPlatform().setNominalViewingTransform();
universe.addBranchGraph(createSceneGraph(canvas));
// 将画布添加到窗口中
javax.swing.JFrame frame = new javax.swing.JFrame("Java 3D Heart");
frame.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(canvas, java.awt.BorderLayout.CENTER);
frame.setSize(500, 500);
frame.setVisible(true);
}
public static BranchGroup createSceneGraph(Canvas3D canvas) {
// 创建一个分支组
BranchGroup objRoot = new BranchGroup();
// 创建一个变换组
TransformGroup objTrans = new TransformGroup();
objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
objRoot.addChild(objTrans);
// 创建一个3D文字
Font3D font3D = new Font3D(new Font("Arial", Font.PLAIN, 1), new FontExtrusion());
GeometryInfo gi = new GeometryInfo(GeometryInfo.POLYGON_ARRAY);
gi.setFont3D(font3D);
gi.addString("I Love You", new Point3d(-2.5, -1.5, 0));
NormalGenerator ng = new NormalGenerator();
ng.generateNormals(gi);
GeometryArray ga = gi.getGeometryArray();
Appearance app = new Appearance();
Material mat = new Material();
mat.setDiffuseColor(new Color3f(Color.RED));
mat.setSpecularColor(new Color3f(Color.WHITE));
app.setMaterial(mat);
Shape3D shape = new Shape3D(ga, app);
objTrans.addChild(shape);
// 创建一个爱心
Point3d[] heartPoints = new Point3d[100];
for (int i = 0; i < 100; i++) {
double theta = i * 2 * Math.PI / 100;
double x = 16 * Math.pow(Math.sin(theta), 3);
double y = 13 * Math.cos(theta) - 5 * Math.cos(2 * theta) - 2 * Math.cos(3 * theta) - Math.cos(4 * theta);
heartPoints[i] = new Point3d(x / 20, y / 20, 0);
}
PointArray heart = new PointArray(heartPoints.length, PointArray.COORDINATES);
heart.setCoordinates(0, heartPoints);
Appearance heartAppearance = new Appearance();
TextureLoader loader = new TextureLoader("heart.jpg", canvas);
Texture texture = loader.getTexture();
heartAppearance.setTexture(texture);
PolygonAttributes polyAttr = new PolygonAttributes();
polyAttr.setCullFace(PolygonAttributes.CULL_NONE);
heartAppearance.setPolygonAttributes(polyAttr);
Shape3D heartShape = new Shape3D(heart, heartAppearance);
objTrans.addChild(heartShape);
// 创建一个背景
Background background = new Background();
background.setColor(1.0f, 1.0f, 1.0f);
background.setApplicationBounds(new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 100.0));
objRoot.addChild(background);
// 创建一个鼠标旋转行为
MouseRotate behavior1 = new MouseRotate();
behavior1.setTransformGroup(objTrans);
objTrans.addChild(behavior1);
behavior1.setSchedulingBounds(new BoundingSphere());
// 创建一个鼠标平移行为
MouseTranslate behavior2 = new MouseTranslate();
behavior2.setTransformGroup(objTrans);
objTrans.addChild(behavior2);
behavior2.setSchedulingBounds(new BoundingSphere());
// 创建一个鼠标缩放行为
MouseZoom behavior3 = new MouseZoom();
behavior3.setTransformGroup(objTrans);
objTrans.addChild(behavior3);
behavior3.setSchedulingBounds(new BoundingSphere());
// 创建一个旋转变换
Transform3D yAxis = new Transform3D();
Alpha rotationAlpha = new Alpha(-1, 4000);
RotationInterpolator rotator = new RotationInterpolator(rotationAlpha, objTrans, yAxis, 0.0f, (float) Math.PI * 2.0f);
BoundingSphere bounds = new BoundingSphere();
rotator.setSchedulingBounds(bounds);
objTrans.addChild(rotator);
return objRoot;
}
}
```
注意:上述代码中的“heart.jpg”是爱心的纹理图片,需要将其放在与Java文件相同的目录下。
阅读全文