package main; import java.awt.*; import java.awt.event.*; import java.util.LinkedList; import java.util.ListIterator; import javax.swing.*; @SuppressWarnings("serial") public class Ecran extends JFrame{ Panel panneau=null;//PANEL D'AFFICHAGE PointProj P0 = new PointProj(new Point (80,45,-100));//Point de projection //Constructeur Ecran Ecran(){ setTitle("ECRAN"); setSize(300,500); panneau=new Panel(); getContentPane().add(panneau); P0 = new PointProj(new Point (150,250,-100000)); } //Panel d'affichage class Panel extends JPanel implements KeyListener{ Solide S; public void setSolide(Solide S){ this.S=S; } public void paintComponent(Graphics g){ //super.paintComponents(g); Solide2D S2D=S.to2D(); S2D.L.it=S2D.L.liste.listIterator(); Surface2D Surf=S2D.L.next(); while (Surf!=null){ g.setColor(Surf.c); g.drawPolygon(Surf.Poly); Surf=S2D.L.next(); } } @Override public void keyPressed(KeyEvent e) { // TODO Auto-generated method stub } @Override public void keyReleased(KeyEvent e) { // TODO Auto-generated method stub } @Override public void keyTyped(KeyEvent e) { // TODO Auto-generated method stub } } //Rotation class Rotate{ Point Centre; double AngleY;//suivant plan ortho. a Y double AngleX;//suivant plan ortho. a X double AngleZ;//suivvant plan orthogonal a Z Solide S; //Constructeur de la rotation public Rotate(Point Centre, double AngleX, double AngleY, double AngleZ, Solide S){ this.Centre=Centre; this.AngleX=AngleX; this.AngleY=AngleY; this.AngleZ=AngleZ; this.S=S; rot(); } public void rot(){//on place le centre de rotation a l'origine via translation t, on fait la rotation puis on fais la translation inverse a t S.L.it=S.L.liste.listIterator(); Surface Surf=S.L.next(); while (Surf!=null){ Surf.P.it=Surf.P.liste.listIterator(); Point P=Surf.P.next(); while(P!=null){ P.MoveBy(Centre.Opposite());//translation du centre de rotation pour placer celui-ci a l'origine //rotation sur plan orthogonal a Oz P.MoveTo(rot2D(P.x,P.y,AngleZ,true),rot2D(P.x,P.y,AngleZ,false),P.z); //rotation sur plan orthogonal a Ox P.MoveTo(P.x,rot2D(P.y,P.z,AngleX,true),rot2D(P.y,P.z,AngleX,false)); //rotation sur plan orthogonal a Oy P.MoveTo(rot2D(P.z,P.x,AngleY,false), P.y, rot2D(P.z,P.x,AngleY,true)); P.MoveBy(Centre); P=Surf.P.next(); } Surf=S.L.next(); } } public double rot2D(double C1, double C2, double Angle, boolean abs){//Rotation dans le plan perpendiculaire a OC3, le boolean servant a choisir quelle coordonee on veut recuperer if(abs)//on veut C1' return((C1*Math.cos(Angle)-C2*Math.sin(Angle))); else return((C1*Math.sin(Angle)+C2*Math.cos(Angle))); } } //Translation class Translate{ Point vecteur; Solide S; Translate(Solide S, Point Vecteur){ vecteur=Vecteur; this.S=S; trans(); } Translate(Solide S,int x, int y, int z){ new Translate(S,new Point(x,y,z)); } public void trans(){ S.L.it=S.L.liste.listIterator(); Surface Surf=S.L.next(); while (Surf!=null){ Surf.P.it=Surf.P.liste.listIterator(); Point P=Surf.P.next(); while(P!=null){ System.out.println("translation de "+P.toString()+" par "+ vecteur.toString()); P.MoveBy(vecteur); //System.out.println("nouveau point "+P.toString()); P=Surf.P.next(); } Surf=S.L.next(); } } } //Classe de projection de points class PointProj{ Point P0;//Point de projection double D0=10;//distance de l'origine par rapport au point de projection //Constructeur public PointProj(Point P0){ this.P0=new Point(0,0,P0.GetZ());//on prends P0 sur l'axe de projection afin de simplifier les calculs D0=P0.GetZ(); } //projection sur P0 public Point2D Proj(Point P){ return(new Point2D( P.GetX()*P0.GetZ()/(P0.GetZ()-P.GetZ()), P.GetY()*P0.GetZ()/(P0.GetZ()-P.GetZ()) )); } } //Definition de la classe Ligne public class Ligne{ Point A,B; public Ligne(Point A, Point B){ this.A=A; this.B=B; } Ligne2D to2D(){ return( new Ligne2D( P0.Proj(A), P0.Proj(B) ) ); } } //Definition de la classe Surface public class Surface{ PointListe P; public Color c; Surface(PointListe P, Color c){ this.P=P; this.c=c; } Surface2D to2D(){ return new Surface2D(this); } } //Classe définissant un solide class Solide{ SurfaceListe L=new SurfaceListe(); public Solide2D to2D(){ return new Solide2D(this); } } //Classe definissant le type Point class Point{ //Coordonnees dans l'espace du point double x,y,z; //Constructeur Point(double x, double y, double z){ this.x=x; this.y=y; this.z=z; } //Permet de deplacer le point suivant le vecteur (X,Y,Z) public void MoveBy(double X, double Y, double Z){ x=x+X; y=y+Y; z=z+Z; } //Permet de deplacer le point suivant le vecteur V public void MoveBy(Point V){//On reutilise la structure de donnee point meme si V est un vecteur x=x+V.x; y=y+V.y; z=z+V.z; } //Calcule l'oppose public Point Opposite(){ return new Point(-x,-y,-z); } //déplace le point jusqu'aux coordonnées indiquées public void MoveTo(double X, double Y, double Z){ x=X; y=Y; z=Z; } //retourne x public double GetX(){ return x; } //retourne y public double GetY(){ return y; } //retourne z public double GetZ(){ return z; } public String toString(){ return ""+GetX()+" "+GetY()+" "+GetZ(); } } //Coordonnee du point sur l'ecran class Point2D{ double x,y; Point2D(double x, double y){ this.x=x; this.y=y; } public double GetX(){return x;} public double GetY(){return y;} } //Definition de la classe Ligne2D, projetee de Ligne sur l'ecran class Ligne2D{ Point2D A,B; Ligne2D(Point2D A, Point2D B){ this.A=A; this.B=B; } } //Definition de la classe Surface2D, projetee de Ligne sur l'ecran class Surface2D{ Polygon Poly=new Polygon(); Color c; Surface2D(Surface S){ S.P.it=S.P.liste.listIterator(); Point P=S.P.next(); while(P!=null){ //System.out.println("Coordonnees P: "+P.x+" "+P.y+" "+P.z); Point2D P2D=P0.Proj(P); //System.out.println("Coordonnees P2D: "+P2D.x+" "+P2D.y); //System.out.println("Coordonnees P2D: "+P2D.GetX()+" "+P2D.GetY()); Poly.addPoint((int)P2D.GetX(),(int)P2D.GetY()); P=S.P.next(); } //System.out.println("Couleur "+S.c); this.c=S.c; } } //Definition de la classe Solide2D, projete de Solide sur l'ecran class Solide2D{ Surface2DListe L=new Surface2DListe(); Solide2D(Solide S){ S.L.it=S.L.liste.listIterator(); Surface Surf=S.L.next(); while(Surf!=null){ L.add(new Surface2D(Surf)); Surf=S.L.next(); } } public void tri(Solide S){//on trie les surfaces selon leur profondeur afin de modifier leur ordre d'affichage S.L.it=S.L.liste.listIterator(); } } //Definition de Listes de Surfaces class Surface2DListe{ LinkedList liste; ListIterator it=null; Surface2DListe(){//Constructe2Dur de la liste vide liste=new LinkedList(); } //Ajout d'une Ligne public synchronized void add(Surface2D L){ liste.addFirst(L); } //renvoi et suppression du premier element public synchronized Surface2D pop(){ if (!this.estVide()) return liste.removeFirst(); else return null; } //permet de determiner si une liste est vide public synchronized boolean estVide(){ return liste.isEmpty(); } public synchronized Surface2D next(){ if(it.hasNext()) return it.next(); else return null; } } //Definition de Listes de Surfaces class SurfaceListe{ LinkedList liste; ListIterator it=null; SurfaceListe(){//Constructeur de la liste vide liste=new LinkedList(); } //Ajout d'une Ligne public synchronized void add(Surface L){ liste.addFirst(L); } //renvoi et suppression du premier element public synchronized Surface pop(){ if (!this.estVide()) return liste.removeFirst(); else return null; } //permet de determiner si une liste est vide public synchronized boolean estVide(){ return liste.isEmpty(); } public synchronized Surface next(){ if (it.hasNext()) return it.next(); else return null; } } //Definition de Listes de lignes class LigneListe{ LinkedList liste; ListIterator it=liste.listIterator(); LigneListe(){//Constructeur de la liste vide liste=new LinkedList(); } //Ajout d'une Ligne public synchronized void add(Ligne L){ liste.addFirst(L); } //renvoi et suppression du premier element public synchronized Ligne pop(){ if (!this.estVide()) return liste.removeFirst(); else return null; } //permet de determiner si une liste est vide public synchronized boolean estVide(){ return liste.isEmpty(); } public synchronized Ligne next(){ if (it.hasNext()) return it.next(); else return null; } } //Definition de Listes de points class PointListe{ LinkedList liste; ListIterator it=null; PointListe(){//Constructeur de la liste vide liste=new LinkedList(); } //Ajout d'un Point public synchronized void add(Point P){ liste.addFirst(P); } //renvoi et suppression du premier element public synchronized Point pop(){ if (!this.estVide()) return liste.removeFirst(); else return null; } //permet de determiner si une liste est vide public synchronized boolean estVide(){ return liste.isEmpty(); } public synchronized Point next(){ if (it.hasNext()) return it.next(); else return null; } } }