Exemple de classe utilisant Java 2D
GrandeOurse3DJava2D.class
Fichier source
/* 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);
}
}
/* Auteur: Nicolas JANEY */
/* nicolas.janey@univ-fcomte.fr */
/* Novembre 2001 */
public class Vector3d {
public double x;
public double y;
public double z;
public Vector3d() {
this.x = 0.0 ;
this.y = 0.0 ;
this.z = 0.0 ;
}
public Vector3d(float x,float y,float z) {
this.x = x ;
this.y = y ;
this.z = z ;
}
public Vector3d(double x,double y,double z) {
this.x = x ;
this.y = y ;
this.z = z ;
}
}
/* Auteur: Nicolas JANEY */
/* nicolas.janey@univ-fcomte.fr */
/* Novembre 2001 */
public class Transform3D {
public double [][] m = new double[4][4];
public Transform3D() {
for ( int i = 0 ; i < 4 ; i++ )
for ( int j = 0 ; j < 4 ; j++ )
m[i][j] = 0.0 ;
}
public void toIdentite() {
for ( int i = 0 ; i < 4 ; i++ )
for ( int j = 0 ; j < 4 ; j++ )
m[i][j] = ( i == j ) ? 1.0 : 0.0 ;
}
public void toTranslation(double dx,double dy,double dz) {
toIdentite();
m[0][3] = dx;
m[1][3] = dy;
m[2][3] = dz;
}
public void toRotationX(double a) {
toIdentite();
m[2][1] = Math.sin(a);
m[1][2] = -m[2][1];
m[1][1] = m[2][2] = Math.cos(a);
}
public void toRotationY(double a) {
toIdentite();
m[0][2] = Math.sin(a);
m[2][0] = -m[0][2];
m[0][0] = m[2][2] = Math.cos(a);
}
public void toRotationZ(double a) {
toIdentite();
m[1][0] = Math.sin(a);
m[0][1] = -m[1][0];
m[1][1] = m[0][0] = Math.cos(a);
}
public Vector3d mult(Vector3d v) {
Vector3d r = new Vector3d();
r.x = m[0][0]*v.x+m[0][1]*v.y+m[0][2]*v.z+m[0][3];
r.y = m[1][0]*v.x+m[1][1]*v.y+m[1][2]*v.z+m[1][3];
r.z = m[2][0]*v.x+m[2][1]*v.y+m[2][2]*v.z+m[2][3];
return(r);
}
}