<repo_name>ntaniraula/3DPuppet<file_sep>/3DPuppet/src/Wireframe.java
// Wireframe.java: Perspective drawing using an input file that lists
//    vertices and faces.
// Uses: Point2D (Section 1.5),
//       Triangle, Tools2D (Section 2.13),
//       Point3D (Section 3.9),
//       Input, Obj3D, Tria, Polygon3D, Canvas3D, Fr3D (Section 5.5),
//       CvWireframe (Section 5.6).
import java.awt.*;
import java.awt.geom.*;
import java.util.*;

import library.Point2D;
import library.Point3D;

public class Wireframe extends Frame
{  public static void main(String[] args)
   {  new Fr3D(args.length  > 0 ? args[0] : null, new CvWireframe(),
        "Wire-frame model");
   }
}

//CvWireframe.java: Canvas class for class Wireframe.


class CvWireframe extends Canvas3D
{  private int maxX, maxY, centerX, centerY;
   private Obj3D obj;
   private Point2D imgCenter;

   Obj3D getObj(){return obj;}
   void setObj(Obj3D obj){this.obj = obj;}
   int iX(float x){return Math.round(centerX + x - imgCenter.x);}
   int iY(float y){return Math.round(centerY - y + imgCenter.y);}

   public void paint(Graphics g)
   {  if (obj == null) return;
      Vector polyList = obj.getPolyList();
      if (polyList == null) return;
      int nFaces = polyList.size();
      if (nFaces == 0) return;
      Dimension dim = getSize();
      maxX = dim.width - 1; maxY = dim.height - 1;
      centerX = maxX/2; centerY = maxY/2;
      // ze-axis towards eye, so ze-coordinates of
      // object points are all negative.
      // obj is a java object that contains all data:
      // - Vector w       (world coordinates)
      // - Array e        (eye coordinates)
      // - Array vScr     (screen coordinates)
      // - Vector polyList (Polygon3D objects)

      // Every Polygon3D value contains:
      // - Array 'nrs' for vertex numbers
      // - Values a, b, c, h for the plane ax+by+cz=h.
      // (- Array t (with nrs.length-2 elements of type Tria))

      obj.eyeAndScreen(dim);
            // Computation of eye and screen coordinates.

      imgCenter = obj.getImgCenter();
      obj.planeCoeff();    // Compute a, b, c and h.
      Point3D[] e = obj.getE();
      Point2D[] vScr = obj.getVScr();

      g.setColor(Color.black);

      for (int j=0; j<nFaces; j++)
      {  Polygon3D pol = (Polygon3D)(polyList.elementAt(j));
         int nrs[] = pol.getNrs();
         if (nrs.length < 3)
            continue;
         for (int iA=0; iA<nrs.length; iA++)
         {  int iB = (iA + 1) % nrs.length;
            int na = Math.abs(nrs[iA]), nb = Math.abs(nrs[iB]);
            // abs in view of minus signs discussed in Section 6.4.
            Point2D a = vScr[na], b = vScr[nb];
            g.drawLine(iX(a.x), iY(a.y), iX(b.x), iY(b.y));
         }
      }
   }
}

<file_sep>/3DPuppet/src/Puppet3DTest.java
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;

import library.*;

import library.DashLine;

