Total members 11895 |It is currently Sun Dec 22, 2024 2:13 pm Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





Programmer: Mohamed sami isamil.
Program: 3d-cube.
Abilities: Move 3d cube.
Technology used: NetBeans
Format: NetBeans Project

How 3d cube works?

you use up and down arrows to move toward the cube.

Program is attached, with a picture.
Cube3d.java
java code
package graphices;

import java. util.ArrayList;


public class Cube3D {

/** Creates a new instance of Cube3D */
public Cube3D() {
create_cube();
}

public void create_cube() {

temp=new Polygon3D(new Vector3D[]{
new Vector3D(0,0,0),
new Vector3D(100,0,0),
new Vector3D(100,100,0),
new Vector3D(0,100,0),
});
Polygons.add(temp);//add the Polygon to the cub

temp=new Polygon3D(new Vector3D[]{
new Vector3D(100,0,0),
new Vector3D(100,0,-100),
new Vector3D(100,100,-100),
new Vector3D(100,100,0),
});
Polygons.add(temp);//add the Polygon to the cub

temp=new Polygon3D(new Vector3D[]{
new Vector3D(0,100,0),
new Vector3D(0,100,-100),
new Vector3D(0,0,-100),
new Vector3D(0,0,0),
});
Polygons.add(temp);//add the Polygon to the cub

temp=new Polygon3D(new Vector3D[]{
new Vector3D(0,0,-100),
new Vector3D(100,0,-100),
new Vector3D(100,100,-100),
new Vector3D(0,100,-100),
});
Polygons.add(temp);//add the Polygon to the cub

temp=new Polygon3D(new Vector3D[]{
new Vector3D(0,100,0),
new Vector3D(100,100,0),
new Vector3D(100,100,-100),
new Vector3D(0,100,-100),
});
Polygons.add(temp);//add the Polygon to the cub


}

public ArrayList getPolygons() {
return Polygons;
}

private ArrayList Polygons=new ArrayList();
private Polygon3D temp;

}

Draw3d.java

java code
package graphices;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.geom.GeneralPath;
import java.util.ArrayList;


public class Draw3D {

/** Creates a new instance of Draw3D */
public Draw3D(int width,int height,Transform3D transformer,ViewWindow window) {
this.width=width;
this.height=height;
this.transformer=transformer;
this.window=window;
}

public void draw3D(Graphics2D g,ArrayList polygons) {
draw_Background(g);//draw the background of the screen


for(int k=0;k<polygons.size();k++) {
drawPol.setTo(((Polygon3D)polygons.get(k)));


Before_Draw(drawPol);//transform+Project to the polygon

myPath=new GeneralPath();//every time there is a new GeneralPath shape


temp=drawPol.getVertex(0);
myPath.moveTo(temp.x,temp.y);

for(int i=1;i<drawPol.getNumVertices();i++) {
temp=drawPol.getVertex(i);
myPath.lineTo(temp.x,temp.y);

}
temp=drawPol.getVertex(0);
myPath.lineTo(temp.x,temp.y);


g.setColor(polygons_colors[k]);
g.draw(myPath);
}

}

public void draw_Background(Graphics2D g ) {

g.setColor(Color.BLACK);
g.fillRect(0,0,width,height);

}
public void Before_Draw(Polygon3D polygon) {

polygon.add(transformer);
polygon.project(window);
}

private final int width;
private final int height;
private final Transform3D transformer;
private final ViewWindow window;
private Polygon3D drawPol=new Polygon3D();
private GeneralPath myPath;
private Color[] polygons_colors=new Color[]{Color.RED,Color.YELLOW,Color.PINK,Color.GREEN,Color.WHITE,
Color.orange,};
private Vector3D temp;
}


Main.java
java code
package graphices;

import java.awt.Color;
import java.awt.DisplayMode;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Point;
import java.awt.geom.GeneralPath;
import java.awt.image.BufferStrategy;
import java.util.ArrayList;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JFrame;

public class main extends JFrame{

/** Creates a new instance of main */
public main() {
setUndecorated(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
setIgnoreRepaint(true);

myDevice.setFullScreenWindow(this);
myDevice.setDisplayMode(myMode);
createBufferStrategy(2);
addKeyListener(mover);
setCursor( getToolkit().getDefaultToolkit().createCustomCursor(new ImageIcon(" ").getImage(),new Point(0,0)
,"invisble"));

Loop();

}

public static void main(String[] args) {

main m= new main();

}
public void Loop() {
BufferStrategy myBuffer=getBufferStrategy();
while(true) {

Graphics2D g=(Graphics2D)myBuffer.getDrawGraphics();
draw(g) ;
myBuffer=getBufferStrategy();
g.dispose();

myBuffer.show();
try {


Thread.sleep(10);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}

}
public void draw(Graphics2D g2d) {
GeneralPath myP=new GeneralPath();
myDrawer.draw3D(g2d,myCube.getPolygons());

g2d.setColor(Color.WHITE);

g2d.drawString("Press UP / DOWN / ESC ",20.0f,20.0f);
g2d.drawString("Press Q/A Rotation Z ",20.0f,40.0f);
g2d.drawString("Press W/S Rotation X ",20.0f,60.0f);
g2d.drawString("Press E/D Rotation Y ",20.0f,80.0f);
g2d.drawString("Shape: Cube ",20.0f,100.0f);

}
private final int width=800;
private final int height=600;
private GraphicsEnvironment myenv=GraphicsEnvironment.getLocalGraphicsEnvironment();
private GraphicsDevice myDevice=myenv.getDefaultScreenDevice();
private DisplayMode myMode=new DisplayMode(width,height,32,DisplayMode.REFRESH_RATE_UNKNOWN);
private Vector3D[] v=new Vector3D[]{new Vector3D(0, 0, 0),new Vector3D(100, 0, 0),new Vector3D(100, 150,0),new Vector3D(0, 150,0)};
private Polygon3D myPol=new Polygon3D(v);
private Polygon3D drawPol=new Polygon3D();
private Transform3D trans=new Transform3D(0.0f,-100.0f,-400.0f);
private ViewWindow myWindow=new ViewWindow(0,0,width,height, (float)Math.toRadians(75));
private MoveManager mover=new MoveManager(trans);
private Draw3D myDrawer=new Draw3D(width,height,trans,myWindow);
private Cube3D myCube=new Cube3D();

}

MoveManger.java
java code
package graphices;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

public class MoveManager implements KeyListener {

/** Creates a new instance of MoveManager */
public MoveManager(Transform3D trans) {
this.trans=trans;
v=trans.getLocation();
}

public void keyTyped(KeyEvent e) {
}

public void keyPressed(KeyEvent e) {
if(e.getKeyCode()==UP) {
v.setTo(v.x,v.y,v.z+10);
} else if(e.getKeyCode()==DOWN) {
v.setTo(v.x,v.y,v.z-10);
} else if(e.getKeyCode()==e.VK_ESCAPE) {
System.exit(0);// Exit the Game and Close all Connections
} else if(e.getKeyChar()=='w') {
trans.rotateAngleX(0.01f);
} else if (e.getKeyChar()=='s') {
trans.rotateAngleX(-0.01f);
} else if(e.getKeyChar()=='e') {
trans.rotateAngleY(0.01f);
} else if(e.getKeyChar()=='d') {
trans.rotateAngleY(-0.01f);
} else if(e.getKeyChar()=='q') {
trans.rotateAngleZ(0.01f);
} else if(e.getKeyChar()=='a') {
trans.rotateAngleZ(-0.01f);
}
e.consume();
}

public void keyReleased(KeyEvent e) {
}

private final int UP=38;
private final int DOWN=40;
private Transform3D trans;
private Vector3D v;
}


Polygon3D.java
java code
package graphices;

public class Polygon3D {

/** Creates a new instance of Polygon3D */
private Vector3D[] v;
private int numVertices;

/**
Creates an empty polygon that can be used as a "scratch"
polygon for transforms, projections, etc.
*/
public Polygon3D() {
numVertices = 0;
v = new Vector3D[0];
}


/**
Creates a new Polygon3D with the specified vertices.
*/
public Polygon3D(Vector3D v0, Vector3D v1, Vector3D v2) {
this(new Vector3D[] { v0, v1, v2 });
}


/**
Creates a new Polygon3D with the specified vertices. All
the vertices are assumed to be in the same plane.
*/
public Polygon3D(Vector3D v0, Vector3D v1, Vector3D v2,
Vector3D v3)
{
this(new Vector3D[] { v0, v1, v2, v3 });
}


/**
Creates a new Polygon3D with the specified vertices. All
the vertices are assumed to be in the same plane.
*/
public Polygon3D(Vector3D[] vertices)
{
this.v = vertices;
numVertices = vertices.length;
}


/**
Sets this polygon to the same vertices as the specified
polygon.
*/
public void setTo(Polygon3D polygon)
{
numVertices = polygon.numVertices;

ensureCapacity(numVertices);
for (int i=0; i<numVertices; i++) {
v[i].setTo(polygon.v[i]);
}
}


/**
Ensures this polygon has enough capacity to hold the
specified number of vertices.
*/
protected void ensureCapacity(int length)
{
if (v.length < length) {
System.out.println("GA");
Vector3D[] newV = new Vector3D[length];
System.arraycopy(v,0,newV,0,v.length);
for (int i=v.length; i<newV.length; i++) {
newV[i] = new Vector3D();
}
v = newV;
}
}


/**
Gets the number of vertices this polygon has.
*/
public int getNumVertices() {
return numVertices;
}


/**
Gets the vertex at the specified index.
*/
public Vector3D getVertex(int index) {
return v[index];
}


/**
Projects this polygon onto the view window.
*/
public void project(ViewWindow view)
{
System.out.println("numVertices ="+numVertices);
for (int i=0; i<numVertices; i++) {
view.project(v[i]);
}
}
public void add(Transform3D trans)
{
for(int i=0;i<numVertices;i++)
{
v[i].add(trans);
}
}


protected Object clone() throws CloneNotSupportedException
{
return new Polygon3D(v);
}

}

Transform3d.java
java code
package graphices;

public class Transform3D {

/** Creates a new instance of Transform3D */
protected Vector3D location;
private float cosAngleX;
private float sinAngleX;
private float cosAngleY;
private float sinAngleY;
private float cosAngleZ;
private float sinAngleZ;

/**
Creates a new Transform3D with no translation or rotation.
*/
public Transform3D() {
this(0,0,0);
}


/**
Creates a new Transform3D with the specified translation
and no rotation.
*/
public Transform3D(float x, float y, float z) {
location = new Vector3D(x, y, z);
setAngle(0,0,0);
}


/**
Creates a new Transform3D
*/
public Transform3D(Transform3D v) {
location = new Vector3D();
setTo(v);
}


public Object clone() {
return new Transform3D(this);
}


/**
Sets this Transform3D to the specified Transform3D.
*/
public void setTo(Transform3D v) {
location.setTo(v.location);
this.cosAngleX = v.cosAngleX;
this.sinAngleX = v.sinAngleX;
this.cosAngleY = v.cosAngleY;
this.sinAngleY = v.sinAngleY;
this.cosAngleZ = v.cosAngleZ;
this.sinAngleZ = v.sinAngleZ;
}


/**
Gets the location (translation) of this transform.
*/
public Vector3D getLocation() {
return location;
}

public float getCosAngleX() {
return cosAngleX;
}

public float getSinAngleX() {
return sinAngleX;
}

public float getCosAngleY() {
return cosAngleY;
}

public float getSinAngleY() {
return sinAngleY;
}

public float getCosAngleZ() {
return cosAngleZ;
}

public float getSinAngleZ() {
return sinAngleZ;
}

public float getAngleX() {
return (float)Math.atan2(sinAngleX, cosAngleX);
}

public float getAngleY() {
return (float)Math.atan2(sinAngleY, cosAngleY);
}

public float getAngleZ() {
return (float)Math.atan2(sinAngleZ, cosAngleZ);
}

public void setAngleX(float angleX) {
cosAngleX = (float)Math.cos(angleX);
sinAngleX = (float)Math.sin(angleX);
}

public void setAngleY(float angleY) {
cosAngleY = (float)Math.cos(angleY);
sinAngleY = (float)Math.sin(angleY);
}

public void setAngleZ(float angleZ) {
cosAngleZ = (float)Math.cos(angleZ);
sinAngleZ = (float)Math.sin(angleZ);
}

public void setAngle(float angleX, float angleY, float angleZ)
{
setAngleX(angleX);
setAngleY(angleY);
setAngleZ(angleZ);
}

public void rotateAngleX(float angle) {
if (angle != 0) {
setAngleX(getAngleX() + angle);
}
}

public void rotateAngleY(float angle) {
if (angle != 0) {
setAngleY(getAngleY() + angle);
}
}

public void rotateAngleZ(float angle) {
if (angle != 0) {
setAngleZ(getAngleZ() + angle);
}
}

public void rotateAngle(float angleX, float angleY,
float angleZ)
{
rotateAngleX(angleX);
rotateAngleY(angleY);
rotateAngleZ(angleZ);
}

}


Vector3d.java
java code
package graphices;

public class Vector3D {

/** Creates a new instance of Vector3D */

public float x;
public float y;
public float z;
public float oldX;
public float oldY;


/**
* Creates a new Vector3D at (0,0,0).
*/
public Vector3D() {
this(0,0,0);
}


/**
* Creates a new Vector3D with the same values as the
* specified Vector3D.
*/
public Vector3D(Vector3D v) {
this(v.x, v.y, v.z);
}


/**
* Creates a new Vector3D with the specified (x, y, z) values.
*/
public Vector3D(float x, float y, float z) {
setTo(x, y, z);
}


/**
* Checks if this Vector3D is equal to the specified Object.
* They are equal only if the specified Object is a Vector3D
* and the two Vector3D's x, y, and z coordinates are equal.
*/
public boolean equals(Object obj) {
Vector3D v = (Vector3D)obj;
return (v.x == x && v.y == y && v.z == z);
}


/**
* Checks if this Vector3D is equal to the specified
* x, y, and z coordinates.
*/
public boolean equals(float x, float y, float z) {
return (this.x == x && this.y == y && this.z == z);
}


/**
* Sets the vector to the same values as the specified
* Vector3D.
*/
public void setTo(Vector3D v) {
setTo(v.x, v.y, v.z);
}


/**
* Sets this vector to the specified (x, y, z) values.
*/
public void setTo(float x, float y, float z) {

this.x =x;
this.y =y;
this.z = z;
System.out.println("--------X= "+x+" Y="+y+" Z "+z );
}


/**
* Adds the specified (x, y, z) values to this vector.
*/
public void add(float x, float y, float z) {
this.x+=x;
this.y+=y;
this.z+=z;
oldX=x;
oldY=y;
}


/**
* Subtracts the specified (x, y, z) values to this vector.
*/
public void subtract(float x, float y, float z) {
add(-x, -y, -z);
}


/**
* Adds the specified vector to this vector.
*/
public void add(Vector3D v) {
add(v.x, v.y, v.z);
}


/**
* Subtracts the specified vector from this vector.
*/
public void subtract(Vector3D v) {
add(-v.x, -v.y, -v.z);
}


/**
* Multiplies this vector by the specified value. The new
* length of this vector will be length()*s.
*/
public void multiply(float s) {
x*=s;
y*=s;
z*=s;
oldX=x;
oldY=y;
}


/**
* Divides this vector by the specified value. The new
* length of this vector will be length()/s.
*/
public void divide(float s) {
x/=s;
y/=s;
z/=s;
oldX=x;
oldY=y;
}


/**
* Returns the length of this vector as a float.
*/
public float length() {
return (float)Math.sqrt(x*x + y*y + z*z);
}


/**
* Converts this Vector3D to a unit vector, or, in other
* words, a vector of length 1. Same as calling
* v.divide(v.length()).
*/
public void normalize() {
divide(length());
}


/**
* Converts this Vector3D to a String representation.
*/
/**
* Rotate this vector around the x-axis the specified amount,
* using precomputed cosine and sine values of the angle to
* rotate.
*/
public void rotateX(float cosAngle, float sinAngle) {
float newY = y*cosAngle - z*sinAngle;
float newZ = y*sinAngle + z*cosAngle;
y = newY;
z = newZ;

}


/**
Rotate this vector around the y-axis the specified amount,
using precomputed cosine and sine values of the angle to
rotate.
*/
public void rotateY(float cosAngle, float sinAngle) {
float newX = z*sinAngle + x*cosAngle;
float newZ = z*cosAngle - x*sinAngle;
x = newX;
z = newZ;

}


/**
Rotate this vector around the y-axis the specified amount,
using precomputed cosine and sine values of the angle to
rotate.
*/
public void rotateZ(float cosAngle, float sinAngle) {
float newX = x*cosAngle - y*sinAngle;
float newY = x*sinAngle + y*cosAngle;
x = newX;
y = newY;

System.out.println(" X= "+x+" Y="+y);
}


/**
Adds the specified transform to this vector. This vector
is first rotated, then translated.
*/
public void add(Transform3D xform) {

// rotate
addRotation(xform);

// translate
add(xform.getLocation());
}


/**
Subtracts the specified transform to this vector. This
vector is translated, then rotated.
*/
public void subtract(Transform3D xform) {

// translate
subtract(xform.getLocation());

// rotate
subtractRotation(xform);
}


/**
Rotates this vector with the angle of the specified
transform.
*/
public void addRotation(Transform3D xform) {
rotateX(xform.getCosAngleX(), xform.getSinAngleX());
rotateZ(xform.getCosAngleZ(), xform.getSinAngleZ());
rotateY(xform.getCosAngleY(), xform.getSinAngleY());
}


/**
Rotates this vector with the opposite angle of the
specified transform.
*/
public void subtractRotation(Transform3D xform) {
// note that sin(-x) == -sin(x) and cos(-x) == cos(x)
rotateY(xform.getCosAngleY(), -xform.getSinAngleY());
rotateZ(xform.getCosAngleZ(), -xform.getSinAngleZ());
rotateX(xform.getCosAngleX(), -xform.getSinAngleX());
}



}

ViewWindow.java
java code
package graphices;

import java.awt.Rectangle;

public class ViewWindow {

/** Creates a new instance of ViewWindow */
private Rectangle bounds;
private float angle;
private float distanceToCamera;

/**
Creates a new ViewWindow with the specified bounds on the
screen and horizontal view angle.
*/
public ViewWindow(int left, int top, int width, int height,
float angle)
{
bounds = new Rectangle();
this.angle = angle;
setBounds(left, top, width, height);
}


/**
Sets the bounds for this ViewWindow on the screen.
*/
public void setBounds(int left, int top, int width,
int height)
{
bounds.x = left;
bounds.y = top;
bounds.width = width;
bounds.height = height;
distanceToCamera = (bounds.width/2) /
(float)Math.tan(angle/2);
}


/**
Sets the horizontal view angle for this ViewWindow.
*/
public void setAngle(float angle) {
this.angle = angle;
distanceToCamera = (bounds.width/2) /
(float)Math.tan(angle/2);
}


/**
Gets the horizontal view angle of this view window.
*/
public float getAngle() {
return angle;
}


/**
Gets the width of this view window.
*/
public int getWidth() {
return bounds.width;
}


/**
Gets the height of this view window.
*/
public int getHeight() {
return bounds.height;
}


/**
Gets the y offset of this view window on the screen.
*/
public int getTopOffset() {
return bounds.y;
}


/**
Gets the x offset of this view window on the screen.
*/
public int getLeftOffset() {
return bounds.x;
}


/**
Gets the distance from the camera to this view window.
*/
public float getDistance() {
return distanceToCamera;
}



/**
Converts an x coordinate on this view window to the
corresponding x coordinate on the screen.
*/
public float convertFromViewXToScreenX(float x) {
return x + bounds.x + bounds.width/2;
}


/**
Converts a y coordinate on this view window to the
corresponding y coordinate on the screen.
*/
public float convertFromViewYToScreenY(float y) {
return -y + bounds.y + bounds.height/2;
}


/**
Converts an x coordinate on the screen to the
corresponding x coordinate on this view window.
*/
public float convertFromScreenXToViewX(float x) {
return x - bounds.x - bounds.width/2;
}


/**
Converts a y coordinate on the screen to the
corresponding y coordinate on this view window.
*/
public float convertFromScreenYToViewY(float y) {
return -y + bounds.y + bounds.height/2;
}


/**
Projects the specified vector to the screen.
*/
public void project(Vector3D v) {
// project to view window


v.x = distanceToCamera * v.x / -v.z;
v.y = distanceToCamera * v.y / -v.z;

// convert to screen coordinates
v.x = convertFromViewXToScreenX(v.x);
v.y = convertFromViewYToScreenY(v.y);
System.out.println("V.X= "+v.x+" V.Y="+v.y+" V.Z "+v.z );


}


}





Attachments:
3DGraphices-Old.rar [41.47 KiB]
Downloaded 1410 times

_________________
M. S. Rakha, Ph.D.
Queen's University
Canada
Author:
Mastermind
User avatar Posts: 2715
Have thanks: 74 time
Post new topic Reply to topic  [ 1 post ] 

  Related Posts  to : 3D-Cube
 draw cube 3d java-Cube 3D     -  
 Cube number     -  
 Cube Series     -  
 cube 3d transformation     -  
 Draw 3d cube using openGL     -  



Topic Tags

Java Projects, Java Graphics






Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com