Results 1 to 8 of 8
Thread: A Shape Class
- 06-18-2011, 05:39 AM #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));
}
}
- 06-18-2011, 06:04 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,545
- Rep Power
- 11
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.the driver i wrote to create and display one of these shapes didn't work
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
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.Java Code:int x=(vertices[i]*c.zMin)/vertices[i+2];
- 06-18-2011, 04:51 PM #3
Member
- Join Date
- Jun 2011
- Posts
- 4
- Rep Power
- 0
That did turn out to be a problem actually.
- 06-19-2011, 03:23 AM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,545
- Rep Power
- 11
Good. (not good that it was a problem, but good it was spotted)
- 06-19-2011, 07:17 AM #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...
And this is the cons classJava 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); } }
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 * * 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; }
However when the shape is moved it never returns to its original position with the same orientation.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)); } }
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?
- 06-19-2011, 01:58 PM #6
How are you able to have an applet use the Robot class? Why an applet instead of an application?
- 06-20-2011, 11:02 AM #7
Member
- Join Date
- Jun 2011
- Posts
- 4
- Rep Power
- 0
Why not? isn't the robot class designed for applets?
- 06-20-2011, 01:13 PM #8
Similar Threads
-
create own shape
By kepep in forum Java 2DReplies: 2Last Post: 12-15-2010, 07:43 PM -
Help with Move Shape
By romina in forum AWT / SwingReplies: 2Last Post: 12-09-2010, 03:25 AM -
how to change the shape of the JFrame to a oval shape
By kiki2009 in forum Java 2DReplies: 1Last Post: 04-02-2010, 12:48 PM -
rezise shape
By frankenstein in forum Java 2DReplies: 5Last Post: 07-30-2009, 12:44 AM -
implementing shape
By sidkdbl07 in forum Java 2DReplies: 1Last Post: 01-12-2008, 06:42 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks