# Thread: A Shape Class

1. Member
Join Date
Jun 2011
Posts
4
Rep Power
0

## A Shape Class

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

2. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,565
Rep Power
12
the driver i wrote to create and display one of these shapes didn't work
We can't see this class, nor do we know what happened when its main() method was invoked or what you expected to happen. "didn't work" conveys no information.

There are another couple of ther things you can do to faciltate communication. Use standard Java naming conventions. Shape not shape (actually MyShape or something might be better to avoid collision with the builtin Shape class.) Provide an implemention for the Cons class so that the program can actually be run.

------------

One little thing I noticed is that you have

Java Code:
`int x=(vertices[i]*c.zMin)/vertices[i+2];`
This will peform integer division ie will throw the remainder away. This may or may not be a problem depending on what c.zMin is.

3. Member
Join Date
Jun 2011
Posts
4
Rep Power
0
That did turn out to be a problem actually.

4. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,565
Rep Power
12
Good. (not good that it was a problem, but good it was spotted)

5. Member
Join Date
Jun 2011
Posts
4
Rep Power
0
I got it working by using a template of another program. This is what it looks like...
Java Code:
```import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class shapeDriver extends Applet implements MouseMotionListener, ActionListener {

/**
*
*/
private static final long serialVersionUID = 1L;
/*
*
*/
private Timer gameTimer;

private shape polygon;
private shape polygon2;
private cons c;

// mouse movement
double angleVert;
double angleHorz;
int mouseCentreX =0;
int mouseCentreY =0;

// double buffer!
private Image dbImage;
private Graphics dbg;

public void init()
{
addMouseMotionListener(this);

this.requestFocus();

polygon = new shape(Color.black);
polygon.addVertex(-100, -100, 300);
polygon.addVertex(100, -100, 300);
polygon.addVertex(100, 100, 300);
polygon.addVertex(-100, 100, 300);
polygon2 = new shape(Color.blue);
polygon2.addVertex(-100, -100, 300);
polygon2.addVertex(100, -100, 300);
polygon2.addVertex(100,-100, 500);
polygon2.addVertex(-100, -100, 500);
for (int i = 0;i<4;i++)
{
System.out.println(polygon.vertices[i*3]+" "+polygon.vertices[i*3+1]+" "+polygon.vertices[i*3+2]);
}

c = new cons();

setSize(c.GAME_WIDTH,c.GAME_HEIGHT);
setBackground(Color.white);

gameTimer = new Timer(10, this);
}

// is called by update() and draws stuff on screen
public void paint(Graphics g)
{

// update coordinates of the shape
System.out.println(angleHorz+" "+angleVert);
polygon.rotateX(angleHorz);
polygon.rotateY(angleVert);
polygon2.rotateX(angleHorz);
polygon2.rotateY(angleVert);
// paint new shape
polygon.paint(g);
polygon2.paint(g);
// update mouse position
try {
// executes if no exception is thrown
// moves mouse to the centre of the screen
Robot robot = new Robot();
robot.mouseMove(c.GAME_WIDTH/2, c.GAME_HEIGHT/2);
} catch (AWTException e) {
e.printStackTrace();
}
}

public void keyPressed(KeyEvent e)
{

}

public void mouseMoved(MouseEvent e)
{
// get the difference in angle of mouse location and centre of screen
angleVert = (e.getY()-mouseCentreY);
angleHorz = (e.getX()-mouseCentreX);
System.out.println("MouseX:\t"+e.getX()+",\tMouseY:\t"+e.getY());
System.out.println(c.GAME_HEIGHT/2+" "+c.GAME_WIDTH/2);
System.out.println();

// call update() ?
repaint();
try {
// executes if no exception is thrown
// moves mouse to the centre of the screen
Robot robot = new Robot();
robot.mouseMove(c.GAME_WIDTH/2, c.GAME_HEIGHT/2);
mouseCentreX = e.getX();
mouseCentreY = e.getY();
System.out.println("Mouse Centre: "+mouseCentreX+" "+mouseCentreY);
} catch (AWTException d) {
d.printStackTrace();
}
}

// this is for when the timer starts.
public void actionPerformed(ActionEvent e)
{

}

public void mouseDragged(MouseEvent e) { }

public void keyTyped(KeyEvent e) { }

public void keyReleased(KeyEvent e) { }

// removes all graphics... i think...
public void update (Graphics g)
{
// initialize buffer
if (dbImage == null)
{
dbImage = createImage (this.getSize().width, this.getSize().height);
dbg = dbImage.getGraphics ();
}

// clear screen in background
dbg.setColor (getBackground ());
dbg.fillRect (0, 0, this.getSize().width, this.getSize().height);

// draw elements in background
dbg.setColor (getForeground());
paint (dbg);

// draw image on the screen
g.drawImage (dbImage, 0, 0, this);
}

}```
And this is the cons class
Java Code:
```/*
*  Daniel V Lee 17/06/2011
*
*  for holding miscellaneous attributes, methods and constants.
*/
public class cons {
public final int zMin = 100;
public final int zMax = 10000;
public final int GAME_WIDTH = 500;
public final int GAME_HEIGHT = 500;

}```
I made some changes to the shape class as well so that it now rotates the shape when the mouse is moved.
Java Code:
```/*
*  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.
public double[] vertices = {};
private cons c = new cons();

private Color colour;

//constructor
public shape(Color newColor)
{
colour = newColor;
}

// add a vertex
public void addVertex(int x,int y, int z)
{
vertices = addV(x, y, z);
}
private double[] addV(int x, int y, int z)
{
double[] newArray = new double[vertices.length+3];
// copy array
for (int i = 0; i< vertices.length;i++)
{
newArray[i] = vertices[i];
}
// add new vertex
newArray[vertices.length]=x;
newArray[vertices.length+1]=y;
newArray[vertices.length+2]=z;
return newArray;
}

// rotate the shape in the x-z plane
public void rotateX(double a)
{
for (int vertex = 0; vertex<vertices.length;vertex+=3)
{
double[] point = {vertices[vertex],vertices[vertex+1],vertices[vertex+2]};
// new angle in radians
double angle = a/(Math.sqrt(Math.pow(point[0], 2)+Math.pow(point[1], 2)+Math.pow(point[2], 2)));
vertices[vertex] = (point[0]*Math.cos(angle)-point[2]*Math.sin(angle));
vertices[vertex+2]= (point[0]*Math.sin(angle)+point[2]*Math.cos(angle));
}
}

// rotate the shape in the y-z plane
public void rotateY(double a)
{
for (int vertex = 0;vertex<vertices.length;vertex+=3)
{
double[] point = {vertices[vertex],vertices[vertex+1],vertices[vertex+2]};
double angle = a/(Math.sqrt(Math.pow(point[0], 2)+Math.pow(point[1], 2)+Math.pow(point[2], 2)));
vertices[vertex+1] = (point[1]*Math.cos(angle)-point[2]*Math.sin(angle));
vertices[vertex+2] = (point[1]*Math.sin(angle)+point[2]*Math.cos(angle));
}
}
// 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)
{
//System.out.println(vertices[i]+" "+vertices[i+1]+" "+vertices[i+2]);
int x=toJavaCoor((vertices[i]*c.zMin)/vertices[i+2],0);
int y=toJavaCoor((vertices[i+1]*c.zMin)/vertices[i+2],1);
pg.addPoint(x,y);

}
g.setColor(colour);
g.fillPolygon(pg);
}
// convert the point to java coordinates
// coor is the coordinate relative to centre of screen.
// axes: 0 for the x axes, 1 for the y axes
public int toJavaCoor(double coor, int axes)
{
// x direction
if (axes == 0)
{
return (int)(coor+(c.GAME_WIDTH/2));
}
// y direction
return (int)(coor+(c.GAME_HEIGHT/2));
}
}```
However when the shape is moved it never returns to its original position with the same orientation.
It is supposed work like you would expect a first person game to work when you move the mouse.
Would you know what is causing this?

Also i don't fully understand what the repaint() method does. does it simply call the update() method?

6. How are you able to have an applet use the Robot class? Why an applet instead of an application?

7. Member
Join Date
Jun 2011
Posts
4
Rep Power
0
Why not? isn't the robot class designed for applets?

8. I don't know. Unsigned applets would not have permission to use the Robot class.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•