public class Puppet3DTest extends Frame
{
	public static void main(String[] args){	new Puppet3DTest();}
	Puppet3DTest()
	{
		 super("Puppet 3D");
	      addWindowListener(new WindowAdapter()
	         {public void windowClosing(WindowEvent e){System.exit(0);}});
	      
	      Dimension dim = getToolkit().getScreenSize();
	      setSize(dim.width, dim.height);
	      setLocation(0, 0);
	      add("Center", new CvPuppet());
	      show();
	}

}
class CvPuppet extends Canvas implements MouseListener,KeyListener,MouseMotionListener,MouseWheelListener
{
	CoordinateSys cordSys;
	Puppet3D puppet;
	Walker walker;
	Graphics g;
	BufferedImage image;
	Graphics imgGraphics;
	int countClick=0;
	Dimension dimScene;
	boolean puppetSet=false;
	boolean panScreen=false;
	CvPuppet()
	{
		cordSys=new CoordinateSys();
		puppet=new Puppet3D(cordSys);
		this.addMouseListener(this);
		this.addKeyListener(this);
		this.addMouseMotionListener(this);
		this.addMouseWheelListener(this);
		repaint();
	}
	public void paint(Graphics g)
	{
		dimScene=new Dimension(this.getSize().width,this.getSize().height);
		image=new BufferedImage(dimScene.width,dimScene.height-40,BufferedImage.TYPE_INT_RGB);
		imgGraphics=image.getGraphics();
		this.g=this.getGraphics();
	}
	public void paintScene()
	{
		imgGraphics.setColor(Color.white);
		imgGraphics.fillRect(0, 0, dimScene.width, dimScene.height);
		if(cordSys.isSet)cordSys.drawAxis(imgGraphics);
		if(puppetSet)puppet.drawPuppet(imgGraphics);
		g.drawImage(image, 0, 0, image.getWidth(), image.getHeight(), this);
	}
	
	public void mouseClicked(MouseEvent evt) 
	{
		if(!panScreen)
		{
			if(countClick==0)
			{
				cordSys.setCamera(0.75F,0.75F, 200,200);
				cordSys.setCoordinateSys(200, 200, 200, new Point2D(evt.getX(),evt.getY()));
			}
			else if(countClick==1)
			{
				puppet.startPoint.x=Vector3D.mapTo3D(cordSys.origin,cordSys.org, cordSys.xXis2d,cordSys.xXis, new Point2D(evt.getX(),evt.getY()));
			}
			else if(countClick==2)
			{
				puppet.startPoint.y=Vector3D.mapTo3D(cordSys.origin,cordSys.org, cordSys.yXis2d,cordSys.xXis, new Point2D(evt.getX(),evt.getY()));
			}
			else if(countClick==3)
			{
				puppet.endPoint.x=Vector3D.mapTo3D(cordSys.origin,cordSys.org, cordSys.xXis2d,cordSys.xXis, new Point2D(evt.getX(),evt.getY()));
			}
			else if(countClick==4)
			{
				puppet.endPoint.y=Vector3D.mapTo3D(cordSys.origin,cordSys.org, cordSys.yXis2d,cordSys.xXis, new Point2D(evt.getX(),evt.getY()));
			}
			else if(countClick==5)
			{
				puppet.makePuppet(50);
				walker=new Walker(puppet,this);
				Thread t=new Thread(walker);
				t.start();			
				puppetSet=true;
			}
			countClick++;
			paintScene();
		}
	}
	@Override
	public void mouseEntered(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void mouseExited(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void mousePressed(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void mouseReleased(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void keyPressed(KeyEvent evt)
	{
		if(evt.getKeyCode()==KeyEvent.VK_UP)
			cordSys.camUp();
		else if(evt.getKeyCode()==KeyEvent.VK_DOWN)
			cordSys.camDown();
		else if(evt.getKeyCode()==KeyEvent.VK_LEFT)
			cordSys.camLeft();
		else if(evt.getKeyCode()==KeyEvent.VK_RIGHT)
			cordSys.camRight();
		paintScene();
		if(evt.getKeyCode()==KeyEvent.VK_SPACE)
			panScreen=!panScreen;
			
	}
	@Override
	public void keyReleased(KeyEvent arg0) {
		// TODO Auto-generated method stub
		//panScreen=false;
	}
	@Override
	public void keyTyped(KeyEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void mouseDragged(MouseEvent e) 
	{
		if(panScreen==true)
		{
			cordSys.origin=new Point2D(e.getX(),e.getY());
			paintScene();
		}
	}
	@Override
	public void mouseMoved(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void mouseWheelMoved(MouseWheelEvent evt) 
	{
		if(evt.getWheelRotation()>0)cordSys.zoomOut();
		else if(evt.getWheelRotation()<0)cordSys.zoomIn();
		paintScene();
	}
}
