Forum |  HardWare.fr | News | Articles | PC | S'identifier | S'inscrire | Shop Recherche
1342 connectés 

  FORUM HardWare.fr
  Programmation

  [Java] - Double buffering ?

 


 Mot :   Pseudo :  
 
Bas de page
Auteur Sujet :

[Java] - Double buffering ?

n°97293
Cherrytree
cn=?
Posté le 06-02-2002 à 13:00:15  profilanswer
 

Bon, je me suis mis un peu sérieusement à mon projet de jeu vidéo et pour ça, j'ai mis la main sur le Black Art of Java Game Programming (merci à JeromeV au passage). Bien, je compile mon premier exemple pour voir ce qu'ilk donne, ça clippe à mort, je modifie le code comme ils semblent vouloir me faire faire et j'obtiens un truc qui marche pas. C'est sensé être un rectangle qui se balade au milieu de l'écran, entouré par d'autres rectangles qui ne bougent pas. Le code source est le suivant, et je ne vois rien d'incorrect.
 

import java.applet.*;
import java.awt.*;
 
public class Broadway extends Applet implements Runnable {
     
    Thread animation;
    int locx,locy;          // location of rectangle
    int width, height;      // dimensions of rectangle
    static final byte UP = 0; // direction of motion
    static final byte DOWN = 1;
    static final byte LEFT = 2;
    static final byte RIGHT = 3;
     
    byte state;                // state the rect is in
    // length of pausing interval
    static final int REFRESH_RATE = 100;    // in ms
     
    Graphics offscreen;    // Declaration of offscreen buffer
    Image image;
    // applet methods:
     
    public void init() {
         
        System.out.println(">> init <<" );
        setBackground(Color.black);
        locx = 80;             // parameters of center rect
        locy = 100;
        width = 110;
        height = 90;
        state = UP;
        image = createImage(width,height); // allocation of offscreen
        offscreen = image.getGraphics();   //               buffer
         
    }
     
    public void start() {
         
        System.out.println(">> start <<" );
        animation = new Thread(this);
        if (animation != null) {
            animation.start();
        }
    }
     
    public void paint(Graphics g) {
        System.out.println(">> paint <<" );
         
        offscreen.setColor(Color.black);
        offscreen.fillRect(0,0,300,300);  // clear buffer
        offscreen.setColor(Color.yellow);
        offscreen.fillRect(0,0,90,90);
        offscreen.fillRect(250,0,40,190);
        offscreen.fillRect(80,110,100,20);
         
        /*g.setColor(Color.yellow);
        g.fillRect(0,0,90,90);
        g.fillRect(250,0,40,190);
        g.fillRect(80,110,100,20); // hidden rectangle
         
        g.setColor(Color.blue);
        g.fillRect(80,200,220,90);
        g.fillRect(100,10,90,80);
         
        g.setColor(Color.lightGray);
        g.fillRect(locx,locy,width,height);
         
        g.setColor(Color.red);
        g.fillRect(200,0,45,45);
        g.fillRect(0,100,70,200);
         
        g.setColor(Color.magenta);
        g.fillRect(200,55,60,135);*/
         
        //offscreen.fillRect(80,110,100,20); // hidden rectangle
         
        offscreen.setColor(Color.blue);
        offscreen.fillRect(80,200,220,90);
        offscreen.fillRect(100,10,90,80);
         
        offscreen.setColor(Color.lightGray);
        offscreen.fillRect(locx,locy,width,height);
         
        offscreen.setColor(Color.red);
        offscreen.fillRect(200,0,45,45);
        offscreen.fillRect(0,100,70,200);
         
        offscreen.setColor(Color.magenta);
        offscreen.fillRect(200,55,60,135);
        g.drawImage(image,0,0,this);      // draw offscreen buffer to screen
         
    }
     
    public void update(Graphics g) {
        paint(g);
    }
     
     
     
    // update the center rectangle
    void updateRectangle() {
        switch (state) {
            case DOWN:
                locy += 2;
                if (locy >= 110) {
                    state = UP;
                }
                break;
            case UP:
                locy -= 2;
                if (locy <= 90) {
                    state = RIGHT;
                }
                break;
            case RIGHT:
                locx += 2;
                if (locx >= 90) {
                    state = LEFT;
                }
                break;
            case LEFT:
                locx -= 2;
                if (locx <= 70) {
                    state = DOWN;
                }
                break;
                 
        }
         
    }
     
    public void run() {
        while (true) {
            repaint();
            updateRectangle();
            try {
                Thread.sleep (REFRESH_RATE);
            } catch (Exception exc) { };
        }
    }
     
    public void stop() {
         
        System.out.println(">> stop <<" );
        if (animation != null) {
            animation.stop();
            animation = null;
        }
    }
     
}

 
Voilà si qqn a une idée qui pourrait me sauver du double buffering qui bufferize que dalle, je prends !

