Java 3D



Share on Google+Share on Google+

Java 3D
2 Answer(s)      8 years ago
Posted in : Java3D
View Answers

April 1, 2009 at 3:17 PM

hi Sivaraman,

Create Java Applet.
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.lang.Math;

class Point3D {
public int x, y, z;
public Point3D(int X, int Y, int Z) {
x = X;
y = Y;
z = Z;
class Edge {
public int a, b;
public Edge(int A, int B) {
a = A;
b = B;
public class DrawCube3D extends Applet implements MouseListener, MouseMotionListener {
private static final long serialVersionUID = 1L;
int width, height;
int mx, my;
Image imag;
Graphics graph;
int azimuth = 35, elevation = 30;
Point3D[] vertices;
Edge[] edges;

public void init() {
setSize(640, 480);
width = getSize().width;
height = getSize().height;
vertices = new Point3D[8];
vertices[0] = new Point3D(-1, -1, -1);
vertices[1] = new Point3D(-1, -1, 1);
vertices[2] = new Point3D(-1, 1, -1);
vertices[3] = new Point3D(-1, 1, 1);
vertices[4] = new Point3D(1, -1, -1);
vertices[5] = new Point3D(1, -1, 1);
vertices[6] = new Point3D(1, 1, -1);
vertices[7] = new Point3D(1, 1, 1);
edges = new Edge[12];
edges[0] = new Edge(0, 1);
edges[1] = new Edge(0, 2);
edges[2] = new Edge(0, 4);
edges[3] = new Edge(1, 3);
edges[4] = new Edge(1, 5);
edges[5] = new Edge(2, 3);
edges[6] = new Edge(2, 6);
edges[7] = new Edge(3, 7);
edges[8] = new Edge(4, 5);
edges[9] = new Edge(4, 6);
edges[10] = new Edge(5, 7);
edges[11] = new Edge(6, 7);
imag = createImage(width, height);
graph = imag.getGraphics();
void drawWireFrame(Graphics g) {
double theta = Math.PI * azimuth / 180.0;
double phi = Math.PI * elevation / 180.0;
float cosT = (float) Math.cos(theta), sinT = (float) Math.sin(theta);
float cosP = (float) Math.cos(phi), sinP = (float) Math.sin(phi);
float cosTcosP = cosT * cosP, cosTsinP = cosT * sinP, sinTcosP = sinT * cosP, sinTsinP = sinT * sinP;
Point[] points;
points = new Point[vertices.length];
int scaleFactor = width / 4;
float near = 3;
int i;
float nearToObj = 1.5f;

for (i = 0; i < vertices.length; i++) {
int x0 = vertices[i].x;
int y0 = vertices[i].y;
int z0 = vertices[i].z;
float x1 = cosT * x0 + sinT * z0;
float y1 = -sinTsinP * x0 + cosP * y0 + cosTsinP * z0;
float z1 = cosTcosP * z0 - sinTcosP * x0 - sinP * y0;
x1 = x1 * near / (z1 + near + nearToObj);
y1 = y1 * near / (z1 + near + nearToObj);
points[i] = new Point((int) (width / 2 + scaleFactor * x1 + 0.5),(int) (height / 2 - scaleFactor * y1 + 0.5));
g.fillRect(0, 0, width, height);
for (i = 0; i < edges.length; ++i) {
g.drawLine(points[edges[i].a].x, points[edges[i].a].y,
points[edges[i].b].x, points[edges[i].b].y);

April 1, 2009 at 3:19 PM

public void mouseClicked(MouseEvent e) {
public void mouseEntered(MouseEvent e) {
public void mouseExited(MouseEvent e) {
public void mousePressed(MouseEvent e) {
mx = e.getX();
my = e.getY();
public void mouseReleased(MouseEvent e) {
public void mouseDragged(MouseEvent arg0) {
int new_mx = arg0.getX(), new_my = arg0.getY();
azimuth -= new_mx - mx;
elevation += new_my - my;
mx = new_mx;
my = new_my;
public void update(Graphics g) {
g.drawImage(imag, 0, 0, this);
System.out.println("azimuth: " + azimuth + ", elevation: " + elevation);
public void paint(Graphics g) {
public void mouseMoved(MouseEvent arg0) {

Then call it by using applet tag of html page like this:

<applet code=DrawCube3D.class width=500 height=400>



Related Tutorials/Questions & Answers:
java or 3d
java or 3d  can we use of 3d in core java? if yes pls tell me a demo example of 3d java. thanks...   Have a look at the following link:
3D JButton in java?
3D JButton in java?  how we can create 3D button in java. thanks
java 3d code - Java3D
java 3d code  can u please send source code, how to clear canvas3d in java 3d. In my project if i click on clear button in awt frame then canvas must be clear, please send code
Java 3D - Java3D
Java 3D  Hi can u tell me how can i write the code to develop the 3Dimension figure in java  hi Sivaraman, Create Java Applet. import java.applet.*; import java.awt.*; import java.awt.event.
3D PASSWORD  HI i would like to know abt where the 3d password in india used and also few info about the 3d password pls rply some1 as soon as possible
Difference between 3d and 4d
Difference between 3d and 4d  what is the difference between 3d and 4d
How to make a 3D text, make a 3D text, a 3D text
How to make a 3D text       The 3d text can be made by the photoshop, this example... and use these settings. This is your 3D text. Final Result
3d pie chart - Java3D
3d pie chart  Dear Sir, i want to create 3d pie chart using jsp, i have some code for 2d pie chart in jsp and 3d pie chart in applet. for 2d pie chart my jsp page is running properly but for 3d i used some code from applet
how to create 3d chart in dojox charting - XML
how to create 3d chart in dojox charting  Please provide some example for 3d charting in dojox chart
How to make a reflected a 3D text, make a reflected a 3D text, reflected a 3D text
How to make a reflected a 3D text       In this example you will learn a simple way to make a 3D reflection text effect, It has very simple steps to make it easy for the beginners so
What is 3D Printing?
3D printing is one of those technologies that made manual job with the objects... and in the meantime we lose the erstwhile sense of solid sensory reality. 3D printing... is manufactured meticulously following the design, look and feel of a digital 3D
3D Pie Chart
3D Pie Chart       In this section we are providing you an example to create a 3D Pie Chart. Description of Program :    For creating a 3D Pie Chart we use
3D Bar Chart
3D Bar Chart       In this section we are providing you an example to create a 3D Bar Chart. Description of Program  For creating a 3D Bar chart we use the object
JFree 3D Bar Chart not displaying in JSP - Java3D
JFree 3D Bar Chart not displaying in JSP  Hi I am trying to run JFree 3D bar chart written in your site under link: This is not working. PNG file
JavaFX 3D Effect Example
JavaFX 3D Effect Example       We can create an Object in JavaFX and can give him 3D Effect (like Creating Sphere with Circle and 3D text by blurring Text in background
All New HP 3D Printer: Specs, Features and Expectations Explained
All New HP 3D Printer: Specs, Features and Expectations Explained After more... 3D printer market, the ace printer maker Hewlett-Packard just a week before unveiled two brand new models of 3D printers. The new HP 3D printer is named as HP
Stacked 3d Bar Chart Example using JFreeChart
Stacked 3d Bar Chart Example using JFreeChart... a Stacked 3d bar chart using JFreeChart. Bar chart will represent the score...;    new StackedBarChart3D("Stacked 3D Bar Chart"
Java - Java Beginners
Java Questions - Java Beginners
Java Questions  Can we make 3d games in java
Create a 3D bar chart in JSP page using JFreeChart
Create a 3D bar chart in JSP page using JFreeChart       This Example shows you how to create a 3D bar chart in jsp page using JFreeChart. Code given below creates a bar
JavaFX 3D Effect Example
JavaFX 3D Effect Example       Working with java Collections class In this section... java collections framework is a set of classes and interfaces that are used
java graphics - Design concepts & design patterns
java graphics  i dont wanna use Java3D but put solid 3D objects on screen, use z-buffering, ray-casting etc. to make realistic images...this will help build 3D fractals etc...Where must i begin reading
the world, Banking Application, view 3D image and Shopping Cart. Java find  what is java   java is a programing langauge   Java is an objected oriented programming language. It is designed to work
JavaFX       JavaFX 3D Effect Example We can create an Object in JavaFX and can give him 3D Effect (like Creating Sphere with Circle and 3D text
JavaFX Scripting Language
UIs leveraging Java Swing, Java 2D and Java 3D for developers and content... for deployment on Java environments. Since JavaFX Script is statically typed, it has... to create and maintain very large programs using Java technology. It gives Java
Open Source Game Engine
and others. JOAL gives Java technology developers access to hardware-accelerated 3D... is a real-time 3D rendering environment for all of your real-time 3D needs... world-class, real-time 3D applications today. If you are new to developing
Java Runtime
Java Runtime       In this section, you will learn about Java Runtime Environment (JRE), it also known as Java Runtime. This is a part of Java Development Kit (JDK). It is a set
trafic site statistics - Java Server Faces Questions
trafic site statistics  Hello, Does the framework JSF work for creating web pages containing 3D entities (like 3DCharts)and displaying web trafic... correspond the balises d:chartItem to a java class (Bean)? Thanks for any
Java API
and messaging application. Java 3D A scene graph based 3D API. Java Open GLA wrapper...Java API what is Java API? API stands for Application Programming Interface... of code. Java Application Programming Interface is a library of classes
Ajax3D       Fostering the development of web-based 3D applications and online virtual worlds using Ajax and X3D-based open technologies. Read full Description
java  diff bt core java and java


Advertisement null