Results 1 to 4 of 4

Thread: thread problem

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

    Default thread problem

    The project stop when arriving at the red sector, can someone please help me how to solve the problem
    a friend told me that i have another thread in another file in the project so how can i fix it.... HELP
    if you need the main project i can send it to you....

    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);}
    }
    }
    });

    //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();
    }

    }
    public void Start(){
    while(true){
    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

    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    The project stop when arriving at the red sector,
    Can you explain what you mean by "stop"?
    Try debugging the code by adding printlns to show where the execution goes. Add as many as you need to find where the code is executing and where it stops.
    Is the program in a loop that needs to exit at some point?

    Please add code tags to your code: BB Code List - Java Forums

    What method is the call to Start() in? Its hard to read with unformatted code.
    You should NOT call a method that loops forever from a listener method.

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

    Default

    ProjetFinal.zip - 4shared.com - online file sharing and storage - download
    this is the project can you download it and help me please, my presentation is tomorrow

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    Sorry, I retired years ago.
    Did you read my post about how to start debugging your code?

Similar Threads

  1. Thread Problem
    By zhein04 in forum Threads and Synchronization
    Replies: 2
    Last Post: 02-07-2011, 12:12 AM
  2. problem creating new thread
    By SeanieSortMeOut in forum Forum Lobby
    Replies: 2
    Last Post: 01-23-2011, 08:40 PM
  3. Thread problem.
    By Mekonom in forum New To Java
    Replies: 4
    Last Post: 03-16-2010, 04:19 PM
  4. thread problem
    By Harris68 in forum Networking
    Replies: 3
    Last Post: 03-03-2010, 07:53 PM
  5. Problem with a thread
    By dotabyss in forum Threads and Synchronization
    Replies: 3
    Last Post: 02-14-2010, 10:56 PM

Posting Permissions

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