Foros del Web » Programación para mayores de 30 ;) » Programación General »

Problema con Threads

Estas en el tema de Problema con Threads en el foro de Programación General en Foros del Web. Buenas quisiera ayuda... Estoy muy trabado con esto. Es sobre threads. No me instancia mas de un auto. Vean el codigo esta dado clase por ...
  #1 (permalink)  
Antiguo 25/06/2013, 15:08
 
Fecha de Ingreso: junio-2013
Mensajes: 6
Antigüedad: 10 años, 10 meses
Puntos: 0
Pregunta Problema con Threads

Buenas quisiera ayuda... Estoy muy trabado con esto. Es sobre threads. No me instancia mas de un auto. Vean el codigo esta dado clase por clase. Esta programado en NETBEANS 7.2


Código Java:
Ver original
  1. package ciudad;
  2. public class Auto {
  3.          
  4.         Image imagen;
  5.         public int x;
  6.         public int y;
  7.         public int velocidad;
  8.  
  9.     public Auto(Image imagen, int x, int y, int velocidad) {
  10.         Toolkit t = Toolkit.getDefaultToolkit();
  11.         this.imagen = t.getImage("auto.gif");
  12.         this.x = x;
  13.         this.y = y;
  14.         this.velocidad = velocidad;
  15.        
  16.     }
  17.    }        
  18.  
  19. package ciudad;
  20.  
  21. i
  22. public class Interfaz {
  23.  
  24.     private final int LARGO_IMAGENES = 15;
  25.     private final int ALTURA_IMAGENES = 15;
  26.    
  27.     JPanel uno = new JPanel();
  28.     JPanel dos = new JPanel();
  29.     JPanel tre = new JPanel();
  30.     JPanel cua = new JPanel();
  31.     JPanel cin = new JPanel();
  32.     JPanel inter = new JPanel();
  33.    
  34.     Pixel x = new Pixel("x", 0, "9", cin);
  35.     Pixel a = new Pixel("rigth", 1, null, cua); // calle derecha
  36.     Pixel b = new Pixel("left", 2, null, tre); //calle izquierda
  37.     Pixel c = new Pixel("down", 3, null, dos); // calle abajo
  38.     Pixel d = new Pixel("up", 4, null, uno);  // calle arriba
  39.     Pixel e = new Pixel("e", 5, null, inter); // interseccion
  40.     public Pixel InterfazActual[][] = {
  41.         {x, x, x, x, a, x, x, x, x, a, x, x, x, x, b, x, x, x, x, a, x, x, x, x, a, b},
  42.         {x, x, x, x, a, x, x, x, x, a, x, x, x, x, b, x, x, x, x, a, x, x, x, x, a, b},
  43.         {x, x, x, x, a, x, x, x, x, a, x, x, x, x, b, x, x, x, x, a, x, x, x, x, a, b},
  44.         {x, x, x, x, a, x, x, x, x, a, x, x, x, x, b, x, x, x, x, a, x, x, x, x, a, b},
  45.         {c, c, c, c, e, c, c, c, c, e, c, c, c, c, e, c, c, c, c, e, c, c, c, c, e, e},
  46.         {x, x, x, x, a, x, x, x, x, a, x, x, x, x, b, x, x, x, x, a, x, x, x, x, a, b},
  47.         {x, x, x, x, a, x, x, x, x, a, x, x, x, x, b, x, x, x, x, a, x, x, x, x, a, b},
  48.         {x, x, x, x, a, x, x, x, x, a, x, x, x, x, b, x, x, x, x, a, x, x, x, x, a, b},
  49.         {x, x, x, x, a, x, x, x, x, a, x, x, x, x, b, x, x, x, x, a, x, x, x, x, a, b},
  50.         {d, d, d, d, e, d, d, d, d, e, d, d, d, d, e, d, d, d, d, e, d, d, d, d, e, e},
  51.         {c, c, c, c, e, c, c, c, c, e, c, c, c, c, e, c, c, c, c, e, c, c, c, c, e, e},
  52.         {x, x, x, x, a, x, x, x, x, a, x, x, x, x, b, x, x, x, x, a, x, x, x, x, a, b},
  53.         {x, x, x, x, a, x, x, x, x, a, x, x, x, x, b, x, x, x, x, a, x, x, x, x, a, b},
  54.         {x, x, x, x, a, x, x, x, x, a, x, x, x, x, b, x, x, x, x, a, x, x, x, x, a, b},
  55.         {x, x, x, x, a, x, x, x, x, a, x, x, x, x, b, x, x, x, x, a, x, x, x, x, a, b},
  56.         {c, c, c, c, e, c, c, c, c, e, c, c, c, c, e, c, c, c, c, e, c, c, c, c, e, e},
  57.         {x, x, x, x, a, x, x, x, x, a, x, x, x, x, b, x, x, x, x, a, x, x, x, x, a, b},
  58.         {x, x, x, x, a, x, x, x, x, a, x, x, x, x, b, x, x, x, x, a, x, x, x, x, a, b},
  59.         {x, x, x, x, a, x, x, x, x, a, x, x, x, x, b, x, x, x, x, a, x, x, x, x, a, b},
  60.         {x, x, x, x, a, x, x, x, x, a, x, x, x, x, b, x, x, x, x, a, x, x, x, x, a, b},
  61.         {c, c, c, c, e, c, c, c, c, e, c, c, c, c, e, c, c, c, c, e, c, c, c, c, e, e},
  62.         {d, d, d, d, e, d, d, d, d, e, d, d, d, d, e, d, d, d, d, e, d, d, d, d, e, e},
  63.         {x, x, x, x, a, x, x, x, x, a, x, x, x, x, b, x, x, x, x, a, x, x, x, x, a, b},
  64.         {x, x, x, x, a, x, x, x, x, a, x, x, x, x, b, x, x, x, x, a, x, x, x, x, a, b},
  65.         {x, x, x, x, a, x, x, x, x, a, x, x, x, x, b, x, x, x, x, a, x, x, x, x, a, b},
  66.         {x, x, x, x, a, x, x, x, x, a, x, x, x, x, b, x, x, x, x, a, x, x, x, x, a, b},};
  67.  
  68.     public String DeolverCodigoImagenMatriz(int Fila, int Columna) {
  69.         return InterfazActual[Fila][Columna].getTipo();
  70.     }
  71.  
  72.     public int DevolverLargoImagenes() {
  73.         return LARGO_IMAGENES;
  74.     }
  75.  
  76.     public int DevolverAlturaImagenes() {
  77.         return ALTURA_IMAGENES;
  78.     }
  79.  
  80.     public int DevolverSentido(int Fila, int Columna) {
  81.  
  82.         return InterfazActual[Fila][Columna].getSentido();
  83.  
  84.     }
  85. }
  86. package ciudad;
  87.  
  88.  
  89. public class Pixel {
  90.    
  91.    private String tipo;
  92.     private Integer sentido;
  93.     private String contiene;
  94.     private JPanel panel;
  95.  
  96.     public Pixel(String tipo, Integer sentido, String contiene, JPanel panel) {
  97.         this.tipo = tipo;
  98.         this.sentido = sentido;
  99.         this.contiene = contiene;
  100.         this.panel = panel;
  101.     }
  102.  
  103.     public JPanel getPanel() {
  104.         return panel;
  105.     }
  106.  
  107.     public void setPanel(JPanel panel) {
  108.         this.panel = panel;
  109.     }
  110.    
  111.  
  112.     public String getTipo() {
  113.         return tipo;
  114.     }
  115.  
  116.     public void setTipo(String tipo) {
  117.         this.tipo = tipo;
  118.     }
  119.  
  120.     public Integer getSentido() {
  121.         return sentido;
  122.     }
  123.  
  124.     public void setSentido(Integer sentido) {
  125.         this.sentido = sentido;
  126.     }
  127.  
  128.     public String getContiene() {
  129.         return contiene;
  130.     }
  131.  
  132.     public void setContiene(String contiene) {
  133.         this.contiene = contiene;
  134.     }
  135.    
  136.    
  137.    
  138. }
En el paquete Images solo estan las imagenes para que funcionen los autos. Espero su ayuda muchas gracias.

Última edición por razpeitia; 25/06/2013 a las 19:06
  #2 (permalink)  
Antiguo 25/06/2013, 15:09
 
Fecha de Ingreso: junio-2013
Mensajes: 6
Antigüedad: 10 años, 10 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
  #3 (permalink)  
Antiguo 25/06/2013, 15:11
 
Fecha de Ingreso: junio-2013
Mensajes: 6
Antigüedad: 10 años, 10 meses
Puntos: 0
Respuesta: Problema con Threads

Mi principal problema es instanciar otro auto, de modo que el primero siga existiendo.

Etiquetas: java, netbeans, threads
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 00:10.