java3d

import java.awt.Color;
import com.sun.j3d.utils.geometry.GeometryInfo;
import com.sun.j3d.utils.geometry.NormalGenerator;
import com.sun.j3d.utils.universe.SimpleUniverse;
import javax.media.j3d.*;
import javax.vecmath.*;

// An Egyptian pyramid
// Base divided into two triangles

public class PyramidExample {
public static void main(String[] args) {
SimpleUniverse universe = new SimpleUniverse();
BranchGroup group = new BranchGroup();

Point3f e = new Point3f(1.0f, 0.0f, 0.0f); // east
Point3f s = new Point3f(0.0f, 0.0f, 1.0f); // south
Point3f w = new Point3f(-1.0f, 0.0f, 0.0f); // west
Point3f n = new Point3f(0.0f, 0.0f, -1.0f); // north
Point3f t = new Point3f(0.0f, 0.721f, 0.0f); // top

TriangleArray pyramidGeometry = new TriangleArray(18,
TriangleArray.COORDINATES);
pyramidGeometry.setCoordinate(0, e);
pyramidGeometry.setCoordinate(1, t);
pyramidGeometry.setCoordinate(2, s);

pyramidGeometry.setCoordinate(3, s);
pyramidGeometry.setCoordinate(4, t);
pyramidGeometry.setCoordinate(5, w);

pyramidGeometry.setCoordinate(6, w);
pyramidGeometry.setCoordinate(7, t);
pyramidGeometry.setCoordinate(8, n);

pyramidGeometry.setCoordinate(9, n);
pyramidGeometry.setCoordinate(10, t);
pyramidGeometry.setCoordinate(11, e);

pyramidGeometry.setCoordinate(12, e);
pyramidGeometry.setCoordinate(13, s);
pyramidGeometry.setCoordinate(14, w);

pyramidGeometry.setCoordinate(15, w);
pyramidGeometry.setCoordinate(16, n);
pyramidGeometry.setCoordinate(17, e);
GeometryInfo geometryInfo = new GeometryInfo(pyramidGeometry);
NormalGenerator ng = new NormalGenerator();
ng.generateNormals(geometryInfo);

GeometryArray result = geometryInfo.getGeometryArray();

// yellow appearance
Appearance appearance = new Appearance();
Color3f color = new Color3f(Color.yellow);
Color3f black = new Color3f(0.0f, 0.0f, 0.0f);
Color3f white = new Color3f(1.0f, 1.0f, 1.0f);
Texture texture = new Texture2D();
TextureAttributes texAttr = new TextureAttributes();
texAttr.setTextureMode(TextureAttributes.MODULATE);
texture.setBoundaryModeS(Texture.WRAP);
texture.setBoundaryModeT(Texture.WRAP);
texture.setBoundaryColor(new Color4f(0.0f, 1.0f, 0.0f, 0.0f));
Material mat = new Material(color, black, color, white, 70f);
appearance.setTextureAttributes(texAttr);
appearance.setMaterial(mat);
appearance.setTexture(texture);
Shape3D shape = new Shape3D(result, appearance);
group.addChild(shape);

// above pyramid
Vector3f viewTranslation = new Vector3f();
viewTranslation.z = 3;
viewTranslation.x = 0f;
viewTranslation.y = .3f;
Transform3D viewTransform = new Transform3D();
viewTransform.setTranslation(viewTranslation);
Transform3D rotation = new Transform3D();
rotation.rotX(-Math.PI / 12.0d);
rotation.mul(viewTransform);
universe.getViewingPlatform().getViewPlatformTransform().setTransform(
rotation);
universe.getViewingPlatform().getViewPlatformTransform().getTransform(
viewTransform);

// lights
BoundingSphere bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0),
1000.0);
Color3f light1Color = new Color3f(.7f, .7f, .7f);
Vector3f light1Direction = new Vector3f(4.0f, -7.0f, -12.0f);
DirectionalLight light1 = new DirectionalLight(light1Color, light1Direction);
light1.setInfluencingBounds(bounds);
group.addChild(light1);
Color3f ambientColor = new Color3f(.4f, .4f, .4f);
AmbientLight ambientLightNode = new AmbientLight(ambientColor);
ambientLightNode.setInfluencingBounds(bounds);
group.addChild(ambientLightNode);

universe.addBranchGraph(group);
}
}

 


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.media.j3d.*;
import javax.vecmath.*;
import com.sun.j3d.utils.universe.*;
import com.sun.j3d.utils.geometry.*;

public class Titles {

public static void main(String[] args) {
Titles t = new Titles();
t.setUp();
}

public void setUp() {
JFrame jf = new JFrame(“Welcome”);
// kill the window on close
jf.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent winEvent) {
System.exit(0);
}
});
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(1, 1, 2, 2));

GraphicsConfiguration config = SimpleUniverse
.getPreferredConfiguration();
Canvas3D canvas3D = new Canvas3D(config);
canvas3D.setSize(360, 160);
SimpleUniverse universe = new SimpleUniverse(canvas3D);
BranchGroup group = new BranchGroup();
addObjects(group);
addLights(group);
universe.getViewingPlatform().setNominalViewingTransform();
universe.addBranchGraph(group);
panel.add(canvas3D);
jf.getContentPane().add(panel, BorderLayout.CENTER);
jf.pack();
jf.setVisible(true);
}

public void addLights(BranchGroup group) {
BoundingSphere bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0),
1000.0);

Color3f light1Color = new Color3f(1.0f, 1.0f, 1.0f);
Vector3f light1Direction = new Vector3f(4.0f, -7.0f, -12.0f);
DirectionalLight light1 = new DirectionalLight(light1Color,
light1Direction);
light1.setInfluencingBounds(bounds);
group.addChild(light1);

// Set up the ambient light
Color3f ambientColor = new Color3f(.1f, .1f, .1f);
AmbientLight ambientLightNode = new AmbientLight(ambientColor);
ambientLightNode.setInfluencingBounds(bounds);
group.addChild(ambientLightNode);
}

private void addObjects(BranchGroup group) {
Font3D f3d = new Font3D(new Font(“TestFont”, Font.PLAIN, 2),
new FontExtrusion());
Text3D text = new Text3D(f3d, new String(“Java3D.org”), new Point3f(-3.5f,
-.5f, -4.5f));

text.setString(“Java3D.org”);
Color3f white = new Color3f(1.0f, 1.0f, 1.0f);
Color3f blue = new Color3f(.2f, 0.2f, 0.6f);
Appearance a = new Appearance();
Material m = new Material(blue, blue, blue, white, 80.0f);
m.setLightingEnable(true);
a.setMaterial(m);

Shape3D sh = new Shape3D();
sh.setGeometry(text);
sh.setAppearance(a);
TransformGroup tg = new TransformGroup();
Transform3D t3d = new Transform3D();
Transform3D tDown = new Transform3D();
Transform3D rot = new Transform3D();
Vector3f v3f = new Vector3f(-1.6f, -1.35f, -6.5f);
t3d.setTranslation(v3f);
rot.rotX(Math.PI / 5);
t3d.mul(rot);
v3f = new Vector3f(0, -1.4f, 0f);
tDown.setTranslation(v3f);
t3d.mul(tDown);
tg.setTransform(t3d);
tg.addChild(sh);
group.addChild(tg);

}
}

 

