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

avec Java Discussion :

Frogger en java


Sujet :

avec Java

  1. #1
    Membre à l'essai
    Profil pro
    Inscrit en
    Mars 2010
    Messages
    35
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2010
    Messages : 35
    Points : 11
    Points
    11
    Par défaut Frogger en java
    Bonjour,
    Mon professeur de TP veut que l'on test le code suivant:
    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
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    import java.util.*;
    import java.io.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.Image.*;
    import javax.swing.*;
     
    public class TimerTestGood
    {
        public static void main(String args[])throws IOException
     
        {
         Windows myW=new Windows();
        myW.setSize(800,600);
     
            myW.addWindowListener(new WindowAdapter()
     
            {
     
                public void windowCLosing(WindowEvent event)
     
                {
     
                    System.exit(0);
     
                }
     
            });
            myW.show();
    }
     
    }
    class Windows extends Frame implements KeyListener
     
    {
        private int x, y, xold, yold, deltaX1, deltaX2, deltaX3, raftX1, raftX2, raftX3;
     
        private boolean go, move, firsttime, play, playTime, frog;
     
        private Image dbImage;
     
        private Graphics dbg;
     
        public Windows()
     
        {
            x = 390;
            deltaX1=0;
            deltaX2=800;
            deltaX3=0;
            raftX1=0;
            raftX2=800;
            raftX3=0;
            y=560;
            xold=x;
            yold=y;
            firsttime=true;
            go=false;
            play=false;
            move=false;
            playTime=false;
            addKeyListener(this);
     
            //frog=true;
        }
     
        public void paint(Graphics g){
            if(firsttime)
                play(g);
            //repaint();
        }
     
        public void update(Graphics g)
        {
            if (dbImage == null) {
                dbImage = createImage (this.getSize().width, this.getSize().height);
                  dbg = dbImage.getGraphics ();
            }
            // clear screen in background
     
            dbg.setColor (getBackground ());
            dbg.fillRect (0, 0, this.getSize().width, this.getSize().height);
            // draw elements in background
            dbg.setColor (getForeground());
            paint (dbg);
            // draw image on the screen
            g.drawImage (dbImage, 0, 0, this);
                //paint(g);
        }
     
        public void start(Graphics g)
        {
            if(play)
                play(g);
            g.setColor(Color.cyan);
            g.fillRect(0,0,800,600);
            g.setColor(Color.black);
            g.setFont(new Font("Courier", Font.BOLD, 40));
            g.drawString("Frogger Game", 200, 60);
            g.setFont(new Font("Courier", Font.BOLD, 20));
            g.drawString("Enter to Play", 100, 80);
            g.drawString("Escape to Exit", 600, 80);
            if(play)
                play(g);
    }
        public void play(Graphics g) {
    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
            g.setColor(Color.blue);
            g.fillRect(0,60,800,215);
            g.setColor(Color.gray);
            g.fillRect(0,275,800,285);
            g.setColor(Color.black);
            g.fillRect(0,560,800,40);
            g.setColor(Color.green);
            g.fillRect(0,0,800,95);
            g.setColor(Color.black);
            g.drawRect(20,465,15,4);
            g.fillRect(20,465,15,4); // FIRST ROW
            g.fillRect(50,465,15,4);
            g.fillRect(80,465,15,4);
            g.fillRect(110,465,15,4);
            g.fillRect(140,465,15,4);
            g.fillRect(170,465,15,4);
            g.fillRect(200,465,15,4);
            g.fillRect(230,465,15,4);
            g.fillRect(260,465,15,4);
            g.fillRect(300,465,15,4);
            g.fillRect(330,465,15,4);
            g.fillRect(360,465,15,4);
            g.fillRect(390,465,15,4);
            g.fillRect(420,465,15,4);
            g.fillRect(450,465,15,4);
            g.fillRect(480,465,15,4);
            g.fillRect(510,465,15,4);
            g.fillRect(540,465,15,4);
            g.fillRect(570,465,15,4);
            g.fillRect(600,465,15,4);
            g.fillRect(630,465,15,4);
            g.fillRect(660,465,15,4);
            g.fillRect(690,465,15,4);
            g.fillRect(720,465,15,4);
            g.fillRect(750,465,15,4);
            g.fillRect(780,465,15,4); // FIRST ROW END
            g.fillRect(20,370,15,4); // SECOND ROW
            g.fillRect(50,370,15,4);
            g.fillRect(80,370,15,4);
            g.fillRect(110,370,15,4);
            g.fillRect(140,370,15,4);
            g.fillRect(170,370,15,4);
            g.fillRect(200,370,15,4);
            g.fillRect(230,370,15,4);
            g.fillRect(260,370,15,4);
            g.fillRect(300,370,15,4);
            g.fillRect(330,370,15,4);
            g.fillRect(360,370,15,4);
            g.fillRect(390,370,15,4);
            g.fillRect(420,370,15,4);
            g.fillRect(450,370,15,4);
            g.fillRect(480,370,15,4);
            g.fillRect(510,370,15,4);
            g.fillRect(540,370,15,4);
            g.fillRect(570,370,15,4);
            g.fillRect(600,370,15,4);
            g.fillRect(630,370,15,4);
            g.fillRect(660,370,15,4);
            g.fillRect(690,370,15,4);
            g.fillRect(720,370,15,4);
            g.fillRect(750,370,15,4);
            g.fillRect(780,370,15,4); // SECOND ROW END
            Image car1=Toolkit.getDefaultToolkit().getImage("C:\\Users\\Andy\\Pictures\\Random\\MOBILE2RIGHT.jpg");
            Image car2=Toolkit.getDefaultToolkit().getImage("C:\\Users\\Andy\\Pictures\\Random\\MOBILE2.jpg");
            Image car3=Toolkit.getDefaultToolkit().getImage("C:\\Users\\Andy\\Pictures\\Random\\MOBILE2RIGHT.jpg");
            Image raft1=Toolkit.getDefaultToolkit().getImage("C:\\Users\\Andy\\Pictures\\Random\\raft.jpg");
            Image raft2=Toolkit.getDefaultToolkit().getImage("C:\\Users\\Andy\\Pictures\\Random\\raft.jpg");
            Image raft3=Toolkit.getDefaultToolkit().getImage("C:\\Users\\Andy\\Pictures\\Random\\raft.jpg");
            g.setColor(Color.gray);
            frog=true;
            while (frog) {
                /////////////////////////////////////////////////////////////////////
            Image holmesWALK=Toolkit.getDefaultToolkit().getImage("C:\\Users\\Andy\\Pictures\\Random\\holmesWALK.jpg");
            g.drawImage(holmesWALK, x, y, this);
                try
                    {
                      int count = 0;
                      int sleep_time = 2;
                      while (true)
                      {
                        Thread.sleep(2);
                        count++;
                        if (count >=2)
                       {
                        g.setColor(Color.gray);
                          if(deltaX1>=800)
                              deltaX1=0;
                          if(deltaX2<=-64)
                              deltaX2=800;
                          if(deltaX3/2>=800)
                              deltaX3=0;
                          //RAFTX's
                          if(raftX1/3>=800)
                              raftX1=0;
                          if(raftX2<=-100)
                              raftX2=800;
                          if(raftX3*2>=800)
                              raftX3=0;
                        g.drawImage(car1, deltaX1, 300, this);
                        g.fillRect(deltaX1, 300 ,4, 50);
                        g.drawImage(car2, deltaX2, 400, this);
                        g.fillRect(deltaX2+64, 400 ,4, 50);
                        g.drawImage(car3, deltaX3/2, 500, this);
                        g.fillRect(deltaX3/2, 500 ,4, 50);
                        //rafts
                        g.setColor(Color.blue);
                        g.drawImage(raft1, raftX1/3, 95, this);
                        g.fillRect(raftX1/3, 95 ,4, 60);
                        g.drawImage(raft2, raftX2, 155, this);
                        g.fillRect(raftX2+100, 155 ,4, 60);
                        g.drawImage(raft3, raftX3*2, 215, this);
                        g.fillRect(raftX3*2, 215 ,4, 60);
                        //rafts end
                        Thread.sleep(sleep_time);
                        repaint();
                        break;
                        }
                        if (count <=5 ) {
                          deltaX1 += 2;
                          deltaX2 -= 2;
                          deltaX3 += 2;
                          raftX1 += 2;
                          raftX2 -= 2;
                          raftX3 +=2;
                        }
                        else if (count >5 && count <=15)
                         deltaX1 += 2;
                        else
                          deltaX1 +=2;
                        repaint();
                      }
                    }
                    catch(Exception e){}
                ////////////////////////////////////////////////////////////////////
            }
                    go=false;
        }
     
        public void keyPressed(KeyEvent event)
        {
            switch(event.getKeyCode())
                {
                    case KeyEvent.VK_LEFT:if(x>=2)
                                            x-=4;
                                        else
                                            x-=0;
                                        move=true;
                                        break;
                    case KeyEvent.VK_RIGHT:if(x<=798)
                                            x+=4;
                                        else
                                            x+=0;
                                        move=true;
                                        break;
        case KeyEvent.VK_UP:if(y<=598)
         y-=4;
         else
          y-=0;
        move=true;
          break;
        case KeyEvent.VK_DOWN:if(y>=2)
                   y+=4;
                  else
                    y+=0;
                    move=true;
                    break;
       case KeyEvent.VK_ENTER:
        play=true;
           break;
       case KeyEvent.VK_ESCAPE:go=false;
          System.exit(0);
          break;
         }
        }
        public void keyReleased(KeyEvent event){}
        public void keyTyped(KeyEvent event){}
        public void exit(Graphics g)
        {
     
            go=false;
        }
    }
    En fait quand je le lance rien ne s'affiche, quelqu'un peut m'aider en le fesant tourner chez lui et en me l'envoyant le dossier project.
    Merci d'avance.

  2. #2
    Expert éminent sénior
    Avatar de tchize_
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Avril 2007
    Messages
    25 481
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 44
    Localisation : Belgique

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Avril 2007
    Messages : 25 481
    Points : 48 806
    Points
    48 806
    Par défaut
    votre code paint est erroné. Le code paint doit dessiner, le plus rapidement possible est rien d'autre. Il est possible qu'il soit appelé de nombreuses fois par secondes. Tant que vous ne sortez pas de la méthode, plus rien ne fonctionne au niveau de l'interface graphique, puisque vous bloquez le Thread de l'EDT.

    Tous les calculs, l'animation etc doivent se faire dans un Thread à part qui, lorsque c'est nécessaire, appel repaint() pour que l'affichage se refasse. Dons dans paint vous ne devez pas avoir
    • de boucle infinie
    • de Thread.sleep
    • d'appel à repaint

  3. #3
    Membre à l'essai
    Profil pro
    Inscrit en
    Mars 2010
    Messages
    35
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2010
    Messages : 35
    Points : 11
    Points
    11
    Par défaut
    ok merci mais comment y remedier, désoler je suis nouveau en informatique, et je maitrise pas trop les thread

  4. #4
    Expert éminent sénior
    Avatar de tchize_
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Avril 2007
    Messages
    25 481
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 44
    Localisation : Belgique

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Avril 2007
    Messages : 25 481
    Points : 48 806
    Points
    48 806

Discussions similaires

  1. le Java est la continuité du C++ ???
    Par Vincent PETIT dans le forum Débats sur le développement - Le Best Of
    Réponses: 33
    Dernier message: 25/08/2005, 20h17
  2. Envoi de Datagrames par IP en JAVA
    Par the java lover dans le forum Développement
    Réponses: 2
    Dernier message: 14/08/2002, 11h44
  3. directx et java?? possible??
    Par jiraiya dans le forum DirectX
    Réponses: 3
    Dernier message: 09/07/2002, 19h55
  4. webcam : lire sur un port usb en c/c++ ou java. sous win. ?
    Par flo007 dans le forum Choisir un environnement de développement
    Réponses: 2
    Dernier message: 24/05/2002, 23h24
  5. Service de nommage java C++
    Par Anonymous dans le forum CORBA
    Réponses: 3
    Dernier message: 15/04/2002, 12h48

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