I am relatively new to java and have recently taken an interest in 3D programming.
The following is a class i wrote to create a shape in 3D space that can then be represented as a polygon in a java Applet.
The problem is that the driver i wrote to create and display one of these shapes didn't work. I was wondering if someone could glance over this and give me some feedback on whether it should work and if so how would i go about writing a driver to create and display one of these shapes.
The class 'cons', is another class that contains some random constants and methods for use throughout the project.
/* shape.java
* Daniel V Lee 17/06/2011
*
* a shape class to hold the 3D coordinates of the vertices of a polygon.
* to draw the polygon.
*/
import java.awt.*;
public class shape {
// integer array of the coordinates of the polygon.
// each three subsequent numbers are a set of 3D coordinates.
int[] vertices = {};
cons c = new cons();
// add a vertex
public void addVertex(int x,int y, int z)
{
vertices = addV(x, y, z);
}
private int[] addV(int x, int y, int z)
{
int[] newArray = new int[vertices.length+3];
for (int i = 0; i< vertices.length;i++)
{
newArray[i] = vertices[i];
}
newArray[vertices.length]=x;
newArray[vertices.length+1]=y;
newArray[vertices.length+2]=z;
return newArray;
}
// transform each 3D coordinate into a 2D one to be drawn
// all edges drawn between subsequent vertices with lines.
public void paint(Graphics g)
{
Polygon pg = new Polygon();
for (int i = 0;i<vertices.length;i+=3)
{
int x=(vertices[i]*c.zMin)/vertices[i+2];
int y=(vertices[i+1]*c.zMin)/vertices[i+2];
pg.addPoint(toJavaCoor(x,0),toJavaCoor(y,1));
}
g.fillPolygon(pg);
}
// convert the point to java x, y coordinates
// coor is the coordinate relative to centre of screen.
// axes: 0 for the x axes, 1 for the y axes
public int toJavaCoor(int coor, int axes)
{
// x direction
if (axes == 0)
{
return (coor+(c.GAME_WIDTH/2));
}
// y direction
return (coor+(c.GAME_HEIGHT/2));
}
}
