Ver Mensaje Individual
  #2 (permalink)  
Antiguo 25/06/2013, 15:09
alvarezcan
 
Fecha de Ingreso: junio-2013
Mensajes: 6
Antigüedad: 10 años, 11 meses
Puntos: 0
Respuesta: Problema con Threads

Ahi va la clase que faltaba.
Código Java:
Ver original
  1. package ciudad;
  2.  
  3. public class Panel extends javax.swing.JPanel implements Runnable {
  4.  
  5.     Thread hilo;
  6.     private Interfaz LaberintoVirtual = new Interfaz();
  7.     private JPanel[][] LaberintoGrafico = new JPanel[30][30];
  8.  
  9.     public Panel() {
  10.  
  11.         initComponents();
  12.         GenerarLaberintoGrafico();
  13.         // hilo = new Thread(this);
  14.  
  15.     }
  16.  
  17.     private void GenerarLaberintoGrafico() {
  18.         for (int i = 0; i < 26; i++) {
  19.             for (int j = 0; j < 26; j++) {
  20.                 LaberintoGrafico[i][j] = new JPanel();
  21.  
  22.                 add(LaberintoGrafico[i][j]);
  23.  
  24.  
  25.             }
  26.         }
  27.  
  28.  
  29.         for (int i = 0; i < 26; i++) {
  30.             for (int j = 0; j < 26; j++) {
  31.                 LaberintoGrafico[i][j].setBounds(i * LaberintoVirtual.DevolverLargoImagenes(), j * LaberintoVirtual.DevolverAlturaImagenes(), LaberintoVirtual.DevolverLargoImagenes(), LaberintoVirtual.DevolverAlturaImagenes());
  32.                 LaberintoGrafico[i][j].validate();
  33.  
  34.             }
  35.         }
  36.         for (int i = 0; i < 26; i++) {
  37.             for (int j = 0; j < 26; j++) {
  38.  
  39.  
  40.                 String a = LaberintoVirtual.DeolverCodigoImagenMatriz(i, j);
  41.  
  42.                 if (a == "x") {
  43.                     LaberintoGrafico[i][j].setBackground(Color.GREEN);
  44.                 }
  45.  
  46.                 if (a == "e" || a == "up" || a == "left" || a == "rigth" || a == "down") {
  47.                     LaberintoGrafico[i][j].setBackground(Color.GRAY);
  48.                 }
  49.  
  50.  
  51.             }
  52.         }
  53.     }
  54.    
  55.    
  56.     public void inicio() {
  57.  
  58.         x = 0;
  59.         y = 60;
  60.         dx = 15;
  61.         dy = 0;
  62.         new Auto().start();
  63.      
  64.        
  65.  
  66.     }
  67.  
  68.     public void pausa() {
  69.         hilo.suspend();
  70.     }
  71.  
  72.     public void reanudar() {
  73.         hilo.resume();
  74.     }
  75.     int x = 0;
  76.     int y = 60;
  77.     int f;
  78.     int d;
  79.     int dx = 15;
  80.     int dy = 0;
  81.     int ban = 1;
  82.  
  83.    
  84.     @Override
  85.     public void paint(Graphics g) {
  86.  
  87.         super.paint(g);
  88.         Toolkit t = Toolkit.getDefaultToolkit();
  89.         Image imagen = t.getImage("auto.gif");
  90.  
  91.         g.drawImage(imagen, x, y, 15, 15, this);
  92.  
  93.     }
  94.    
  95.     @SuppressWarnings("unchecked")
  96.     // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
  97.     private void initComponents() {
  98.  
  99.         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
  100.         this.setLayout(layout);
  101.         layout.setHorizontalGroup(
  102.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  103.             .addGap(0, 400, Short.MAX_VALUE)
  104.         );
  105.         layout.setVerticalGroup(
  106.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  107.             .addGap(0, 300, Short.MAX_VALUE)
  108.         );
  109.     }// </editor-fold>                        
  110.     // Variables declaration - do not modify                    
  111.     // End of variables declaration                  
  112.  
  113.     @Override
  114.     public void run() {
  115.         throw new UnsupportedOperationException("Not supported yet.");
  116.     }
  117.  
  118.     public class Auto extends Thread {
  119.  
  120.         Image imagen;
  121.  
  122.         public Auto() {
  123.              Toolkit t = Toolkit.getDefaultToolkit();
  124.              this.imagen = t.getImage("auto.gif");
  125.          
  126.         }
  127.        
  128.        
  129.         @Override
  130.         public void run() {
  131.            
  132.             System.out.println("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
  133.             try {
  134.  
  135.                 //while(true){
  136.                 System.out.println("ooooooooooooooooooooooooooooooooooo");
  137.                 System.out.println(x + "wwwwwwwwwwwwwwww" + y + "qqqqqqqq" + getHeight() + "rrrrrrrrrr" + getWidth());
  138.                 while (x < getWidth() - 20 && y < getHeight() - 20 && y > -16) {
  139.  
  140.                     System.out.println(x + "dddddddddddddddddddddddddddddddddd" + y);
  141.  
  142.                     Thread.sleep(500);
  143.  
  144.                     x += dx;
  145.                     int incrementoX = dx;
  146.                     y += dy;
  147.                     int incrementoY = dy;
  148.                     repaint();
  149.  
  150.  
  151.                     for (int i = 0; i < 26; i++) {
  152.                         for (int j = 0; j < 26; j++) {
  153.  
  154.                             if (x == LaberintoGrafico[i][j].getX()) {
  155.                                 f = i;
  156.  
  157.  
  158.                             }
  159.  
  160.                             if (y == LaberintoGrafico[i][j].getY()) {
  161.                                 d = j;
  162.  
  163.                             }
  164.                         }
  165.                     }
  166.                     int sent = LaberintoVirtual.DevolverSentido(f, d);
  167.  
  168.                     int random = (int) (Math.random() * 100 + 1);
  169.                     if (sent == 5 && random % 2 == 0) {
  170.                         System.out.println(random + "kkkkkkkkkkkkkkkkkk");
  171.                         //ban=1;
  172.  
  173.                         if (incrementoY > 0) {
  174.                             if (LaberintoVirtual.DevolverSentido(f, d - 1) == 3 && LaberintoVirtual.DevolverSentido(f - 1, d) == 1) {
  175.                                 dx = 15;
  176.                                 dy = 0;
  177.                                 System.out.println("Bajando y dobla a derecha ");
  178.                             }
  179.  
  180.                             if (LaberintoVirtual.DevolverSentido(f, d - 1) == 3 && LaberintoVirtual.DevolverSentido(f - 1, d) == 2) {
  181.                                 dx = -15;
  182.                                 dy = 0;
  183.                                 System.out.println("Bajando y dobla a izqierda ");
  184.                             }
  185.                         }
  186.                         if (incrementoX > 0) {
  187.                             if (LaberintoVirtual.DevolverSentido(f - 1, d) == 1 && LaberintoVirtual.DevolverSentido(f, d - 1) == 4) {
  188.                                 dx = 0;
  189.                                 dy = -15;
  190.                                 System.out.println("Va por derecha y dobla arriba");
  191.                             }
  192.                             if (LaberintoVirtual.DevolverSentido(f - 1, d) == 1 && LaberintoVirtual.DevolverSentido(f, d - 1) == 3) {
  193.                                 dx = 0;
  194.                                 dy = 15;
  195.                                 System.out.println("Va por derecha y dobla abajo");
  196.                             }
  197.                         }
  198.                         if (incrementoX < 0) {
  199.  
  200.                             if (LaberintoVirtual.DevolverSentido(f + 1, d) == 2 && LaberintoVirtual.DevolverSentido(f, d - 1) == 3) {
  201.                                 dx = 0;
  202.                                 dy = 15;
  203.                                 System.out.println("Va por izquierda y dobla abajo");
  204.                             }
  205.  
  206.                             if (LaberintoVirtual.DevolverSentido(f + 1, d) == 2 && LaberintoVirtual.DevolverSentido(f, d - 1) == 4) {
  207.                                 dx = 0;
  208.                                 dy = -15;
  209.                                 System.out.println("Va por izquierda y dobla arriba");
  210.                             }
  211.                         }
  212.  
  213.                         if (incrementoY < 0) {
  214.  
  215.                             if (LaberintoVirtual.DevolverSentido(f, d + 1) == 4 && LaberintoVirtual.DevolverSentido(f - 1, d) == 1) {
  216.                                 dx = 15;
  217.                                 dy = 0;
  218.                                 System.out.println("Va subiendo y dobla a derecha");
  219.                             }
  220.  
  221.                             if (LaberintoVirtual.DevolverSentido(f, d + 1) == 4 && LaberintoVirtual.DevolverSentido(f, d - 1) == 2) {
  222.                                 dx = -15;
  223.                                 dy = 0;
  224.                                 System.out.println("Va subiendo y dobla a izquierda");
  225.                             }
  226.  
  227.                         }
  228.                     }
  229.  
  230.  
  231.                 }
  232.                
  233.                
  234.  
  235.             } catch (Exception e) {
  236.                 System.out.println("sucedio un error" + e.getMessage());
  237.             }
  238.  
  239.  
  240.         }
  241.     }
  242. }

Espero su colaboracion. Gracias.

Última edición por razpeitia; 25/06/2013 a las 19:07