comment pouvoir agrandir la dimens d'un canvas3D a plus de 2048*2048 - Java - Programmation
Marsh Posté le 15-05-2006 à 20:46:59
Heu, mais qu'est-ce que c'est que ce hack immonde ?
Canvas3D c'est la zone de rendu de toute application Java3D. Celle que tu vas mettre dans une frame ou une applet.
La taille du canvas c'est donc la taille de l'image produite, plus elle est grande et plus les resources demandées sont importantes et on peut comprendre que son comportement ne soit plus très sûr passé une certaine taille...
Donc si tu doubles les dimensions du canvas tu auras une impression de zoom car le comportement par défaut de Java3D est d'agrandir en conséquence l'image produite. Tes objets 3D aparaissent plus gros, mais ce n'est pas un zoom.
Tu fais de la 3D, donc zoomer revient à modifier la focale de ta vue, comme sur un appareil photo.
Tu dois récupérer la vue associée à ton univers et modifier ses caractéristiques (voir les méthodes setFieldOfView() ou setScreenScale() de View)
Marsh Posté le 15-05-2006 à 12:38:47
j'ai un gros probleme avec le canvas3D;comment peut-on agrandir la dimension d'un canvas3D au dela de 2048*2048??? , parceque je dois realiser une application dans laquelle l'utilisateur doit pouvoir zoomer, et quand j'agrandie la taille du canvas l'affichage se bousille lorseque les dimension du canvas depasse 2048*2048.
si vous voulez essayer le code suivant vous vous renderez compte du probléme:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
// classes Java 3D
import com.sun.j3d.utils.geometry.*;
import com.sun.j3d.utils.universe.SimpleUniverse;
import javax.media.j3d.*;
import java.awt.image.*;//BufferedImage;
import javax.vecmath.*;
import java.util.*;
import com.sun.j3d.utils.picking.behaviors.*;
import com.sun.j3d.utils.picking.*;
public class Zoom extends Frame implements MouseListener,MouseMotionListener/*,Adjustable*/{
Canvas3D canvas3D = new Canvas3D(SimpleUniverse.getPreferredConfiguration());
SimpleUniverse simpleU= new SimpleUniverse(canvas3D); ;
TransformGroup tg=new TransformGroup();
Appearance app_vert= new Appearance();
Appearance app_marron= new Appearance();
TransformGroup tg1=new TransformGroup();TransformGroup tg2=new TransformGroup();
TransformGroup tg3=new TransformGroup();TransformGroup tg4=new TransformGroup();
TransformGroup tg5=new TransformGroup();
BoundingSphere behaveBounds = new BoundingSphere();
JButton dimension;//bouton qui va nous donner les dimensions du canvas3D;
JButton zoom_avant;//bouton qui va nous permettre d'agrandir le canvas3D;
JButton zoom_arriere;//bouton qui va nous permettre de diminuer la taille du canvas3D;
JButton defaut;//bouton qui va nous permettre de remettre la taille du canvas3D a defaut
Scrollbar scrollver=new Scrollbar(Scrollbar.VERTICAL, 0, 1, 0, 255);//barre de defilement verticale
Scrollbar scrollhor=new Scrollbar(Scrollbar.HORIZONTAL, 0, 1, 0, 255);//barre de defilement horizontale
int anc_val_ver=0;int anc_val_hor=0;
Panel pan;
//---------constructeur
public Zoom(){
super("Zoom et defilement d'une zone de dessin" );
setLayout(new BorderLayout());
Panel framepane=new Panel();
framepane.setBackground(Color.cyan);
this.add(framepane);
framepane.setLayout(null);
this.setSize(500,500);this.setVisible(true);
// on crée une apparence de couleur verte
ColoringAttributes vert=new ColoringAttributes();
vert.setColor(0.1f,1.0f,0.1f);
vert.setShadeModel(ColoringAttributes.NICEST);
app_vert.setColoringAttributes(vert);
// on crée une apparence de couleur marron
// Appearance app_marron = new Appearance();
ColoringAttributes marron=new ColoringAttributes();
marron.setColor(0.4f,0.4f,0.2f);
marron.setShadeModel(ColoringAttributes.NICEST);
app_marron.setColoringAttributes(marron);
Panel panel=new Panel();
panel.setLayout(new BorderLayout());panel.setBackground(Color.blue);
panel.setSize(500,500);panel.setLocation(90,40);
pan=new Panel();pan.setLayout(null);
pan.setBackground(Color.yellow);
panel.add(pan);
pan.add(canvas3D);
pan.setSize(300,300);canvas3D.setSize(300,300);
panel.add(scrollver,BorderLayout.EAST);
panel.add(scrollhor,BorderLayout.SOUTH);
scrollver.setMaximum(0);scrollhor.setMaximum(0);
//canvas3D.setLocation((int)((panel.getWidth()-canvas3D.getWidth())/2),(int)((panel.getHeight()-canvas3D.getHeight())/2));
canvas3D.setLocation(pan.getLocation());
canvas3D.setBackground(Color.red);
framepane.add(panel);
//---creation des boutons
dimension=new JButton("Dimens." );dimension.addMouseListener(this);
dimension.setSize(80,30);dimension.setLocation(160,10);
framepane.add(dimension);
zoom_avant=new JButton("zoom (+)" );zoom_avant.addMouseListener(this);
zoom_avant.setSize(85,30);zoom_avant.setLocation(250,10);
framepane.add(zoom_avant);
zoom_arriere=new JButton("zoom (-)" );zoom_arriere.addMouseListener(this);
zoom_arriere.setSize(85,30);zoom_arriere.setLocation(350,10);
framepane.add(zoom_arriere);
defaut=new JButton("par déf." );defaut.addMouseListener(this);
defaut.setSize(85,30);defaut.setLocation(450,10);
framepane.add(defaut);
BranchGroup bg=createSceneGraph2();
simpleU.addBranchGraph(bg);
simpleU.getViewingPlatform().setNominalViewingTransform();
scrollver.addAdjustmentListener(new java.awt.event.AdjustmentListener() {
public void adjustmentValueChanged(AdjustmentEvent e) {
adjustmentValueChangedVer(e);
}
});
scrollhor.addAdjustmentListener(new java.awt.event.AdjustmentListener() {
public void adjustmentValueChanged(AdjustmentEvent e) {
adjustmentValueChangedHor(e);
}
});
this.setSize(500,500);this.setVisible(true);
}
//--------------------------
public void adjustmentValueChangedVer(AdjustmentEvent e) {
int deplx=(anc_val_ver-scrollver.getValue());
canvas3D.setLocation(canvas3D.getX(),canvas3D.getY()+deplx);
Transform3D t=new Transform3D();
Vector3f v=new Vector3f();
tg1.getTransform(t);
t.get(v);
v.y=v.y+2*((float)(scrollver.getValue()-anc_val_ver))/(float)scrollver.getMaximum();
t.set(v);
tg1.setTransform(t);
tg2.getTransform(t);
t.get(v);
v.y=v.y+2*((float)(scrollver.getValue()-anc_val_ver))/(float)scrollver.getMaximum();
t.set(v);
tg2.setTransform(t);
tg3.getTransform(t);
t.get(v);
v.y=v.y+2*((float)(scrollver.getValue()-anc_val_ver))/(float)scrollver.getMaximum();
t.set(v);
tg3.setTransform(t);
tg4.getTransform(t);
t.get(v);
v.y=v.y+2*((float)(scrollver.getValue()-anc_val_ver))/(float)scrollver.getMaximum();
t.set(v);
tg4.setTransform(t);
tg5.getTransform(t);
t.get(v);
v.y=v.y+2*((float)(scrollver.getValue()-anc_val_ver))/(float)scrollver.getMaximum();
t.set(v);
tg5.setTransform(t);
canvas3D.repaint();
anc_val_ver=scrollver.getValue();
}
//-----------------------------------
public void adjustmentValueChangedHor(AdjustmentEvent e) {
Transform3D t=new Transform3D();
Vector3f v=new Vector3f();
t.get(v);
int deply=(anc_val_hor-scrollhor.getValue());
canvas3D.setLocation(canvas3D.getX()+deply,canvas3D.getY());
tg1.getTransform(t);
t.get(v);
v.x=v.x+2*((float)(anc_val_hor-scrollhor.getValue()))/(float)scrollhor.getMaximum();
t.set(v);
tg1.setTransform(t);
tg2.getTransform(t);
t.get(v);
v.x=v.x+2*((float)(anc_val_hor-scrollhor.getValue()))/(float)scrollhor.getMaximum();
t.set(v);
tg2.setTransform(t);
tg3.getTransform(t);
t.get(v);
v.x=v.x+2*((float)(anc_val_hor-scrollhor.getValue()))/(float)scrollhor.getMaximum();
t.set(v);
tg3.setTransform(t);
tg4.getTransform(t);
t.get(v);
v.x=v.x+2*((float)(anc_val_hor-scrollhor.getValue()))/(float)scrollhor.getMaximum();
t.set(v);
tg4.setTransform(t);
tg5.getTransform(t);
t.get(v);
v.x=v.x+2*((float)(anc_val_hor-scrollhor.getValue()))/(float)scrollhor.getMaximum();
t.set(v);
tg5.setTransform(t);
anc_val_hor=scrollhor.getValue();
canvas3D.repaint();
}
//---------reponses aux évenements de la souris
public void mouseClicked(MouseEvent e){
Transform3D translate2=new Transform3D();
if (e.getSource()==zoom_avant) {
scrollver.setValue(0);scrollhor.setValue(0);
adjustmentValueChangedVer(null);adjustmentValueChangedHor(null);
canvas3D.setLocation(pan.getLocation());
Transform3D t=new Transform3D();
Vector3f v=new Vector3f();
if (canvas3D.getSize().getHeight()*2==1200/*&&canvas3D.getSize().getHeight()*2<=2048*/){
tg1.getTransform(t);
t.get(v);v.y+=-1;
t.set(v);tg1.setTransform(t);
tg2.getTransform(t);
t.get(v);v.y+=-1;
t.set(v);tg2.setTransform(t);
tg3.getTransform(t);
t.get(v);v.y+=-1;
t.set(v);tg3.setTransform(t);
tg4.getTransform(t);
t.get(v);v.y+=-1;
t.set(v);tg4.setTransform(t);
tg5.getTransform(t);
t.get(v);v.y+=-1;
t.set(v);tg5.setTransform(t);
}
else
if (/*canvas3D.getSize().getHeight()*2>=1200&&*/canvas3D.getSize().getHeight()*2>=2400/*2048*/){
tg1.getTransform(t);
t.get(v);v.y+=-0.4;//+2*v.y;
t.set(v);tg1.setTransform(t);
tg2.getTransform(t);
t.get(v);v.y+=-0.4;//+2*v.y;
t.set(v);tg2.setTransform(t);
tg3.getTransform(t);
t.get(v);v.y+=-0.4;//+2*v.y;
t.set(v);tg3.setTransform(t);
tg4.getTransform(t);
t.get(v);v.y+=-0.4;//+2*v.y;
t.set(v);tg4.setTransform(t);
tg5.getTransform(t);
t.get(v);v.y+=-0.4;//+2*v.y;
t.set(v);tg5.setTransform(t);
}
canvas3D.setSize((int)(canvas3D.getWidth()*2f),(int)(canvas3D.getHeight()*2f));
}
if (e.getSource()==zoom_arriere) {
scrollver.setValue(0/*scrollver.getMaximum()*/);scrollhor.setValue(0/*scrollhor.getMaximum()*/);//rafraichir();
adjustmentValueChangedVer(null);adjustmentValueChangedHor(null);
canvas3D.setLocation(pan.getLocation());
Transform3D t=new Transform3D();//la sphere a gauche
Vector3f v=new Vector3f();
if (canvas3D.getSize().getHeight()>300){
if (canvas3D.getSize().getHeight()>=2400/*>=2048*/){
tg1.getTransform(t);
t.get(v);
v.y+=0.4f;t.set(v);tg1.setTransform(t);
tg2.getTransform(t);
t.get(v);
v.y+=0.4f;t.set(v);tg2.setTransform(t);
tg3.getTransform(t);
t.get(v);
v.y+=0.4f;t.set(v);tg3.setTransform(t);
tg4.getTransform(t);
t.get(v);
v.y+=0.4f;t.set(v);tg4.setTransform(t);
tg5.getTransform(t);
t.get(v);
v.y+=0.4f;t.set(v);tg5.setTransform(t);
canvas3D.setSize(1200, 1200);
}
else
if (canvas3D.getSize().getHeight()==1200){
tg1.getTransform(t);
t.get(v);
v.y+=1f;t.set(v);tg1.setTransform(t);
tg2.getTransform(t);
t.get(v);
v.y+=1f;t.set(v);tg2.setTransform(t);
tg3.getTransform(t);
t.get(v);
v.y+=1f;t.set(v);tg3.setTransform(t);
tg4.getTransform(t);
t.get(v);
v.y+=1f;t.set(v);tg4.setTransform(t);
tg5.getTransform(t);
t.get(v);
v.y+=1f;t.set(v);tg5.setTransform(t);
canvas3D.setSize((int)(canvas3D.getWidth()/2f),(int)(canvas3D.getHeight()/2f));
}
else
canvas3D.setSize((int)(canvas3D.getWidth()/2f),(int)(canvas3D.getHeight()/2f));
}
}
if (e.getSource()==dimension) {
JOptionPane.showMessageDialog(null,"dimension de canvas3D="+canvas3D.getSize());
}
if (e.getSource()==defaut) {
canvas3D.setSize(300,300);
Transform3D t1=new Transform3D();t1.setTranslation(new Vector3f(-1.0f,0.0f,0f));
tg1.setTransform(t1);
Transform3D t2=new Transform3D();t2.setTranslation(new Vector3f(0.0f,1.0f,0f));
tg2.setTransform(t2);
tg3.setTransform(new Transform3D());
Transform3D t4=new Transform3D();t4.setTranslation(new Vector3f(0.0f,-1.0f,0f));
tg4.setTransform(t4);
Transform3D t5=new Transform3D();t5.setTranslation(new Vector3f(1.0f,0.0f,0f));
tg5.setTransform(t5);
canvas3D.setLocation(pan.getLocation());
}
rafraichir();
}
//-------------------------------------
public void rafraichir(){
if(canvas3D.getWidth()-pan.getWidth()>0){
scrollhor.setMaximum(canvas3D.getWidth());
scrollhor.setValue(0);
scrollhor.setUnitIncrement(scrollhor.getMaximum()/12);
}
else
scrollhor.setMaximum(0);
if(canvas3D.getHeight()-pan.getHeight()>0){
scrollver.setMaximum(canvas3D.getHeight());
scrollver.setValue(0);
scrollver.setUnitIncrement(scrollver.getMaximum()/12);
}
else
scrollver.setMaximum(0);
anc_val_ver=scrollver.getValue();
anc_val_hor=scrollhor.getValue();
canvas3D.repaint();
}
public void mousePressed(MouseEvent e){}
public void mouseReleased(MouseEvent e){canvas3D.repaint() ;}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mouseDragged(MouseEvent e){
canvas3D.repaint() ;
}
public void mouseMoved(MouseEvent e){}
//ajouter des spheres a notre univers
public BranchGroup createSceneGraph2() {
Transform3D translate1 = new Transform3D();
translate1.set(new Vector3f(-1.0f,0.0f,0f));
tg1.setTransform(translate1) ;
Transform3D translate3 = new Transform3D();
translate3.set(new Vector3f(0f,1f,0f));
tg2.setTransform(translate3) ;
Transform3D translate2 = new Transform3D();
translate2.set(new Vector3f(0,0,0));
tg3.setTransform(translate2) ;
Transform3D translate4 = new Transform3D();
translate4.set(new Vector3f(0.0f,-1f,0.0f));
tg4.setTransform(translate4) ;
Transform3D translate5 = new Transform3D();
translate5.set(new Vector3f(1f,0f,0.0f));
tg5.setTransform(translate5) ;
//on crée le Bg principal
BranchGroup objRoot=new BranchGroup();
// on crée une apparence de couleur blanche
Appearance app_blanc = new Appearance();
ColoringAttributes blanc=new ColoringAttributes();
blanc.setColor(1.0f,1.0f,1.0f);
// on crée une apparence de couleur verte
ColoringAttributes vert=new ColoringAttributes();
vert.setColor(0.1f,1.0f,0.1f);
vert.setShadeModel(ColoringAttributes.NICEST);
app_vert.setColoringAttributes(vert);
// on crée une apparence de couleur marron
// Appearance app_marron = new Appearance();
ColoringAttributes marron=new ColoringAttributes();
marron.setColor(0.4f,0.4f,0.4f);
marron.setShadeModel(ColoringAttributes.NICEST);
app_marron.setColoringAttributes(marron);
// on précise le rendu
blanc.setShadeModel(ColoringAttributes.NICEST);
app_blanc.setColoringAttributes(blanc);
tg1.addChild(new Sphere(0.3f,Primitive.GENERATE_NORMALS,256,app_blanc));
tg2.addChild(new Sphere(0.3f,Primitive.GENERATE_NORMALS,256,app_vert));
tg3.addChild(new Sphere(0.3f,Primitive.GENERATE_NORMALS,256,app_marron));
tg4.addChild(new Sphere(0.3f,Primitive.GENERATE_NORMALS,256,app_vert));
tg5.addChild(new Sphere(0.3f,Primitive.GENERATE_NORMALS,256,app_blanc));
tg1.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
tg1.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
tg2.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
tg2.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
tg3.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
tg3.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
tg4.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
tg4.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
tg5.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
tg5.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
objRoot.setCapability(BranchGroup.ENABLE_PICK_REPORTING);
objRoot.addChild(tg1);objRoot.addChild(tg2);objRoot.addChild(tg3);
objRoot.addChild(tg4);objRoot.addChild(tg5);
return objRoot;
}
//-------methode main
public static void main(String args[]){
Zoom myApp=new Zoom();
}
}