// draw3D3.java java draw3D3 root-file-name // .draw3d may be read and/or written // .stl may be written // opens four view windows X-Y X-Z Z-Y and ortho // opens 3D window and Menu window // select color, shape, placement // no painters algorithm, yet import java.*; import java.awt.*; // basic drawing import java.awt.event.*; import java.util.*; // get LinkedList import java.io.*; // for file read and write import java.text.*; // for token public class draw3D3 { final int width = 400; // window, make square for good negate final int height = 400; final int Mwidth = 200; // menu final int mh = 20; final int mo = 110; final int m2 = 70; final int debug = 1; String rootfilename = new String(" "); boolean have_filename = false; int xm, ym, bm; // mouse position (inverted y) int x0, y0, z0, x1, y1, z1; // "rubber" coordinates int x2, y2, z2, x3, y3, z3, tx, ty, tz; // "extra points" boolean posX = true; boolean posY = true; boolean posZ = true; boolean solid = true; boolean selected = false; boolean gridOn = true; int grid = 50; // grid spacing int bo = 510; // for color Menu int ba = 45; // for color Menu Color drawColor = new Color(255, 0, 0); // Color of object being drawn int objSelect = 0; // can be sequences // -1 object container, possible internal drawList // 0=cube face 1, 1x=cube third dimension // 2=sphere center and edge, 3x=sphere center third dimension // 4=surface rectangle, 5x=third dimension plane // 6=surface triangle P1,P2, 7x= third dimension P1,P2 // 8x=P3, 9x=P3 third dimension LinkedList drawList = new LinkedList(); // list of objects, initially empty objCube objC; // temp cube object objSphere objS; // temp sphere object objRect objR; // temp rectangle surface objTri objT; // temp triangle surface int instanceCount = 0; // give each object a unique instance int xp; // last mouse location during drag int yp; boolean trackingXY = false; // tracking XY mouse motion boolean trackingXZ = false; // tracking XZ mouse motion boolean trackingZY = false; // tracking ZY mouse motion boolean tempXY = false; // hold rubber for third dimension boolean tempXZ = false; // hold rubber for third dimension boolean tempZY = false; // hold rubber for third dimension Frame fXY, fXZ, fZY, f3D, fMenu; Graphics gXY, gXZ, gZY, g3D, gMenu; draw3D3(String[] args) { System.out.println("draw3D3 starting"); if(args.length>=1) { rootfilename = new String(args[0]); have_filename = true; } objC = new objCube(); // temp cube object objS = new objSphere(); // temp sphere object objR = new objRect(); // temp rectangle surface objT = new objTri(); // temp triangle surface // each thread opens its own window new MyThread("X-Y (front view)", Mwidth, height).start(); new MyThread("X-Z (top view)", Mwidth, 0).start(); new MyThread("Z-Y (side view)", width+Mwidth, height).start(); new MyThread("3D", width+Mwidth, 0).start(); new MyThread("Menu", 0, 0).start(); System.out.println("draw3D3 constructor ending"); } // end draw3D3 constructor class MyThread extends Thread { String id; Graphics g; Frame f; MyThread(String who, int xLoc, int yLoc) { id = who; // initiate a window for each thread f = new Frame(); f.setTitle("draw3D3 "+who); f.setLocation(xLoc, yLoc); if(id=="Menu") f.setSize(Mwidth, 2*height); else f.setSize(width, height); f.setBackground(Color.white); f.setForeground(Color.black); f.setVisible(true); g = f.getGraphics(); // every window has its Frame, Graphics f.addWindowListener(new WindowAdapter() // and Listeners { public void windowClosing(WindowEvent e) { System.exit(0); } }); f.addMouseListener (new mousePressHandler()); f.addMouseListener (new mouseReleaseHandler()); f.addMouseMotionListener (new mouseMotionHandler()); } // end MyThread constructor public void run() { try { if(debug>=1)System.out.println(id+" running"); if (id=="X-Y (front view)") {fXY=f; gXY=g;} else if(id=="X-Z (top view)") {fXZ=f; gXZ=g;} else if(id=="Z-Y (side view)") {fZY=f; gZY=g;} else if(id=="3D") {f3D=f; g3D=g;} else if(id=="Menu"){fMenu=f; gMenu=g; paint(g);} sleep(1); } catch(InterruptedException e) { } if(debug>=1)System.out.println("run exiting"); } // end MyThread run } // end class MyThread public void paint(Graphics g) { // call methods to do actual drawing // System.out.println("paint running"); drawXY(fXY, gXY); drawXZ(fXZ, gXZ); drawZY(fZY, gZY); draw3d(f3D, g3D); drawMenu(fMenu, gMenu); } // end paint public void drawXY(Frame f, Graphics g) { objList objL; // System.out.println("in drawXY"); g.clearRect(0, 0, width, height); g.setColor(Color.black); if(gridOn) for(int i=grid; i=3)System.out.println("x1f="+x1f+", y1f="+y1f); if(debug>=3)System.out.println("x2f="+x2f+", y2f="+y2f); if(debug>=3)System.out.println("x1b="+x1b+", y1b="+y1b); if(debug>=3)System.out.println("x2b="+x2b+", y2b="+y2b); xp[0]=x1f; xp[1]=x1f; xp[2]=x2f; xp[3]=x2f; yp[0]=y1f; yp[1]=y2f; yp[2]=y2f; yp[3]=y1f; g.setColor(objColor); if(solid) { g.fillPolygon(xp, yp, np); // front g.setColor(Color.black); // else color of wireframe is object color } g.drawPolygon(xp, yp, np); // front xp[0]=x2f; xp[1]=x2f; xp[2]=x2b; xp[3]=x2b; yp[0]=y1f; yp[1]=y2f; yp[2]=y2b; yp[3]=y1b; g.setColor(objColor); if(solid) { g.fillPolygon(xp, yp, np); // side g.setColor(Color.black); } g.drawPolygon(xp, yp, np); // side xp[0]=x1f; xp[1]=x1b; xp[2]=x2b; xp[3]=x2f; yp[0]=y1f; yp[1]=y1b; yp[2]=y1b; yp[3]=y1f; g.setColor(objColor); if(solid) { g.fillPolygon(xp, yp, np); // top g.setColor(Color.black); } g.drawPolygon(xp, yp, np); // top if(!solid) // draw hidden wireframe { g.setColor(objColor); xp[0]=x1b; xp[1]=x1b; xp[2]=x2b; xp[3]=x2b; yp[0]=y1b; yp[1]=y2b; yp[2]=y2b; yp[3]=y1b; g.drawPolygon(xp, yp, np); // back //xp[0]=x2f; xp[1]=x2f; xp[2]=x2b; xp[3]=x2b; //yp[0]=y1f; yp[1]=y2f; yp[2]=y2b; yp[3]=y1b; //g.drawPolygon(xp, yp, np); // other side xp[0]=x1f; xp[1]=x1b; xp[2]=x2b; xp[3]=x2f; yp[0]=y2f; yp[1]=y2b; yp[2]=y2b; yp[3]=y2f; g.drawPolygon(xp, yp, np); // bottom } } public String getName() { return name+instance+"\n"+ "x1="+x1+", y1="+y1+", z1="+z1+"\n"+ "x2="+x2+", y2="+y2+", z2="+z2+"\n"; } } // end class objCube class objSphere extends objList { int x1, y1, z1, r; // center and radius char need = ' '; double c = width/2.0; // center and scale factor double sf = 0.5; public objSphere() {} public objSphere(objSphere obj) { x1=obj.x1; y1=obj.y1; z1=obj.z1; r=obj.r; bx1=obj.bx1; bx2=obj.bx2; by1=obj.by1; by2=obj.by2; bz1=obj.bz1; bz2=obj.bz2; objColor=obj.objColor; name=obj.name; instance=obj.instance; drawList=obj.drawList;} public void misc(Color c, char n) { objColor = drawColor; need = n; name = "Sphere"; instance = instanceCount++;} public void centxy(int xa, int ya, int xb, int yb) { x1=xa; y1=ya; double dx=(double)(xa-xb); double dy=(double)(ya-yb); r = (int)(Math.sqrt(dx*dx+dy*dy)/2.0); bx1=x1-r; bx2=x1+r; by1=y1-r; by2=y2+r;} public void centz (int za) { z1=za; bx1=z1-r; bz2=z1+r;} public void centxz(int xa, int za, int xb, int zb) { x1=xa; z1=za; double dx=(double)(xa-xb); double dz=(double)(za-zb); r = (int)(Math.sqrt(dx*dx+dz*dz)/2.0); bx1=x1-r; bx2=x1+r; bz1=z1-r; bz2=z1+r;} public void centy (int ya) { y1=ya; by1=y1+r; by2=y1+r;} public void centzy(int za, int ya, int zb, int yb) { z1=za; y1=ya; double dz=(double)(za-zb); double dy=(double)(ya-yb); r = (int)(Math.sqrt(dz*dz+dy*dy)/2.0); by1=y1-r; by2=y1+r; bz1=z1-r; bz2=z1+r;} public void centx (int xa) { x1=xa; bx1=x1-r; bx2=x1+r;} public void drawXY(Graphics g) {g.setColor(objColor); g.fillOval(x1-r, y1-r, 2*r, 2*r);} public void drawZY(Graphics g) {g.setColor(objColor); g.fillOval(z1-r, y1-r, 2*r, 2*r);} public void drawXZ(Graphics g) {g.setColor(objColor); g.fillOval(x1-r, height-(z1+r), 2*r, 2*r);} int xortho(int x, int z) {return (int)(((x-c)+0.7071*(z-c))*sf+c);} int yortho(int y, int z) {return (int)(((y-c)-0.7071*(z-c))*sf+c);} public void draw3d(Graphics g) { // convert coordinates int xa = xortho(x1, z1); int ya = yortho(y1, z1); g.setColor(objColor); if(solid) { g.fillOval(xa-(int)(sf*r), ya-(int)(sf*r), (int)(sf*2.0*r), (int)(sf*2.0*r)); g.setColor(Color.black); } double wfx[][] = new double[7][7]; double wfy[][] = new double[7][7]; double angx, angy; for(int i=0; i<7; i++) { angy = Math.PI*(double)(i)/(double)6; for(int j=0; j<7; j++) { angx = Math.PI*(double)(j)/(double)6; wfx[i][j] = Math.cos(angy)*Math.sin(angx); wfy[i][j] = Math.cos(angx); if(debug>=3)System.out.println("wf["+i+"]["+j+"]=("+ wfx[i][j]+","+wfy[i][j]+")"); } } for(int i=0; i<7; i++) { for(int j=0; j<7; j++) { if(i<6) g.drawLine(xa+(int)(wfx[i][j]*sf*r), ya+(int)(wfy[i][j]*sf*r), xa+(int)(wfx[i+1][j]*sf*r), ya+(int)(wfy[i+1][j]*sf*r)); if(j<6) g.drawLine(xa+(int)(wfx[i][j]*sf*r), ya+(int)(wfy[i][j]*sf*r), xa+(int)(wfx[i][j+1]*sf*r), ya+(int)(wfy[i][j+1]*sf*r)); } } } public String getName() { return name+instance+"\n"+ "x1="+x1+", y1="+y1+", z1="+z1+"\n"+ "r="+r+"\n"; } } // end class objSphere class objRect extends objList { int x1, y1, z1, x2, y2, z2; // diagonal corners char need = ' '; double c = width/2.0; // center and scale factor double sf = 0.5; public objRect() {} public objRect(objRect obj) { x1=obj.x1; y1=obj.y1; z1=obj.z1; x2=obj.x2; y2=obj.y2; z2=obj.z2; bx1=obj.bx1; bx2=obj.bx2; by1=obj.by1; by2=obj.by2; bz1=obj.bz1; bz2=obj.bz2; objColor=obj.objColor; name=obj.name; instance=obj.instance; drawList=obj.drawList;} public void misc(Color c, char n) { objColor = drawColor; need = n; name = "Rect"; instance = instanceCount++;} public void facexy(int xa, int ya, int xb, int yb) { x1=xa=3)System.out.println("x1f="+x1f+", y1f="+y1f); if(debug>=3)System.out.println("x2f="+x2f+", y2f="+y2f); if(debug>=3)System.out.println("x1b="+x1b+", y1b="+y1b); if(debug>=3)System.out.println("x2b="+x2b+", y2b="+y2b); xp[0]=x1f; xp[1]=x1f; xp[2]=x2f; xp[3]=x2f; yp[0]=y1f; yp[1]=y2f; yp[2]=y2f; yp[3]=y1f; g.setColor(objColor); if(solid) { g.fillPolygon(xp, yp, np); // front g.setColor(Color.black); } g.drawPolygon(xp, yp, np); // front xp[0]=x2f; xp[1]=x2f; xp[2]=x2b; xp[3]=x2b; yp[0]=y1f; yp[1]=y2f; yp[2]=y2b; yp[3]=y1b; g.setColor(objColor); if(solid) { g.fillPolygon(xp, yp, np); // side g.setColor(Color.black); } g.drawPolygon(xp, yp, np); // side xp[0]=x1f; xp[1]=x1b; xp[2]=x2b; xp[3]=x2f; yp[0]=y1f; yp[1]=y1b; yp[2]=y1b; yp[3]=y1f; g.setColor(objColor); if(solid) { g.fillPolygon(xp, yp, np); // top g.setColor(Color.black); } g.drawPolygon(xp, yp, np); // top } public String getName() { return name+instance+"\n"+ "x1="+x1+", y1="+y1+", z1="+z1+"\n"+ "x2="+x2+", y2="+y2+", z2="+z2+"\n"; } } // end class objRect class objTri extends objList { int x1, y1, z1, x2, y2, z2, x3, y3, z3; // 3 points char need = ' '; double c = width/2.0; // center and scale factor double sf = 0.5; public objTri() {} public objTri(objTri obj) { x1=obj.x1; y1=obj.y1; z1=obj.z1; x2=obj.x2; y2=obj.y2; z2=obj.z2; x3=obj.x3; y3=obj.y3; z3=obj.z3; bx1=obj.bx1; bx2=obj.bx2; by1=obj.by1; by2=obj.by2; bz1=obj.bz1; bz2=obj.bz2; objColor=obj.objColor; name=obj.name; instance=obj.instance; drawList=obj.drawList;} public void misc(Color c, char n) { objColor = drawColor; need = n; name = "Tri"; instance = instanceCount++;} public void misc3(Color c, char n) { objColor = drawColor; need = n; name = "Tri";} public void facexy(int xa, int ya, int xb, int yb) { bx1=xa=3)System.out.println("x1f="+x1f+", y1f="+y1f); if(debug>=3)System.out.println("x2f="+x2f+", y2f="+y2f); xp[0]=x1f; xp[1]=x2f; xp[2]=x3f; yp[0]=y1f; yp[1]=y2f; yp[2]=y3f; g.setColor(objColor); if(solid) { g.fillPolygon(xp, yp, np); // face g.setColor(Color.black); } g.drawPolygon(xp, yp, np); // face } public String getName() { return name+instance+"\n"+ "x1="+x1+", y1="+y1+", z1="+z1+"\n"+ "x2="+x2+", y2="+y2+", z2="+z2+"\n"+ "x3="+x3+", y3="+y3+", z3="+z3+"\n"; } } // end class objTri boolean in_rect(int x, int y, int w, int h) { return xm>x && xmy && ymval[j]) { t=val[i]; val[i]=val[j]; val[j]=t; t=ix[i]; ix[i]= ix[j]; ix[j]=t; } } } } static public void main(String[] args) { for(int i=0; i