Ads

Ads

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();

drawWireFrame(graph);

addMouseListener(this);

addMouseMotionListener(this);

}

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.setColor(Color.black);

g.fillRect(0, 0, width, height);

g.setColor(Color.green);

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);

}

}

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();

drawWireFrame(graph);

addMouseListener(this);

addMouseMotionListener(this);

}

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.setColor(Color.black);

g.fillRect(0, 0, width, height);

g.setColor(Color.green);

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

cont....

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();

e.consume();

}

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;

drawWireFrame(graph);

mx = new_mx;

my = new_my;

repaint();

arg0.consume();

}

public void update(Graphics g) {

g.drawImage(imag, 0, 0, this);

System.out.println("azimuth: " + azimuth + ", elevation: " + elevation);

}

public void paint(Graphics g) {

update(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>

</applet>

Thanks

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();

e.consume();

}

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;

drawWireFrame(graph);

mx = new_mx;

my = new_my;

repaint();

arg0.consume();

}

public void update(Graphics g) {

g.drawImage(imag, 0, 0, this);

System.out.println("azimuth: " + azimuth + ", elevation: " + elevation);

}

public void paint(Graphics g) {

update(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>

</applet>

Thanks

Ads

Related Tutorials/Questions & Answers: