import java.awt.*; public class Modeler extends Canvas { Triplet points = null; int origin_x = 0; int origin_y = 0; int xrot=0, yrot=0, zrot=0; public Modeler() { } public void setOrigin(int org_x, int org_y) { origin_x = org_x; origin_y = -org_y; } public void addPoint(int mx, int my, int mz) { addPoint(points, mx, -my, mz); } void addPoint(Triplet t, int mx, int my, int mz) { if (t!=null) { t.second = new Triplet(t.first, t.second, t.third); t.first = new Point(mx, my); t.third = new Integer(mz); } else if(t==points) { points = new Triplet(new Point(mx, my), null, new Integer(mz)); } } public void paint(Graphics g) { super.paint(g); int w = size().width; int h = size().height; int tmp_x=0; int tmp_y=0; int tmp_z=0; g.setColor(Color.black); g.drawRect(w/2, h/2, 6, 6); g.drawString("Heading: "+yrot+" Pitch: "+xrot+" Roll: "+zrot, 5, 30); double new_x = 0; double new_y = 0; double new_z = 0; Point pt = null; Triplet to_draw = points; double cosh = Math.cos((double)(yrot)*Math.PI/180.0); double sinh = Math.sin((double)(yrot)*Math.PI/180.0); double cosp = Math.cos((double)(xrot)*Math.PI/180.0); double sinp = Math.sin((double)(xrot)*Math.PI/180.0); double cosb = Math.cos((double)(zrot)*Math.PI/180.0); double sinb = Math.sin((double)(zrot)*Math.PI/180.0); double a1 = cosh*cosb+sinh*sinp*sinb; double a2 = sinb*cosp; double a3 = -sinh*cosb-cosh*sinp*sinb; double b1 = -cosh*sinb+sinh*sinp*cosb; double b2 = cosb*cosp; double b3 = sinb*sinh+cosh*sinp*cosb; double c1 = sinh*cosp; double c2 = -sinp; double c3 = cosh*cosp; while(to_draw!=null) { pt = (Point) (to_draw.first); tmp_x=pt.x-origin_x; tmp_y=pt.y-origin_y; tmp_z= ((Integer) (to_draw.third)).intValue(); to_draw = (Triplet) (to_draw.second); new_z = 1000-(tmp_x*c1 + tmp_y*c2 + tmp_z*c3); new_x = tmp_x*a1 + tmp_y*a2 + tmp_z*a3; new_y = tmp_x*b1 + tmp_y*b2 + tmp_z*b3; //System.out.println(length+" "+new_x+" "+new_y+" "+new_z+" "+new_zrot+" : "+tmp_x*tmp_x+" "+tmp_x+" "+tmp_y+" "+tmp_z); if (new_z!=0) { g.drawRect(w/2 + (int)(new_x*300.00/new_z), h/2 - (int)(new_y*300.00/new_z), 6, 6); } } } public boolean mouseEnter(Event e, int mx, int my) { requestFocus(); return true; } public boolean keyDown(Event e, int key) { boolean touched = false; int stepper = 10; switch (key) { case 'w': xrot=(xrot+stepper)%360; touched = true; break; case 'q': yrot=(yrot+stepper)%360; touched = true; break; case 'e': zrot=(zrot+stepper)%360; touched = true; break; case 'x': xrot=(xrot-stepper+360)%360; touched = true; break; case 'z': yrot=(yrot-stepper+360)%360; touched = true; break; case 'c': zrot=(zrot-stepper+360)%360; touched = true; break; } if (touched==true) { repaint(); } return true; } public Dimension minimumSize() { return (new Dimension(150, 100)); } public Dimension preferredSize() { return (new Dimension(500, 250)); } }