mood
Publicité
Posté le 06-02-2002 à 13:00:15  profilanswer
 

n°97346
Cherrytree
cn=?
Posté le 06-02-2002 à 14:37:01  profilanswer
 

Bon, ben après quelques heures de recherches à droite à gauche, j'ai trouvé le truc qui collait pas. J'avais absolument pas vu que mon buffer offscreen ne concernait qu'une partie de mon image : globalement le quart nord-ouest. La solution au problème était donc :  

import java.applet.*;  
import java.awt.*;  
 
public class Broadway extends Applet implements Runnable {  
   
   Thread animation;  
   int locx,locy;
   int width, height;
   static final byte UP = 0;  
   static final byte DOWN = 1;  
   static final byte LEFT = 2;  
   static final byte RIGHT = 3;  
     
   byte state;
   static final int REFRESH_RATE = 100;
     
   Graphics offscreen;
   Image image;
     
   public void init() {  
         
       System.out.println(">> init <<" );  
       setBackground(Color.black);  
       locx = 80;
       locy = 100;  
       width = 110;  
       height = 90;  
       state = UP;  
       image = createImage(
300,300);
       offscreen = image.getGraphics();
   }  
     
   public void start() {  
       System.out.println(">> start <<" );  
       animation = new Thread(this);  
       if (animation != null) {  
           animation.start();  
       }  
   }  
     
   public void paint(Graphics g) {  
       System.out.println(">> paint <<" );  
         
       offscreen.setColor(Color.black);  
       offscreen.fillRect(0,0,300,300);
       offscreen.setColor(Color.yellow);  
       offscreen.fillRect(0,0,90,90);  
       offscreen.fillRect(250,0,40,190);  
       offscreen.fillRect(80,110,100,20);  
         
       offscreen.setColor(Color.blue);  
       offscreen.fillRect(80,200,220,90);  
       offscreen.fillRect(100,10,90,80);  
         
       offscreen.setColor(Color.lightGray);  
       offscreen.fillRect(locx,locy,width,height);  
         
       offscreen.setColor(Color.red);  
       offscreen.fillRect(200,0,45,45);  
       offscreen.fillRect(0,100,70,200);  
         
       offscreen.setColor(Color.magenta);  
       offscreen.fillRect(200,55,60,135);  
       g.drawImage(image,0,0,this);      // draw offscreen buffer to screen  
         
   }  
     
   public void update(Graphics g) {  
       paint(g);  
   }  
     
     
     
   // update the center rectangle  
   void updateRectangle() {  
       switch (state) {  
           case DOWN:  
               locy += 2;  
               if (locy >= 110) {  
                   state = UP;  
               }  
               break;  
           case UP:  
               locy -= 2;  
               if (locy <= 90) {  
                   state = RIGHT;  
               }  
               break;  
           case RIGHT:  
               locx += 2;  
               if (locx >= 90) {  
                   state = LEFT;  
               }  
               break;  
           case LEFT:  
               locx -= 2;  
               if (locx <= 70) {  
                   state = DOWN;  
               }  
               break;  
                 
       }  
         
   }  
     
   public void run() {  
       while (true) {  
           repaint();  
           updateRectangle();  
           try {  
               Thread.sleep (REFRESH_RATE);  
           } catch (Exception exc) { };  
       }  
   }  
     
   public void stop() {  
         
       System.out.println(">> stop <<" );  
       if (animation != null) {  
           animation.stop();  
           animation = null;  
       }  
   }  
     
}  

 
Comme ça tout le monde en profite [:cherrytree]

 

[edtdd]--Message édité par Cherrytree--[/edtdd]

n°97361
JeromeV
On en a gros!
Posté le 06-02-2002 à 14:44:08  profilanswer
 

Pour faire propre, recupere plutot la taille de l'applet avec  
this.getSize().width et this.getSize().height

n°97417
Cherrytree
cn=?
Posté le 06-02-2002 à 16:07:12  profilanswer
 

Très juste.


Aller à :
Ajouter une réponse
  FORUM HardWare.fr
  Programmation

  [Java] - Double buffering ?

 

Sujets relatifs
2D sous JAVA[Java] Liste des fonctions et méthodes
Java -> JavascriptDémineur en java avec swing
[JAVA]: Lever une exception d'un constructeur?[JAVA + SQL] installation...
[java] un moteur de recherche par methodes ca vous interesse?[Java] applet Vs Application
[Java] Comment faire un multi thread[Java] Comment fait-on du double buffering en Graphics2D ?
Plus de sujets relatifs à : [Java] - Double buffering ?


Copyright © 1997-2022 Hardware.fr SARL (Signaler un contenu illicite / Données personnelles) / Groupe LDLC / Shop HFR