/* Auteur: Nicolas JANEY */ /* nicolas.janey@univ-fcomte.fr */ /* Novembre 2001 */ import java.util.*; import java.awt.*; import java.awt.geom.*; import java.awt.event.*; import java.applet.*; import com.sun.j3d.utils.applet.MainFrame; public class GrandeOurse3DJava2D extends Applet implements MouseListener,MouseMotionListener { private int initX = 0 ; private int initY = 0 ; private int px = 0 ; private int py = 0 ; private int dx = 0 ; private int dy = 0 ; private Vector3d eta = new Vector3d( 2.6782868 , 0.95249915 ,10.211735-8); private Vector3d miz = new Vector3d( 1.3972227 , 0.11056229 , 7.9981213-8); private Vector3d dze = new Vector3d( 1.3814067 , 0.11733321 , 8.1529656-8); private Vector3d eps = new Vector3d( 0.70122137, 0.080953359, 7.6173640-8); private Vector3d del = new Vector3d( 0.0 , 0.0 , 8.04-8); private Vector3d gam = new Vector3d(-0.8883190 , 0.82615963 ,15.251831-8); private Vector3d alp = new Vector3d(-1.4533803 ,-1.0239428 , 9.6983930-8); private Vector3d bet = new Vector3d(-1.2871224 ,-0.10782208 , 7.1645021-8); private float dz = 8.0F; public void init() { setBackground(Color.black); addMouseMotionListener(this); addMouseListener(this); } public void positionEcran(Vector3d e,Vector3d ecr,float ay,float ax) { Transform3D t = new Transform3D(); t.toRotationY(-ay); Vector3d e1 = t.mult(e); t.toRotationX(-ax); Vector3d e2 = t.mult(e1); float x =(float) e2.x; float y =(float) e2.y; float z =(float) e2.z+dz; x /= z; y /= -z; ecr.x = x*300+128; ecr.y = y*300+128; ecr.z = z; } public void afficheEtoile(Vector3d e,float dz,float ax,float ay,Graphics2D g2) { Vector3d v = new Vector3d(); ax *= 3.14159F/180.0F; ay *= 3.14159F/180.0F; positionEcran(e,v,ax,ay); g2.fill(new Ellipse2D.Double(v.x-3,v.y-3,6,6)) ; } public void afficheLiaison(Vector3d e1,Vector3d e2,float dz,float ax,float ay,Graphics2D g2) { Vector3d v1 = new Vector3d(); Vector3d v2 = new Vector3d(); ax *= 3.14159F/180.0F; ay *= 3.14159F/180.0F; positionEcran(e1,v1,ax,ay); positionEcran(e2,v2,ax,ay); g2.draw(new Line2D.Double(v1.x,v1.y,v2.x,v2.y)) ; } public void paint(Graphics g) { Graphics2D g2 =(Graphics2D) g; g2.setColor(Color.white) ; afficheEtoile(eta,dz,dx,dy,g2); afficheEtoile(miz,dz,dx,dy,g2); afficheEtoile(dze,dz,dx,dy,g2); afficheEtoile(eps,dz,dx,dy,g2); afficheEtoile(del,dz,dx,dy,g2); afficheEtoile(gam,dz,dx,dy,g2); afficheEtoile(alp,dz,dx,dy,g2); afficheEtoile(bet,dz,dx,dy,g2); afficheLiaison(eta,dze,dz,dx,dy,g2); afficheLiaison(dze,eps,dz,dx,dy,g2); afficheLiaison(eps,del,dz,dx,dy,g2); afficheLiaison(del,gam,dz,dx,dy,g2); afficheLiaison(del,alp,dz,dx,dy,g2); afficheLiaison(alp,bet,dz,dx,dy,g2); afficheLiaison(bet,gam,dz,dx,dy,g2); } public String getAppletInfo() { return "La Grande Ourse en Java2D."; } public void mouseDragged(MouseEvent e) { int x = e.getX(); int y = e.getY(); dx = px + x - initX; dy = py + y - initY; repaint(); } public void mouseMoved(MouseEvent e) { } public void mouseClicked(MouseEvent e) { } public void mouseEntered(MouseEvent e) { int x = e.getX() ; int y = e.getY() ; /* System.out.println(x+" "+y); PopupMenu pm = new PopupMenu("Hello"); MenuItem mi1 = new MenuItem("Avec les noms"); MenuItem mi2 = new MenuItem("Sans les noms"); pm.add(mi); this.add(pm); pm.show(this,x,y);*/ } public void mouseExited(MouseEvent e) { } public void mousePressed(MouseEvent e) { px = dx ; py = dy ; initX = e.getX() ; initY = e.getY() ; } public void mouseReleased(MouseEvent e) { int x = e.getX(); int y = e.getY(); dx = px + x - initX; dy = py + y - initY; repaint(); } public static void main(String[] args) { new MainFrame(new GrandeOurse3DJava2D(),256,256); } }