Un bras robot dirigé depuis l'ordinateur Serveur
/* Auteur: Nicolas JANEY */
/* nicolas.janey@univ-fcomte.fr */
/* Novembre 2001 */
import java.applet.Applet;
import java.awt.*;
import java.net.*;
import java.awt.event.*;
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.geometry.*;
import com.sun.j3d.utils.universe.*;
import javax.media.j3d.*;
import javax.vecmath.*;
import java.util.Enumeration;
public class BrasRobot extends Applet {
public String host ;
public float angle1 = 0.0F ;
public float angle2 = 0.0F ;
public float angle3 = 0.0F ;
private static float extension = (float) Math.PI/4 ;
private TransformGroup l1RotTrans ;
private Transform3D yAxis ;
private TransformGroup articulation ;
private Transform3D arti ;
private TransformGroup rotationTete ;
private Transform3D rotTete ;
public BranchGroup createSceneGraph(SimpleUniverse u) {
Color3f noir = new Color3f(0.0f,0.0f,0.0f);
Color3f blanc = new Color3f(1.0f,1.0f,1.0f);
Color3f jaune = new Color3f(1.0f,1.0f,0.0f);
Color3f gris = new Color3f(0.6f,0.6f,0.6f);
Color3f vert = new Color3f(0.0f,1.0f,0.0f);
Color3f rouge = new Color3f(1.0f,0.0f,0.0f);
Color3f bleu = new Color3f(0.0f,0.0f,1.0f);
Color3f grisFonce = new Color3f(0.2f, 0.2f, 0.2f);
Color3f bgColor = new Color3f(0.05f, 0.05f, 0.2f);
Transform3D t;
BranchGroup objRoot = new BranchGroup();
TransformGroup objScale = new TransformGroup();
Transform3D t3d = new Transform3D();
t3d.setScale(0.3);
objScale.setTransform(t3d);
BoundingSphere bounds = new BoundingSphere(new Point3d(0.0,0.0,0.0),100.0);
Background bg = new Background(bgColor);
bg.setApplicationBounds(bounds);
TransformGroup viewTransform = new TransformGroup();
Transform3D tr = new Transform3D();
tr.setTranslation(new Vector3f(-1.0F,-1.0F,0.0F));
viewTransform.setTransform(tr);
TransformGroup viewRotation = new TransformGroup();
Transform3D viewRot = new Transform3D();
viewRot.setRotation(new AxisAngle4f(1.0F,1.0F,0.0F,(float) Math.PI/5.0F)) ;
viewRotation.setTransform(viewRot);
TransformGroup rotx1 = new TransformGroup();
Transform3D rx1 = new Transform3D();
rx1.setRotation(new AxisAngle4f(1.0F,0.0F,0.0F,(float) Math.PI/2.0F)) ;
rotx1.setTransform(rx1);
TransformGroup rotx2 = new TransformGroup();
Transform3D rx2 = new Transform3D();
rx2.setRotation(new AxisAngle4f(1.0F,0.0F,0.0F,(float) Math.PI/2.0F)) ;
rotx2.setTransform(rx2);
Material matRouge = new Material(gris,noir,rouge,rouge,100.0f);
Appearance a1 = new Appearance();
matRouge.setLightingEnable(true);
a1.setMaterial(matRouge);
Box box1 = new Box(1.0F,0.15F,1.0F,a1);
Material matVert = new Material(gris,noir,vert,vert,100.0f);
Appearance a2 = new Appearance();
matVert.setLightingEnable(true);
a2.setMaterial(matVert);
Box box2 = new Box(1.0f,0.1F,0.1F,a2);
Cylinder cyl1 = new Cylinder(0.4F,0.25F,a2);
Cylinder cyl2 = new Cylinder(0.2F,0.25F,a2);
Material matBleu = new Material(gris,noir,bleu,bleu,100.0f);
Appearance a3 = new Appearance();
matVert.setLightingEnable(true);
a3.setMaterial(matBleu);
Box box3 = new Box(1.0f,0.1F,0.1F,a3);
AmbientLight aLgt = new AmbientLight(grisFonce);
aLgt.setInfluencingBounds(bounds);
l1RotTrans = new TransformGroup();
l1RotTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
yAxis = new Transform3D();
yAxis.rotZ(angle1);
l1RotTrans.setTransform(yAxis);
Vector3f lDirect1 = new Vector3f(0.0F,0.0F,4.0F);
lDirect1.negate();
Light lgt1 = new DirectionalLight(blanc,lDirect1);
lgt1.setInfluencingBounds(bounds);
Group robot = new Group() ;
TransformGroup translation1 = new TransformGroup();
Transform3D trans = new Transform3D();
trans.setTranslation(new Vector3f(1.0F,0.0F,0.0F));
translation1.setTransform(trans);
TransformGroup translation2 = new TransformGroup();
Transform3D trans2 = new Transform3D();
trans2.setTranslation(new Vector3f(1.0F,0.0F,0.0F));
translation2.setTransform(trans2);
articulation = new TransformGroup();
articulation.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
arti = new Transform3D();
arti.rotZ(-angle2); ;
articulation.setTransform(arti);
TransformGroup translation3 = new TransformGroup();
Transform3D trans3 = new Transform3D();
trans3.setTranslation(new Vector3f(1.0F,0.0F,0.0F));
translation3.setTransform(trans3);
TransformGroup translation4 = new TransformGroup();
Transform3D trans4 = new Transform3D();
trans4.setTranslation(new Vector3f(1.0F,0.0F,0.0F));
translation4.setTransform(trans4);
rotationTete = new TransformGroup();
rotationTete.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
rotTete = new Transform3D();
rotTete.rotX(angle3) ;
rotationTete.setTransform(rotTete);
Material matJaune = new Material(gris,noir,jaune,jaune,100.0f);
Appearance a4 = new Appearance();
matJaune.setLightingEnable(true);
a4.setMaterial(matJaune);
Box box4 = new Box(0.1f,0.25F,0.4F,a4);
Material matBlanc = new Material(gris,noir,blanc,blanc,100.0f);
Appearance a5 = new Appearance();
matBlanc.setLightingEnable(true);
a5.setMaterial(matBlanc);
Box box5 = new Box(0.1f,0.3F,0.3F,a5);
Box box6 = new Box(0.1f,0.3F,0.3F,a5);
objRoot.addChild(objScale);
objScale.addChild(bg);
objScale.addChild(viewTransform);
viewTransform.addChild(viewRotation);
viewRotation.addChild(box1);
viewRotation.addChild(rotx1);
rotx1.addChild(cyl1);
viewRotation.addChild(l1RotTrans);
l1RotTrans.addChild(robot);
robot.addChild(translation1);
translation1.addChild(box2);
box2.addChild(translation2);
translation2.addChild(articulation);
articulation.addChild(rotx2);
rotx2.addChild(cyl2);
articulation.addChild(translation3);
translation3.addChild(box3);
box3.addChild(translation4);
translation4.addChild(rotationTete);
rotationTete.addChild(box4);
objScale.addChild(aLgt);
objScale.addChild(lgt1);
objRoot.compile();
return objRoot;
}
public void miseAJour(int a,int b,int c) {
angle1 = 0.4F+(float) Math.toRadians(a)/2.0F;
angle2 = 0.4F+(float) Math.toRadians(b)/2.0F;
angle3 = 0.4F+(float) Math.toRadians(c)/2.0F;
yAxis.rotZ(angle1);
l1RotTrans.setTransform(yAxis);
arti.rotZ(-angle2); ;
articulation.setTransform(arti);
rotTete.rotX(angle3) ;
rotationTete.setTransform(rotTete);
}
public void init() {
URL url = getDocumentBase() ;
host = url.getHost() ;
System.out.println("----> "+host) ;
setLayout(new BorderLayout());
GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration();
Canvas3D c3D = new Canvas3D(config);
add("Center",c3D);
SimpleUniverse u = new SimpleUniverse(c3D);
BranchGroup scene = createSceneGraph(u);
u.getViewingPlatform().setNominalViewingTransform();
u.addBranchGraph(scene);
ThreadBrasRobot te6 = new ThreadBrasRobot(this) ;
te6.start() ;
}
public BrasRobot() {
}
public static void main(String[] args) {
new MainFrame(new BrasRobot(),400,256);
}
}
/* Auteur: Nicolas JANEY */
/* nicolas.janey@univ-fcomte.fr */
/* Novembre 2001 */
import java.awt.* ;
import java.net.* ;
import java.io.* ;
public class ThreadBrasRobot extends Thread {
private BrasRobot e6 ;
public ThreadBrasRobot(BrasRobot e) {
e6 = e ;
}
public void run() {
boolean bRun = true ;
while ( bRun) {
Socket s ;
int port = 5556 ;
try {
s = new Socket(e6.host,port) ;
InputStream is = s.getInputStream() ;
DataInputStream ois = new DataInputStream(is) ;
int a1 = ois.readInt() ;
int a2 = ois.readInt() ;
int a3 = ois.readInt() ;
e6.miseAJour(a1,a2,a3) ;
System.out.println(a1+" : "+a2+" : "+a3) ;
ois.close() ; }
catch (UnknownHostException uhe) {
bRun = false ; }
catch (IOException ioe) {
bRun = false ; } ;
try {
sleep(100) ; }
catch (Exception e) { } ; }
}
}
/* Auteur: Nicolas JANEY */
/* nicolas.janey@univ-fcomte.fr */
/* Novembre 2001 */
import java.net.* ;
import java.io.* ;
public class ServeurRobot extends Thread {
private ServerSocket srv ;
private ServeurBrasRobot s6 ;
public ServeurRobot(ServerSocket s,ServeurBrasRobot sp) {
srv = s ;
s6 = sp ;
}
public void run() {
while ( true ) {
try {
Socket connexion = srv.accept() ;
OutputStream os = connexion.getOutputStream() ;
DataOutputStream oos = new DataOutputStream(os) ;
oos.writeInt(s6.a1) ;
oos.writeInt(s6.a2) ;
oos.writeInt(s6.a3) ;
connexion.close() ; }
catch(IOException e) { } }
}
}
/* Auteur: Nicolas JANEY */
/* nicolas.janey@univ-fcomte.fr */
/* Novembre 2001 */
import java.net.* ;
import java.awt.* ;
import java.io.* ;
import java.awt.event.*;
public class ServeurBrasRobot implements MouseListener,MouseMotionListener {
private static Frame f ;
private static Scrollbar sb1 ;
private static Scrollbar sb2 ;
private static Scrollbar sb3 ;
private static EtatScrollBar esb1 ;
private static EtatScrollBar esb2 ;
private static EtatScrollBar esb3 ;
public int a1 ;
public int a2 ;
public int a3 ;
public static void main(String [] args) {
ServeurBrasRobot se6 = new ServeurBrasRobot() ;
}
public ServeurBrasRobot() {
ServerSocket srv ;
int port = 5556 ;
try {
srv = new ServerSocket(port) ;
System.out.println("Serveur Ok") ;
ServeurRobot s6 = new ServeurRobot(srv,this) ;
s6.start() ; }
catch(IOException e) { }
f = new Frame("Serveur BrasRobot") ;
f.setSize(200,200) ;
Panel c = new Panel();
c.setLayout(new GridLayout(1,3,2,0));
sb1 = new Scrollbar(Scrollbar.VERTICAL,0,1,0,255);
c.add(sb1);
sb2 = new Scrollbar(Scrollbar.VERTICAL,0,1,0,255);
c.add(sb2);
sb3 = new Scrollbar(Scrollbar.VERTICAL,0,1,0,255);
c.add(sb3);
esb1 = new EtatScrollBar(sb1,0) ;
sb1.addMouseListener(this);
sb1.addMouseMotionListener(this);
esb2 = new EtatScrollBar(sb2,0) ;
sb2.addMouseListener(this);
sb2.addMouseMotionListener(this);
esb3 = new EtatScrollBar(sb3,0) ;
sb3.addMouseListener(this);
sb3.addMouseMotionListener(this);
f.setLayout(new BorderLayout());
f.add("East",c);
f.show() ;
while ( true ) {
try {
Thread.sleep(100) ; }
catch (Exception e) { } }
}
private void miseAJour(int a,int b,int c) {
a1 = a ;
a2 = b ;
a3 = c ;
}
public void mouseClicked(MouseEvent e ) {
}
public void mouseReleased(MouseEvent e ) {
}
private void traitementScrollBar(EtatScrollBar esb,MouseEvent e) {
Scrollbar sb = esb.getScrollbar() ;
if ( e.getSource() == sb ) {
int y ;
int h ;
if ( sb.getOrientation() == Scrollbar.VERTICAL) {
y = e.getY() ;
h =(int) sb.getSize().getHeight() ; }
else {
y = e.getX() ;
h =(int) sb.getSize().getWidth() ; }
int pos = -1 ;
if ( y < 16 ) {
pos = ( sb.getValue() - 1 < sb.getMinimum() ) ? sb.getMinimum() : sb.getValue()-1 ; }
else
if ( y > h-16 ) {
pos = ( sb.getValue() + 1 > sb.getMaximum()-1 ) ? sb.getMaximum()-1 : sb.getValue()+1 ;}
else {
int ymin = 16-3+(h-32)*sb.getValue()/255 ;
int ymax = ymin+6 ;
if ( y < ymin )
pos = ( sb.getValue() - sb.getBlockIncrement() < sb.getMinimum() ) ? sb.getMinimum() : (sb.getValue()-sb.getBlockIncrement()) ;
else
if ( y > ymax ) {
pos = ( sb.getValue() + sb.getBlockIncrement() > sb.getMaximum()-1 ) ? sb.getMaximum()-1 : (sb.getValue()+sb.getBlockIncrement()) ; } }
if ( pos != -1 ) {
if ( sb == sb1 )
miseAJour(pos,sb2.getValue(),sb3.getValue()) ;
if ( sb == sb2 )
miseAJour(sb1.getValue(),pos,sb3.getValue()) ;
if ( sb == sb3 )
miseAJour(sb1.getValue(),sb2.getValue(),pos) ; }
else
esb.setEtat(1) ; }
}
public void mousePressed(MouseEvent e ) {
traitementScrollBar(esb1,e) ;
traitementScrollBar(esb2,e) ;
traitementScrollBar(esb3,e) ;
}
public void mouseDragged(MouseEvent e ) {
}
private void traitement2ScrollBar(EtatScrollBar esb,MouseEvent e) {
if ( ( e.getSource() == sb1 ) || ( e.getSource() == sb2 ) || ( e.getSource() == sb3 ) )
if ( esb.getEtat() == 1 ) {
esb.setEtat(0) ;
miseAJour(sb1.getValue(),sb2.getValue(),sb3.getValue()) ; }
}
public void mouseMoved(MouseEvent e ) {
traitement2ScrollBar(esb1,e) ;
traitement2ScrollBar(esb2,e) ;
traitement2ScrollBar(esb3,e) ;
}
public void mouseEntered(MouseEvent e ) {
}
public void mouseExited(MouseEvent e ) {
}
}
/* Auteur: Nicolas JANEY */
/* nicolas.janey@univ-fcomte.fr */
/* Novembre 2001 */
import java.awt.*;
public class EtatScrollBar {
private Scrollbar sb ;
private int etat ;
public EtatScrollBar(Scrollbar s,int e) {
sb = s ;
etat = e ;
}
public void setEtat(int e) {
etat = e ;
}
public int getEtat() {
return(etat) ;
}
public Scrollbar getScrollbar() {
return(sb) ;
}
}
Remarques, erreurs
nicolas.janey@univ-fcomte.fr