import java.applet.Applet;
import java.awt.FlowLayout;
import java.awt.event.*;
import java.awt.GraphicsConfiguration;
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.universe.*;
import javax.media.j3d.*;
import javax.vecmath.*;
import javax.swing.*;
import com.sun.j3d.utils.behaviors.mouse.*;
import com.sun.j3d.utils.behaviors.keyboard.KeyNavigatorBehavior;
import com.sun.j3d.loaders.objectfile.ObjectFile;
import com.sun.j3d.loaders.Scene;
import java.net.URL;
import java.util.Hashtable;
import java.util.Enumeration;
public class RedGreenGirl extends Applet {

protected Canvas3D c1 = new Canvas3D(SimpleUniverse.getPreferredConfiguration());
// private Canvas3D c2 = new Canvas3D(SimpleUniverse.getPreferredConfiguration());
private static MainFrame mf;
protected SimpleUniverse u = null;
protected BranchGroup scene = null;
protected String URLString = “http://www.java3d.org/renee.obj”;
protected float eyeOffset =0.03F;
protected static int size=600;
public void init() {
setLayout(new FlowLayout());
GraphicsConfiguration config =
SimpleUniverse.getPreferredConfiguration();
String v = getParameter(“url”);
if (v != null) { URLString = v; }
c1.setSize(size, size);
add(c1);
scene = createSceneGraph(0);
u = new SimpleUniverse(c1);
// This will move the ViewPlatform back a bit so the
// objects in the scene can be viewed.
//u.getViewingPlatform().setNominalViewingTransform();
u.addBranchGraph(scene);

}
public BranchGroup createSceneGraph(int i) {
System.out.println(“Creating scene for: “+URLString);
// Create the root of the branch graph
BranchGroup objRoot = new BranchGroup();
try{
Transform3D myTransform3D = new Transform3D();
myTransform3D.setTranslation(new Vector3f(+0.0f,-0.15f,-3.6f));
TransformGroup objTrans = new TransformGroup(myTransform3D);
objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
Transform3D t = new Transform3D();
TransformGroup tg = new TransformGroup(t);
tg.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
tg.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
objTrans.addChild(tg);
URL url = new URL(URLString) ;
ObjectFile f = new ObjectFile();
f.setFlags(ObjectFile.RESIZE | ObjectFile.TRIANGULATE | ObjectFile.STRIPIFY);
System.out.println( “About to load” );

Scene s = f.load(url);
Transform3D myTrans = new Transform3D();
myTrans.setTranslation(new Vector3f(eyeOffset, -eyeOffset, 0F));
TransformGroup mytg = new TransformGroup(myTrans);
//mytg.addChild(s.getSceneGroup());
tg.addChild(mytg);
Transform3D myTrans2 = new Transform3D();
myTrans2.setTranslation(new Vector3f(-eyeOffset, +eyeOffset, 0F));
TransformGroup mytg2 = new TransformGroup(myTrans2);
//mytg2.addChild(s.getSceneGroup());
Hashtable table = s.getNamedObjects();
for (Enumeration e = table.keys() ; e.hasMoreElements() 😉 {
Object key = e.nextElement();
System.out.println(key);
Object obj = table.get(key);
System.out.println(obj.getClass().getName());
Shape3D shape = (Shape3D)obj;
//shape.setCapability(Shape3D.ALLOW_APPEARANCE_WRITE);
Appearance ap = new Appearance();
Color3f black = new Color3f(0.0f, 0.0f, 0.0f);
Color3f red = new Color3f(0.7f, .0f, .15f);
Color3f green = new Color3f(0f, .7f, .15f);
ap.setMaterial(new Material(green,black, green, black, 1.0f));
Appearance ap2 = new Appearance();
ap2.setMaterial(new Material(red, black, red, black, 1.0f));
float transparencyValue = 0.5f;
TransparencyAttributes t_attr =
new TransparencyAttributes(
TransparencyAttributes.BLENDED,
transparencyValue,
TransparencyAttributes.BLEND_SRC_ALPHA,
TransparencyAttributes.BLEND_ONE);
ap2.setTransparencyAttributes( t_attr );
ap2.setRenderingAttributes( new RenderingAttributes() );
ap.setTransparencyAttributes( t_attr );
ap.setRenderingAttributes( new RenderingAttributes() );
// bg.addChild(ap);
shape.setAppearance(ap);
mytg2.addChild(new Shape3D(shape.getGeometry(),ap2));
mytg.addChild(new Shape3D(shape.getGeometry(),ap));
}
tg.addChild(mytg2);
System.out.println( “Finished Loading” );
BoundingSphere bounds =
new BoundingSphere(new Point3d(0.0,0.0,0.0), 100.0);
Color3f light1Color = new Color3f(.9f, 0.9f, 0.9f);
Vector3f light1Direction = new Vector3f(4.0f, -7.0f, -12.0f);
DirectionalLight light1
= new DirectionalLight(light1Color, light1Direction);
light1.setInfluencingBounds(bounds);
objTrans.addChild(light1);
// Set up the ambient light
Color3f ambientColor = new Color3f(1.0f, .4f, 0.3f);
AmbientLight ambientLightNode = new AmbientLight(ambientColor);
ambientLightNode.setInfluencingBounds(bounds);
objTrans.addChild(ambientLightNode);

MouseRotate behavior = new MouseRotate();
behavior.setTransformGroup(tg);
objTrans.addChild(behavior);
// Create the translate behavior node
MouseTranslate behavior3 = new MouseTranslate();
behavior3.setTransformGroup(tg);
objTrans.addChild(behavior3);
behavior3.setSchedulingBounds(bounds);

KeyNavigatorBehavior keyNavBeh = new KeyNavigatorBehavior(tg);
keyNavBeh.setSchedulingBounds(new BoundingSphere(
new Point3d(),1000.0));
objTrans.addChild(keyNavBeh);

behavior.setSchedulingBounds(bounds);
objRoot.addChild(objTrans);
} catch(Throwable t){System.out.println(“Error: “+t);}
return objRoot;
}
public RedGreenGirl() {
}
public void destroy() {
u.removeAllLocales();
}

public static void main(String[] args) {

RedGreenGirl s = new RedGreenGirl();
if (args.length > 0) {
s.URLString = args[0];
}
mf = new MainFrame(s, size, size);
}
}

 

cube3d.java

import java.applet.*;
import java.awt.*;

@SuppressWarnings(“serial”)
public class cube3d extends Applet implements Runnable {
private Thread my_Thread = null;
private Image my_offscreen=null;
private Graphics g2;

private double rotatex=10;
private double rotatey=0;
private double rotatez=0;

@SuppressWarnings(“static-access”)
private int threadpriority=my_Thread.MIN_PRIORITY;
private Color bgcolor=new Color(0,0,0);
private Color fgcolor=new Color(0,255,0);

private double centerx;
private double centery;

private double x[]=new double[10];
private double y[]=new double[10];
private double z[]=new double[10];
private int xx[]=new int[10];
private int yy[]=new int[10];

private double pi=3.14159265;

@SuppressWarnings(“deprecation”)
public void init()
{
setAppletSize(this.size().width, this.size().height);
//rotatex=(double)(Integer.parseInt(getParameter(“Xrot”)))/2;
//rotatey=(double)(Integer.parseInt(getParameter(“Yrot”)))/2;
//rotatez=(double)(Integer.parseInt(getParameter(“Zrot”)))/2;
rotatex=30;
rotatey=30;
rotatez=30;

//bgcolor=new Color(Integer.parseInt(getParameter(“bgcolor”),16));
//fgcolor=new Color(Integer.parseInt(getParameter(“fgcolor”),16));
bgcolor = new Color(0x000000);
fgcolor= new Color(0xff0000);

rotatex=(rotatex/180*pi)/20;
rotatey=(rotatey/180*pi)/20;
rotatez=(rotatez/180*pi)/20;

x[1]=-1;y[1]=-1;z[1]=-1;
x[2]=1;y[2]=-1;z[2]=-1;
x[3]=1;y[3]=-1;z[3]=1;
x[4]=-1;y[4]=-1;z[4]=1;
x[5]=-1;y[5]=1;z[5]=-1;
x[6]=1;y[6]=1;z[6]=-1;
x[7]=1;y[7]=1;z[7]=1;
x[8]=-1;y[8]=1;z[8]=1;

setBackground(bgcolor);
my_offscreen=createImage(this.size().width,this.size().height);
g2=my_offscreen.getGraphics();
}

public void setAppletSize(int ww, int hh)
{
centerx=(double)ww/2;
centery=(double)hh/2;
}

public void destroy()
{

}
public void paint(Graphics gx)
{

}

public void start()
{
if(my_Thread==null)
{
my_Thread=new Thread(this);
my_Thread.start();
my_Thread.setPriority(threadpriority);

}
}

@SuppressWarnings(“deprecation”)
public void stop()
{
if(my_Thread !=null)
{
my_Thread.stop();
my_Thread = null;
}
}

@SuppressWarnings(“static-access”)
@Override
public void run() {
// TODO Auto-generated method stub
while(true)
{
repaint();
try
{
my_Thread.sleep(20);

}
catch(InterruptedException e)
{
System.out.println(“인터럽트가 발생함!”);
}
}
}

public void update(Graphics g)
{
double qx,qy,qz;

for(int i=1;i<=8;i++)
{
qy=y[i];
qz=z[i];
y[i]=qy*Math.cos(rotatex)-qz*Math.sin(rotatex);
z[i]=qz*Math.cos(rotatex)+qy*Math.sin(rotatex);

qz=z[i];
qx=x[i];
z[i]=qz*Math.cos(rotatey)-qx*Math.sin(rotatey);
x[i]=qx*Math.cos(rotatey)+qz*Math.sin(rotatey);

qx=x[i];
qy=y[i];
x[i]=qx*Math.cos(rotatez)-qy*Math.sin(rotatez);
y[i]=qy*Math.cos(rotatez)+qx*Math.sin(rotatez);
}

for(int i=1;i<9;i++)
{
xx[i]=(int)(x[i]*(5/(5+z[i]))*centerx/2+centerx);
yy[i]=(int)(y[i]*(5/(5+z[i]))*centery/2+centery);
}

g2.setColor(bgcolor);
g2.fillRect(0,0,(int)(centerx*2),(int)(centery*2));

g2.setColor(fgcolor);

for(int i=1;i<5;i++)
{
g2.drawLine(xx[i],yy[i],xx[i+4],yy[i+4]);
g2.drawLine(xx[i],yy[i],xx[(i%4)+1],yy[(i%4)+1]);
g2.drawLine(xx[i+4],yy[i+4],xx[(i%4)+5],yy[(i%4)+5]);
}

paint(g2);
g.drawImage(my_offscreen,0,0,this);

}
}

 

[HTML code] cube3d.html

<HTML>
<TITLE>쓰레드를 활용한 3차원 정육면체</TITLE>
<BODY>
<CENTER>
<APPLET CODE=Titles.class WIDTH=200 HEIGHT=200>
<PARAM NAME=Xrot VALUE=30>
<PARAM NAME=Yrot VALUE=30>
<PARAM NAME=Zrot VALUE=30>
<PARAM NAME=bgcolor VALUE=000000>
<PARAM NAME=fgcolor VALUE=ff0000>
</APPLET>
</CENTER>
</BODY>
</HTML>

mystars

myStars.java

import java.applet.*;
import java.awt.*;

public class myStars extends Applet implements Runnable
{
private Thread instance;
private boolean appletRunning;
private Graphics graphicsSurface;

private final int MAX_STARS=200;
private int stars[][];

public myStars()
{

}

//초기화 과정
public void init()
{
stars = new int[MAX_STARS][3];

setSize(400,300);
setBackground(Color.black);
setVisible(true);
}

public void start()
{
appletRunning=true;
instance=new Thread(this);
instance.start();
}

public void stop()
{
appletRunning=false;
instance=null;
}

public void paint(Graphics g)
{

}
public void update(Graphics g)
{

}
public void resetStar(int number)
{
stars[number][0]=(int)(Math.random()*2000-1000);
stars[number][1]=(int)(Math.random()*2000-1000);
stars[number][2]=(int)(Math.random()*5000);
if(stars[number][0]==0) stars[number][0]=5;
if(stars[number][1]==0) stars[number][1]=5;
if(stars[number][2]==0) stars[number][2]=5;
}

public void run()
{
int x,y,nx,ny,dist;
int counter=0;
float rotation=0.0f;
for(int i=0;i<MAX_STARS;i++) resetStar(i);

graphicsSurface=getGraphics();
while(appletRunning)
{
graphicsSurface.clearRect(0,0,400,300);
for(int i=0;i<MAX_STARS;i++)
{
x=256*stars[i][0]/stars[i][2];
y=256*stars[i][1]/stars[i][2];
nx=(int)(x*Math.cos(rotation)-y*Math.sin(rotation));
ny=(int)(x*Math.sin(rotation)+y*Math.cos(rotation));

if(stars[i][2]>3000) graphicsSurface.setColor(Color.darkGray);
else if(stars[i][2]>2000) graphicsSurface.setColor(Color.gray);
else if(stars[i][2]>1000) graphicsSurface.setColor(Color.lightGray);
else graphicsSurface.setColor(Color.white);

graphicsSurface.drawLine(200+nx,150+ny,nx+201,150+ny);

stars[i][2]-=15;
if(stars[i][2]<5) resetStar(i);
if((x+300<0)||(x+100>400)) resetStar(i);
if((y+300<0)||(y+100>300)) resetStar(i);
}
counter++;
if(counter>1600) counter=0;
else if(counter>1200) rotation-=0.01f;
else if(counter>800) {}
else if(counter>400) rotation+=0.01f;

try{instance.sleep(15);}
catch(InterruptedException ie){}
}

}
}

 

3 thoughts on “java3d

    • Certainly I like your web site, however you have to take a look at the spelling on quite a few of your posts. Several of them are rife with spelling problems and I find it very botmersohe to inform you. However I’ll certainly come again again!

  1. Hi I am so excited I found your site, I really found you by mistake, while I was searching on Askjeeve for something else, Anyhow I am here now and would just like to say thanks a lot for a marvelous post and a all round thrilling blog (I also love the thnde/mesige), I don’t have time to read it all at the moment but I have saved it and also added in your RSS feeds, so when I have time I will be back to read much more, Please do keep up the excellent job.

답글 남기기

이메일 주소를 발행하지 않을 것입니다. 필수 항목은 *(으)로 표시합니다