IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Graphisme Java Discussion :

Animation de Sprites .PNG: Démo ?


Sujet :

Graphisme Java

  1. #1
    Membre confirmé Avatar de broumbroum
    Profil pro
    Inscrit en
    Août 2006
    Messages
    406
    Détails du profil
    Informations personnelles :
    Âge : 40
    Localisation : Suisse

    Informations forums :
    Inscription : Août 2006
    Messages : 406
    Points : 465
    Points
    465
    Par défaut Animation de Sprites .PNG: Démo ?
    Bonjour!

    J'arrive au point crucial de mon package: une démo. Alors je m'empresse de taper le code selon le schéma que je pense avoir déjà testé assez, mais l'image ne s'affiche plus. Je lance une fenêtre avec un gestionnaire de doublebuffering hardware comme il y a déjà sur le site et ça tournait pas trop mal sur les premières applets puis plus vraiment. Voici le code:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    public class AnimationDemo3{
        JFrame frame;
        AffineTransform tx;
        Animation anim;
        SpritesChar character = SpritesChar.characters.get("Ryu");
        VolatileImage offscreen;
        static int ticktime = 10;
     
        private MediaTracker mt;
        private Dimension size = new Dimension(400,340);
     
        /** Creates a new instance of Main */
        public AnimationDemo3() {
            frame = new JFrame("Animation Demo - sf3jswing project @sf.net");
    		frame.setLocationRelativeTo(null);
    		frame.setIgnoreRepaint(true);
    		frame.setPreferredSize(size); 
    		frame.setSize(size);
    		frame.setVisible(true);
    		frame.pack();
            tx = AffineTransform.getScaleInstance(1.0,1.0);
            offscreen = sf3.Sprite.createVolatileImage(Toolkit.getDefaultToolkit().createImage(""), size);
            System.out.println(getClass().getResource("Main.class"));
            try {
                anim = new Animation("ryu", character.frames[0][0], character.frames[0][1], character.prefix, character.suffix + ".png", "png", new Dimension(character.spriteWidth,character.spriteWidth), mt = new MediaTracker(frame));
                anim.setResourceEnabled(true);
    			anim.setSwapDiskCacheEnabled(true);
                anim.waitForAllActivity(anim.cacheResources());
    		} catch(URISyntaxException e) { e.printStackTrace(); } finally {
    			frame.createBufferStrategy(2); 
    			frame.getBufferStrategy().show();
                new javax.swing.Timer(ticktime, new ActionListener() { public void actionPerformed(ActionEvent e) {
                    Graphics2D g2 = (Graphics2D)frame.getBufferStrategy().getDrawGraphics();
                    if(anim.status() != Animation.PLAYING) { anim.rewind(); anim.play(); }   
    				/* Graphics2D offG = offscreen.createGraphics();
                    offG.drawRect(0,0,size.width, size.height);
    				offG.drawString("ANIMATIONS GRAPHICAL DISPLAY", 50, 50);
                    anim.draw(offG);
                    g2.drawImage(offscreen, tx, frame);*/
    				anim.draw(g2); g2.drawString("ANIMATIONS GRAPHICAL DISPLAY", 50, 50);
    				frame.getBufferStrategy().show(); // <- ici c buggé
    				//offG.dispose();
    				g2.dispose();
                }}).start();
            }
       /**
                    * @param args the command line arguments
         */
        public static void main(String[] args) {
            // TODO code application logic here
            new AnimationDemo3();
        }
        }
    Je reçois une exception IllegalStateException pour la fenetre "frame" lorsque j'appelle la "stratégie de buffer" , qui peut m'aider?

  2. #2
    Gfx
    Gfx est déconnecté
    Expert éminent
    Avatar de Gfx
    Inscrit en
    Mai 2005
    Messages
    1 770
    Détails du profil
    Informations personnelles :
    Âge : 42

    Informations forums :
    Inscription : Mai 2005
    Messages : 1 770
    Points : 8 178
    Points
    8 178
    Par défaut
    As-tu réellement besoin d'implémenter une stratégie de double-buffering hardware ? Swing étant déjà double-buffered en interne tu peux t'en sortir sans. Il n'y a guère que le mode plein écran qui nécessite une telle technique.

  3. #3
    Membre confirmé Avatar de broumbroum
    Profil pro
    Inscrit en
    Août 2006
    Messages
    406
    Détails du profil
    Informations personnelles :
    Âge : 40
    Localisation : Suisse

    Informations forums :
    Inscription : Août 2006
    Messages : 406
    Points : 465
    Points
    465
    Par défaut
    merci! Mais en fait c'est pas assez rapide. J'ai une app qui tourne 2 à 10 x plus vite avec ce mode. en fenetré. Je me demandais si l'OpenGL apportait rééllement un plus.

  4. #4
    Gfx
    Gfx est déconnecté
    Expert éminent
    Avatar de Gfx
    Inscrit en
    Mai 2005
    Messages
    1 770
    Détails du profil
    Informations personnelles :
    Âge : 42

    Informations forums :
    Inscription : Mai 2005
    Messages : 1 770
    Points : 8 178
    Points
    8 178
    Par défaut
    Quel est le rapport avec OpenGL ?

    D'ailleurs, ton implémentation du double-buffering est faussée. Tout d'abord tu repeins ton buffer à chaque fois alors que ce n'est pas forcément nécessaire (sauf bien entendu si à chaque tick du timer tu modifies le monde du jeu). Il faut en outre constamment vérifier si la volatile image a été perdue.

  5. #5
    Membre confirmé Avatar de broumbroum
    Profil pro
    Inscrit en
    Août 2006
    Messages
    406
    Détails du profil
    Informations personnelles :
    Âge : 40
    Localisation : Suisse

    Informations forums :
    Inscription : Août 2006
    Messages : 406
    Points : 465
    Points
    465
    Par défaut demo
    Eh ben finalement ça marche. AnimationDemo3.jar
    le code est comme ça: avec la modif que Gfx m'a conseillé pour l'image Volatile
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    /*
     * Main.java
     *
     * Created on 15 novembre 2006, 04:32
     *
     * To change this template, choose Tools | Template Manager
     * and open the template in the editor.
     */
     
    import java.awt.Dimension;
    import java.awt.Graphics2D;
    import java.awt.Image;
    import java.awt.MediaTracker;
    import java.awt.Toolkit;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.geom.AffineTransform;
    import java.awt.image.VolatileImage;
    import java.awt.GraphicsConfiguration;
    import java.net.URISyntaxException;
    import javax.swing.JFrame;
    import quicktime.std.anim.Sprite;
    import quicktime.std.clocks.TimeRecord;
    import sf3.*;
     
    /**
     *
     * @author website
     */
    public class AnimationDemo3{
        JFrame frame;
        AffineTransform tx;
        Animation anim;
        SpritesChar character = SpritesChar.characters.get("Ryu");
        VolatileImage offscreen;
        static int ticktime = 10;
     
        private MediaTracker mt;
        private Dimension size = new Dimension(400,340);
     
        /** Creates a new instance of Main */
        public AnimationDemo3() {
            frame = new JFrame("Animation Demo - sf3jswing project @sf.net");
            //frame.setLocationRelativeTo(null);
            frame.setIgnoreRepaint(true);
            frame.setPreferredSize(size);
            frame.setSize(size);
            frame.setVisible(true);
            frame.pack();
            tx = AffineTransform.getScaleInstance(1.0,1.0);
            offscreen = createVolatile(size);
            System.out.println(getClass().getResource("Main.class"));
            try {
                anim = new Animation("ryu", character.frames[0][0], character.frames[0][1], character.prefix, character.suffix + ".png", "png", new Dimension(character.spriteWidth,character.spriteWidth), mt = new MediaTracker(frame));
                anim.setResourceEnabled(true);
                anim.setSwapDiskCacheEnabled(true);
                anim.waitForAllActivity(anim.cacheResources());
            } catch(URISyntaxException e) { e.printStackTrace(); } finally {
                frame.createBufferStrategy(2);
                frame.getBufferStrategy().show();
                new javax.swing.Timer(ticktime, new ActionListener() { public void actionPerformed(ActionEvent e) {
                    Graphics2D g2 = (Graphics2D)frame.getBufferStrategy().getDrawGraphics();
                    if(anim.status() != Animation.PLAYING) { anim.rewind(); anim.play(); }
                    Graphics2D offG = (offscreen.contentsLost())?(offscreen = createVolatile(size)).createGraphics():offscreen.createGraphics();
                    offG.drawRect(0,0,size.width, size.height);
                    offG.drawString("ANIMATIONS GRAPHICAL DISPLAY", 50, 50);
                    anim.draw(offG);
                    g2.drawImage(offscreen, tx, frame);
                    //anim.draw(g2); g2.drawString("ANIMATIONS GRAPHICAL DISPLAY", 50, 50); // no offscreen
                    frame.getBufferStrategy().show();
                    //offG.dispose();
                    //g2.dispose();
                }}).start();
            }
        }
     
        public VolatileImage createVolatile(Dimension size) {
            return sf3.Sprite.createVolatileImage((Image)frame.getGraphicsConfiguration().createCompatibleImage(size.width, size.height), size);
        }
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            // TODO code application logic here
            AnimationDemo3 main = new AnimationDemo3();
        }
     
    }

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Animation de sprites : temps fixe ou variable ?
    Par Julien Bodin dans le forum Développement 2D, 3D et Jeux
    Réponses: 5
    Dernier message: 12/05/2009, 17h44
  2. Créer une animation de sprite sous Eclipse
    Par HekThor dans le forum Développement 2D, 3D et Jeux
    Réponses: 2
    Dernier message: 24/10/2008, 19h30
  3. Réponses: 5
    Dernier message: 13/08/2007, 16h06
  4. Problème pour animer un sprite
    Par Soward dans le forum SDL
    Réponses: 17
    Dernier message: 02/08/2007, 16h30
  5. [ Question ] Animer des sprites 3D
    Par Dam)rpgheaven dans le forum OpenGL
    Réponses: 2
    Dernier message: 19/07/2004, 16h14

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo