Page 1 of 2 12 LastLast
Results 1 to 20 of 24

Thread: Help!

  1. #1
    rowanabejjani is offline Member
    Join Date
    Jun 2011
    Posts
    19
    Rep Power
    0

    Default Help!

    ProjetFinal.zip - 4shared.com - online file sharing and storage - download
    Please can someone see my project i have error and i couldn't solve it with debugging, my presentation is tomorrow, the error is in package snake ->file Fenetre.java -> ligne 135

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,874
    Rep Power
    25

    Default

    Please only post one copy of your question. I see many many copies.
    I've tried to help on one of the threads.
    What was the results of that?
    What about the questions I asked about the code???

    i have a problem

    thread problem

  3. #3
    rowanabejjani is offline Member
    Join Date
    Jun 2011
    Posts
    19
    Rep Power
    0

    Default

    Norm i'm sorry but i'm so depressed no one is replying i debug my code many times and i found where the error is but i don't know how to fix it :S

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,874
    Rep Power
    25

    Default

    Where does the code "stop" executing? Can you post the contents of the console showing the printouts
    and then the lines of code where the last print out occured.

  5. #5
    rowanabejjani is offline Member
    Join Date
    Jun 2011
    Posts
    19
    Rep Power
    0

    Default

    I marked it on red when it arrive at "start method", it start looping and the s.o.p is always start, start, start....

    package snake;

    /*
    * To change this template, choose Tools | Templates
    * and open the template in the editor.
    */

    /*
    * Fenetre.java
    *
    * Created on May 30, 2011, 6:27:25 PM
    */


    /**
    *
    * @author Hp
    */
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Font;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    import java.util.Random;
    import javax.swing.BorderFactory;
    import javax.swing.ButtonGroup;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JMenu;
    import javax.swing.JMenuBar;
    import javax.swing.JMenuItem;
    import javax.swing.JOptionPane;
    import javax.swing.JRadioButtonMenuItem;
    import javax.swing.KeyStroke;


    public class Fenetre extends JFrame implements Runnable {
    private Carré Objectif=new Carré(0,0,20);
    private Serpent Snake=new Serpent('X');
    private PlateauJeu PlateauJeu1;

    private JLabel LabelScore=new JLabel("SCORE: 0");
    private JMenuBar MenuPrincipale=new JMenuBar();
    private JMenu MenuFichier=new JMenu("Fichier"),
    MenuAide=new JMenu("Aide"),
    Vitesse=new JMenu("Vitesse");// , encore Jmenu

    private JMenuItem NouveauJeu=new JMenuItem("Nouveau Jeu"),
    Pause=new JMenuItem("Pause"),
    Quiter=new JMenuItem("Quiter"),
    Control=new JMenuItem("Controle"),
    Apropos=new JMenuItem("À propos?");

    private JOptionPane Information=new JOptionPane();
    private JRadioButtonMenuItem TLent=new JRadioButtonMenuItem("Trés lente"),
    Lent=new JRadioButtonMenuItem("Lente"),
    Moyenne=new JRadioButtonMenuItem("Moyenne"),
    Rapide=new JRadioButtonMenuItem("Rapide"),
    TRapide=new JRadioButtonMenuItem("Trés rapide");

    private int vitesse=200,Score=0;

    private boolean pause=false; // true or false

    char direction;

    public Fenetre(){
    this.setTitle("Snake");
    this.setLocation(200,30);
    this.setSize(630,700);
    this.setResizable(false);
    this.setVisible(true);

    Menu();//

    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );
    RandObjectif();//?
    PlateauJeu1=new PlateauJeu(Snake,Objectif);
    this.add(PlateauJeu1,BorderLayout.CENTER);
    LabelScore.setBorder(BorderFactory.createBevelBord er(1));
    LabelScore.setFont(new Font("Arial",Font.BOLD,20));
    LabelScore.setForeground(Color.PINK);
    this.add(LabelScore,BorderLayout.SOUTH);
    this.addKeyListener(new KeyListener() {


    public void keyTyped(KeyEvent arg0) {
    // TODO Auto-generated method stub
    }


    public void keyReleased(KeyEvent arg0) {
    // TODO Auto-generated method stub
    }

    public void keyPressed(KeyEvent arg0) {
    // TODO Auto-generated method stub
    if(Snake.getDirection()!='x')
    {if(arg0.getKeyCode()==38&&Snake.getDirection()!=' B')
    {Snake.setDirection('H');Jouer(0);}
    if(arg0.getKeyCode()==40&&Snake.getDirection()!='H ')
    {Snake.setDirection('B');Jouer(0);}
    if(arg0.getKeyCode()==39&&Snake.getDirection()!='G ')
    {Snake.setDirection('D');Jouer(0);}
    if(arg0.getKeyCode()==37&&Snake.getDirection()!='D '&&Snake.getDirection()!='X')
    {Snake.setDirection('G');Jouer(0);}
    }
    }
    });
    System.out.println("snake");
    //throw new UnsupportedOperationException("Not supported yet.");

    Start();
    }

    public void RandObjectif(){
    Random Rand=new Random();
    int IndexX=Rand.nextInt(28);
    int IndexY=Rand.nextInt(28);
    Objectif.setX(IndexX*20);
    Objectif.setY(IndexY*20);
    for(Carré T:Snake.getSuit())
    if(IndexX*20==T.getX()&&IndexY*20==T.getY()){
    this.RandObjectif();
    }
    if(IndexX*20==Snake.getTete().getX()&&IndexY*20==S nake.getTete().getY()){
    this.RandObjectif();
    }
    System.out.println("ramdom carre");

    }
    public void Start(){
    while(true){
    System.out.println("start");
    Jouer(5);
    try {
    Thread.sleep(200);

    } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }

    }

    @SuppressWarnings("static-access")
    public void Jouer(int a){
    if((Snake.getTete().getX()==Objectif.getX())&&(Sna ke.getTete().getY()==Objectif.getY()))
    {Snake.AjouterCarré();RandObjectif();Score+=10;
    LabelScore.setText("SCORE: "+Score);
    }
    else if(a!=0)Snake.AvancerSerpent();
    for(Carré T:Snake.getSuit())
    if(Snake.getTete().getX()==T.getX()&&Snake.getTete ().getY()==T.getY()){
    System.out.println("GAME OVER");
    Information.showMessageDialog(null,"Game Over\nCliquez sur OK pour recommencer","Information",JOptionPane.INFORMATION _MESSAGE);
    Snake.Réinitialiser();Score=0;LabelScore.setText(" SCORE: 0");break;
    }//eza fetit be hala

    if(Snake.getTete().getX()<0||Snake.getTete().getX( )>20*30-40||Snake.getTete().getY()<0||Snake.getTete().getY ()>20*30-40)
    {System.out.println("GAME OVER");
    Information.showMessageDialog(null,"Game Over\nCliquez sur OK pour recommencer","Information",JOptionPane.INFORMATION _MESSAGE);
    Snake.Réinitialiser();Score=0;LabelScore.setText(" SCORE: 0");}
    //eza fetit bel het
    PlateauJeu1.repaint();

    }
    public void Menu(){
    MenuPrincipale.add(MenuFichier);
    MenuPrincipale.add(MenuAide);
    MenuFichier.add(NouveauJeu);
    MenuFichier.add(Pause);
    Pause.setAccelerator(KeyStroke.getKeyStroke('p'));
    MenuFichier.add(Vitesse);
    MenuFichier.addSeparator();
    MenuFichier.add(Quiter);
    MenuAide.add(Control);
    MenuAide.add(Apropos);
    this.setJMenuBar(MenuPrincipale);
    ButtonGroup BG=new ButtonGroup();
    BG.add(TLent);BG.add(Lent);BG.add(Moyenne);BG.add( Rapide);
    BG.add(TRapide);
    Moyenne.setSelected(true);

    //ba3mil add lal button l khala2toun fo2
    Vitesse.add(TLent);Vitesse.add(Lent);Vitesse.add(M oyenne);
    Vitesse.add(Rapide);Vitesse.add(TRapide);

    TLent.addActionListener(new VitesseListener());
    Lent.addActionListener(new VitesseListener());
    Moyenne.addActionListener(new VitesseListener());
    Rapide.addActionListener(new VitesseListener());
    TRapide.addActionListener(new VitesseListener());
    NouveauJeu.addActionListener(new ActionListener() {

    @Override
    public void actionPerformed(ActionEvent arg0) {
    // TODO Auto-generated method stub
    Score=0;LabelScore.setText("SCORE: 0");
    Snake.Réinitialiser();
    }
    });
    Quiter.addActionListener(new ActionListener() {

    @Override
    public void actionPerformed(ActionEvent arg0) {
    // TODO Auto-generated method stub
    System.exit(0);
    }
    });
    Control.addActionListener(new ActionListener() {

    @SuppressWarnings("static-access")
    @Override
    public void actionPerformed(ActionEvent arg0) {
    // TODO Auto-generated method stub
    Information.showMessageDialog(null,"Haut -> déplacez en haut\nBas -> déplacez en Bas\nDroite -> déplacez à droite\nGauche -> déplacez à gauche\n p -> Pause","Information",JOptionPane.INFORMATION_MESSA GE);
    }
    });
    Apropos.addActionListener(new ActionListener() {

    @SuppressWarnings("static-access")
    @Override
    public void actionPerformed(ActionEvent arg0) {
    // TODO Auto-generated method stub
    Information.showMessageDialog(null,"Réaliser par:\n Rowana Bejjani \n INF101606","Information",JOptionPane.INFORMATION_M ESSAGE);
    }
    });

    Pause.addActionListener(new ActionListener() {

    @Override
    public void actionPerformed(ActionEvent arg0) {
    // TODO Auto-generated method stub
    if(pause){Snake.setDirection(direction);pause=fals e;}
    else{direction=Snake.getDirection();Snake.setDirec tion('x');pause=true;}
    }
    });

    }

    public void run() {
    throw new UnsupportedOperationException("Not supported yet.");
    }
    class VitesseListener implements ActionListener{

    @Override
    public void actionPerformed(ActionEvent arg0) {
    // TODO Auto-generated method stub
    if(arg0.getSource()==TLent) vitesse=1000;
    if(arg0.getSource()==Lent) vitesse=500;
    if(arg0.getSource()==Moyenne) vitesse=200;
    if(arg0.getSource()==Rapide) vitesse=100;
    if(arg0.getSource()==TRapide) vitesse=20;
    }

    }
    public static void main (String[] args) {
    // TODO Auto-generated method stub
    new Fenetre();
    }


    /** This method is called from within the constructor to
    * initialize the form.
    * WARNING: Do NOT modify this code. The content of this method is
    * always regenerated by the Form Editor.
    */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

    setDefaultCloseOperation(javax.swing.WindowConstan ts.EXIT_ON_CLOSE);
    setName("Form"); // NOI18N

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
    layout.createParallelGroup(javax.swing.GroupLayout .Alignment.LEADING)
    .addGap(0, 400, Short.MAX_VALUE)
    );
    layout.setVerticalGroup(
    layout.createParallelGroup(javax.swing.GroupLayout .Alignment.LEADING)
    .addGap(0, 300, Short.MAX_VALUE)
    );

    pack();
    }// </editor-fold>

    /**
    * @param args the command line arguments
    */
    // Variables declaration - do not modify
    // End of variables declaration

    }

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,874
    Rep Power
    25

    Default

    That loop is supposed to go forever. That is what a while(true) does.
    Why do you have a while(true) there if you don't want it to loop forever?

    What will make the code exit the loop?


    FORMAT YOUR CODE!!! See: http://www.java-forums.org/misc.php?do=bbcode#code

  7. #7
    rowanabejjani is offline Member
    Join Date
    Jun 2011
    Posts
    19
    Rep Power
    0

    Default

    that what i don't know how to make it exit the loop, but the problem is why when i run the file it works, when i run the hole project it loop??

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,874
    Rep Power
    25

    Default

    when i run the file it works
    What is different? What makes the code exit the loop??? There is no code shown to exit the loop.
    When it works, it must not execute the Start() method. Why is that?

    Add lots of printlns that show what the difference is.

  9. #9
    rowanabejjani is offline Member
    Join Date
    Jun 2011
    Posts
    19
    Rep Power
    0

    Default

    it works great when i run only the file.. it drives me crazy, it passed the loop with all the prints,
    if i have another game in the project it can affect this game???

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,874
    Rep Power
    25

    Default

    it passed the loop
    do you mean it did NOT call the Start() method?

    Why does the Start() method loop forever???

  11. #11
    rowanabejjani is offline Member
    Join Date
    Jun 2011
    Posts
    19
    Rep Power
    0

    Default

    yes it call the start(), then the jouer() start()..... no errors or loop forever (sorry for my english)

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,874
    Rep Power
    25

    Default

    Change the while(true) loop to a for() loop that only loops a few times and see what happens.

  13. #13
    rowanabejjani is offline Member
    Join Date
    Jun 2011
    Posts
    19
    Rep Power
    0

    Default

    didn't work :S

  14. #14
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,874
    Rep Power
    25

    Default

    No way to fix a program that "doesn't work".
    You'll have to be more descriptive of what you did and what happened and what should have happened.

  15. #15
    rowanabejjani is offline Member
    Join Date
    Jun 2011
    Posts
    19
    Rep Power
    0

    Default

    ok when i put for() the loop is now on the method jouer().....

  16. #16
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,874
    Rep Power
    25

    Default

    Is that good or bad?
    If bad, please explain.

  17. #17
    rowanabejjani is offline Member
    Join Date
    Jun 2011
    Posts
    19
    Rep Power
    0

    Default

    and i got the screen work (the graphics) its no more white, this is a step

  18. #18
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,874
    Rep Power
    25

    Default

    Are you calling any long running methods from with any listeners?
    This will be a big problem. You must start separate threads for long running methods, otherwise they will cause the GUI to hang.

  19. #19
    rowanabejjani is offline Member
    Join Date
    Jun 2011
    Posts
    19
    Rep Power
    0

    Default

    can you tell me how to do it?

  20. #20
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,874
    Rep Power
    25

    Default

    My suggestion is to write a small simple program with a listener. Then add code to the listener to start another thread. Have the other thread print, sleep and then print to demo it is NOT freezing the GUI.
    Work on that until you get it to work. Post questions here if you have problems.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •